{"record":{"id":"404f7cb06a13ad65","repo":"gohugoio/hugo","slug":"can-t-iterate-over-a-nil-value-404f7c","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/collections.go","lineNumber":82,"sourceCode":"// After returns all the items after the first n items in list l.\nfunc (ns *Namespace) After(n any, l any) (any, error) {\n\tif n == nil || l == nil {\n\t\treturn nil, errors.New(\"both limit and seq must be provided\")\n\t}\n\n\tnv, err := cast.ToIntE(n)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif nv < 0 {\n\t\treturn nil, errors.New(\"sequence bounds out of range [\" + cast.ToString(nv) + \":]\")\n\t}\n\n\tlv := reflect.ValueOf(l)\n\tlv, isNil := hreflect.Indirect(lv)\n\tif isNil {\n\t\treturn nil, errors.New(\"can't iterate over a nil value\")\n\t}\n\n\tswitch lv.Kind() {\n\tcase reflect.Array, reflect.Slice, reflect.String:\n\t\t// okay\n\tdefault:\n\t\treturn nil, errors.New(\"can't iterate over \" + reflect.ValueOf(l).Type().String())\n\t}\n\n\tif nv >= lv.Len() {\n\t\treturn lv.Slice(0, 0).Interface(), nil\n\t}\n\n\treturn lv.Slice(nv, lv.Len()).Interface(), nil\n}\n\n// Delimit takes a given list l and returns a string delimited by sep.\n// If last is passed to the function, it will be used as the final delimiter.","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L64-L100","documentation":"After (collections.go:79-83) calls reflect.Indirect on the list; a typed-nil pointer/slice (isNil==true) hits this branch. Literal nil is caught earlier at line 66, so this specifically means a non-nil interface wrapping nil.","triggerScenarios":"Passing a nil *Pages or nil pointer to a slice that is wrapped in a non-nil interface to after.","commonSituations":".Params.tags typed as a pointer that is unset; a range variable that resolved to a typed nil; a section method returning nil.","solutions":["Guard: {{ with $seq }}{{ after n . }}{{ end }}","Coalesce to empty slice: {{ after n (default $seq slice) }}"],"exampleFix":"// before\n{{ after 2 $nilPtr }}\n// after\n{{ with $nilPtr }}{{ after 2 . }}{{ end }}","handlingStrategy":"validation","validationCode":"{{ with $seq }}\n  {{ after 2 . }}\n{{ end }}","typeGuard":"{{ with $seq }}...{{ end }}","tryCatchPattern":null,"preventionTips":["Wrap optional collections in {{ with }} before after","Coalesce typed-nil sources with (default $v slice)","Audit section/param methods that may return typed-nil"],"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-14T05:17:10.506Z"}