{"record":{"id":"14f8ead909124ec7","repo":"gohugoio/hugo","slug":"sequence-must-be-provided","errorCode":null,"errorMessage":"sequence must be provided","messagePattern":"sequence must be provided","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/sort.go","lineNumber":33,"sourceCode":"\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"reflect\"\n\t\"sort\"\n\t\"strings\"\n\n\t\"github.com/gohugoio/hugo/common/hmaps\"\n\t\"github.com/gohugoio/hugo/common/hreflect\"\n\t\"github.com/gohugoio/hugo/langs\"\n\t\"github.com/gohugoio/hugo/tpl/compare\"\n\t\"github.com/spf13/cast\"\n)\n\n// Sort returns a sorted copy of the list l.\nfunc (ns *Namespace) Sort(ctx context.Context, l any, args ...any) (any, error) {\n\tif l == nil {\n\t\treturn nil, errors.New(\"sequence must be provided\")\n\t}\n\n\tseqv, isNil := hreflect.Indirect(reflect.ValueOf(l))\n\tif isNil {\n\t\treturn nil, errors.New(\"can't iterate over a nil value\")\n\t}\n\n\tctxv := reflect.ValueOf(ctx)\n\n\tvar sliceType reflect.Type\n\tswitch seqv.Kind() {\n\tcase reflect.Array, reflect.Slice:\n\t\tsliceType = seqv.Type()\n\tcase reflect.Map:\n\t\tsliceType = reflect.SliceOf(seqv.Type().Elem())\n\tdefault:\n\t\treturn nil, errors.New(\"can't sort \" + reflect.ValueOf(l).Type().String())\n\t}","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/sort.go#L15-L51","documentation":"The `sort` template function returns a sorted copy of a list. If the list argument is the literal Go `nil` interface value (not a typed nil), this error fires. The check at sort.go:32 distinguishes a truly absent/nil sequence from a typed nil pointer, which produces a different error.","triggerScenarios":"Calling `{{ sort nil }}` or passing a template variable that is unset and evaluates to the untyped nil interface, e.g. `{{ sort .NonexistentVariable }}` where the variable was never assigned.","commonSituations":"A template references a `.Scratch.Get` result that was never set, or a partial receives an empty context, yielding untyped nil. The developer expected an empty slice but got nil.","solutions":["Default the value to an empty slice before sorting: `{{ $list = $list | default slice }}` then `{{ sort $list }}`.","Fix the upstream data source so the variable is always a slice or map.","Add a conditional: `{{ with $list }}{{ sort . }}{{ end }}`."],"exampleFix":"// before\n{{ sort .MaybeNilList }}\n// after\n{{ $list := .MaybeNilList | default slice }}\n{{ sort $list }}","handlingStrategy":"validation","validationCode":"{{ with $list }}{{ sort . }}{{ else }}{{ slice }}{{ end }}","typeGuard":"func isNonNilSequence(v any) bool {\n    if v == nil { return false }\n    kind := reflect.TypeOf(v).Kind()\n    return kind == reflect.Slice || kind == reflect.Array || kind == reflect.Map\n}","tryCatchPattern":null,"preventionTips":["Default list variables with `| default slice` before sorting.","Use `{{ with }}` to guard against absent values.","Initialize upstream lookups to empty slices instead of nil."],"tags":["hugo","template","collections","sort","nil-check"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}