gohugoio/hugo · error
can't iterate over a nil value
Error message
can't iterate over a nil value
What it means
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.
Source
Thrown at tpl/collections/collections.go:82
// After returns all the items after the first n items in list l.
func (ns *Namespace) After(n any, l any) (any, error) {
if n == nil || l == nil {
return nil, errors.New("both limit and seq must be provided")
}
nv, err := cast.ToIntE(n)
if err != nil {
return nil, err
}
if nv < 0 {
return nil, errors.New("sequence bounds out of range [" + cast.ToString(nv) + ":]")
}
lv := reflect.ValueOf(l)
lv, isNil := hreflect.Indirect(lv)
if isNil {
return nil, errors.New("can't iterate over a nil value")
}
switch lv.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
// okay
default:
return nil, errors.New("can't iterate over " + reflect.ValueOf(l).Type().String())
}
if nv >= lv.Len() {
return lv.Slice(0, 0).Interface(), nil
}
return lv.Slice(nv, lv.Len()).Interface(), nil
}
// Delimit takes a given list l and returns a string delimited by sep.
// If last is passed to the function, it will be used as the final delimiter.View on GitHub (pinned to 52c9bd7908)
Solutions
- Guard: {{ with $seq }}{{ after n . }}{{ end }}
- Coalesce to empty slice: {{ after n (default $seq slice) }}
Example fix
// before
{{ after 2 $nilPtr }}
// after
{{ with $nilPtr }}{{ after 2 . }}{{ end }} Defensive patterns
Strategy: validation
Validate before calling
{{ with $seq }}
{{ after 2 . }}
{{ end }} Type guard
{{ with $seq }}...{{ end }} Prevention
- Wrap optional collections in {{ with }} before after
- Coalesce typed-nil sources with (default $v slice)
- Audit section/param methods that may return typed-nil
When it happens
Trigger: Passing a nil *Pages or nil pointer to a slice that is wrapped in a non-nil interface to after.
Common situations: .Params.tags typed as a pointer that is unset; a range variable that resolved to a typed nil; a section method returning nil.
Related errors
- can't iterate over a nil value
- called apply using {xt} as type {targ}
- both limit and seq must be provided
- intersect does not support slices or arrays of uncomparable
- need at least 2 arguments to append
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/404f7cb06a13ad65.
Report an issue: GitHub.