{"record":{"id":"ca9740ddc0dd019a","repo":"gohugoio/hugo","slug":"value-is-nil-should-be-of-type-s","errorCode":null,"errorMessage":"value is nil; should be of type %s","messagePattern":"value is nil; should be of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/index.go","lineNumber":126,"sourceCode":"\t\t\t}\n\t\tcase reflect.Invalid:\n\t\t\t// the loop holds invariant: v.IsValid()\n\t\t\tpanic(\"unreachable\")\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"can't index item of type %s\", v.Type())\n\t\t}\n\t}\n\treturn v.Interface(), nil\n}\n\n// prepareArg checks if value can be used as an argument of type argType, and\n// converts an invalid value to appropriate zero if possible.\n//\n// Copied from Go stdlib src/text/template/funcs.go.\nfunc prepareArg(value reflect.Value, argType reflect.Type) (reflect.Value, error) {\n\tif !value.IsValid() {\n\t\tif !canBeNil(argType) {\n\t\t\treturn reflect.Value{}, fmt.Errorf(\"value is nil; should be of type %s\", argType)\n\t\t}\n\t\tvalue = reflect.Zero(argType)\n\t}\n\tif !value.Type().AssignableTo(argType) {\n\t\treturn reflect.Value{}, fmt.Errorf(\"value has type %s; should be %s\", value.Type(), argType)\n\t}\n\treturn value, nil\n}\n\n// canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero.\n//\n// Copied from Go stdlib src/text/template/exec.go.\nfunc canBeNil(typ reflect.Type) bool {\n\tswitch typ.Kind() {\n\tcase reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:\n\t\treturn true\n\t}\n\treturn false","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/index.go#L108-L144","documentation":"Thrown by prepareArg (used by doIndex for map indexing) when the supplied index value is reflect.Invalid (untyped nil) and the map's key type is not nillable (canBeNil returns false for non-chan/func/interface/map/pointer/slice kinds). A nil index only makes sense for nillable key types; for concrete key types like int or string it is an error naming the expected type via %s.","triggerScenarios":"Calling `{{ index $intKeyedMap nil }}` where the map is `map[int]...`, or `{{ index $stringKeyedMap $missing }}` where $missing resolved to untyped nil. prepareArg receives an invalid reflect.Value and the map key type (e.g. string, int) cannot hold nil.","commonSituations":"A template variable that was never set (defaults to nil) is used as a map key, or optional front matter is nil and passed straight through. Common with optional params in partials where a key may be absent.","solutions":["Default the key before indexing: `{{ $k := default \"fallback\" $maybeNil }}{{ index $m $k }}`.","Guard with isset/with: `{{ with $maybeNil }}{{ index $m . }}{{ end }}`.","Use a map with a pointer/interface key type if nil keys are legitimate.","Ensure partial callers always pass the key argument."],"exampleFix":"// before\n{{ index $stringMap $missingKey }}\n// after\n{{ with $missingKey }}{{ index $stringMap . }}{{ else }}{{ index $stringMap \"default\" }}{{ end }}","handlingStrategy":"validation","validationCode":"func nonNilMapKey(mapV reflect.Value, key any) (reflect.Value, error) {\n    if key == nil || !reflect.ValueOf(key).IsValid() {\n        kt := mapV.Type().Key()\n        switch kt.Kind() {\n        case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:\n            return reflect.Zero(kt), nil\n        default:\n            return reflect.Value{}, fmt.Errorf(\"nil key invalid for map key type %s\", kt)\n        }\n    }\n    return reflect.ValueOf(key), nil\n}","typeGuard":"func canKeyBeNil(kt reflect.Type) bool {\n    switch kt.Kind() {\n    case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Default optional keys before indexing maps.","Guard with `with`/isset when a key may be nil.","Require partial callers to always pass required keys.","Use a nillable key type only if nil keys are legitimate."],"tags":["go","hugo","template","collections","index","map","nil"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}