{"record":{"id":"213e79de0d5af49d","repo":"gohugoio/hugo","slug":"can-t-iterate-over-a-nil-value","errorCode":null,"errorMessage":"can't iterate over a nil value","messagePattern":"can't iterate over a nil value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/apply.go","lineNumber":39,"sourceCode":"\t\"strings\"\n\n\t\"github.com/gohugoio/hugo/common/hreflect\"\n)\n\n// Apply takes an array or slice c and returns a new slice with the function fname applied over it.\nfunc (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any) (any, error) {\n\tif c == nil {\n\t\treturn make([]any, 0), nil\n\t}\n\n\tif fname == \"apply\" {\n\t\treturn nil, errors.New(\"can't apply myself (no turtles allowed)\")\n\t}\n\n\tseqv := reflect.ValueOf(c)\n\tseqv, isNil := hreflect.Indirect(seqv)\n\tif isNil {\n\t\treturn nil, errors.New(\"can't iterate over a nil value\")\n\t}\n\n\tfnv, found := ns.lookupFunc(ctx, fname)\n\tif !found {\n\t\treturn nil, errors.New(\"can't find function \" + fname)\n\t}\n\n\tswitch seqv.Kind() {\n\tcase reflect.Array, reflect.Slice:\n\t\tr := make([]any, seqv.Len())\n\t\tfor i := range seqv.Len() {\n\t\t\tvv := seqv.Index(i)\n\n\t\t\tvvv, err := applyFnToThis(ctx, fnv, vv, args...)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/apply.go#L21-L57","documentation":"Apply (apply.go:36-40) passes c through reflect + hreflect.Indirect; if that yields a typed-nil pointer/slice (isNil==true) it bails. Literal nil is handled earlier at lines 28-30 (returns empty slice), so this path specifically means a non-nil interface wrapping a nil pointer.","triggerScenarios":"Passing a *Pages, *[]Page, or nil map typed as a concrete pointer type to apply, where the underlying value is nil but the interface is non-nil.","commonSituations":"A page-collection variable declared but never assigned; frontmatter param that resolves to a nil pointer; .Next/.Prev on the last/first page returning a typed-nil Page.","solutions":["Guard with {{ with $items }}{{ apply . \"fn\" }}{{ end }}","Coalesce to an empty slice: {{ apply (default $items slice) \"fn\" }}","Check {{ if $items }} before calling apply"],"exampleFix":"// before\n{{ apply $maybeNil \"upper\" }}\n// after\n{{ with $maybeNil }}{{ apply . \"upper\" }}{{ end }}","handlingStrategy":"validation","validationCode":"{{ with $items }}\n  {{ apply . \"upper\" }}\n{{ end }}","typeGuard":"{{/* non-empty typed guard: with skips typed-nil and zero values */}}\n{{ with $items }}...{{ end }}","tryCatchPattern":null,"preventionTips":["Use {{ with }} around any optional collection before applying","Coalesce optional vars with (default $v slice)","Watch for typed-nil returns from .Next/.Prev and section methods"],"tags":["hugo","go-template","collections","nil-check","reflect"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}