{"record":{"id":"d31a869f30ce33d3","repo":"gohugoio/hugo","slug":"value-is-nil-should-be-of-type-s-d31a86","errorCode":null,"errorMessage":"value is nil; should be of type %s","messagePattern":"value is nil; should be of type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/internal/go_templates/texttemplate/funcs.go","lineNumber":150,"sourceCode":"\tif tmpl != nil && tmpl.common != nil {\n\t\ttmpl.muFuncs.RLock()\n\t\tdefer tmpl.muFuncs.RUnlock()\n\t\tif fn := tmpl.execFuncs[name]; fn.IsValid() {\n\t\t\treturn fn, false, true\n\t\t}\n\t}\n\tif fn := builtinFuncs()[name]; fn.IsValid() {\n\t\treturn fn, true, true\n\t}\n\treturn reflect.Value{}, false, false\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.\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 value, nil\n\t}\n\tif intLike(value.Kind()) && intLike(argType.Kind()) && value.Type().ConvertibleTo(argType) {\n\t\tvalue = value.Convert(argType)\n\t\treturn value, nil\n\t}\n\treturn reflect.Value{}, fmt.Errorf(\"value has type %s; should be %s\", value.Type(), argType)\n}\n\nfunc intLike(typ reflect.Kind) bool {\n\tswitch typ {\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\treturn true\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/internal/go_templates/texttemplate/funcs.go#L132-L168","documentation":"Returned by prepareArg when the argument value is invalid (reflect zero = nil/absent) but the target parameter type cannot hold nil (not chan/func/interface/map/pointer/slice). This is raised while coercing template arguments to a func/method/Map key type, e.g. calling a func whose param is int but the template passed an unset variable. The %s is the expected type.","triggerScenarios":"Passing nil to a template function/method/index whose parameter type is non-nillable (int, string, struct, array); an unset pipeline variable ($x never assigned) flowing into a typed arg; a missing map key passed to a typed func.","commonSituations":"Template references a nil context field that feeds a typed func; a partial invoked without expected data; a FuncMap func with a concrete (non-interface) param type receiving a nil dot.","solutions":["Make the func parameter an interface{} or a nillable type if nil is a valid input.","Guard in the template with {{if .Field}}...{{end}} before calling the func.","Provide a non-nil default/zero value for the data before rendering.","Widen the func signature to accept and explicitly handle nil."],"exampleFix":"// before\n// func: func(n int) string\n{{ lower .Missing }} // .Missing is nil -> error\n\n// after\n// func: func(v any) string\n{{ if .Missing }}{{ lower .Missing }}{{ end }}","handlingStrategy":"type-guard","validationCode":"func safeCallFunc(fn func(int) string, v any) (string, error) {\n    if v == nil {\n        return \"\", fmt.Errorf(\"argument is nil\")\n    }\n    n, ok := v.(int)\n    if !ok {\n        return \"\", fmt.Errorf(\"argument not int: %T\", v)\n    }\n    return fn(n), nil\n}","typeGuard":"func isNonNilAssignable(v any, t reflect.Type) bool {\n    if v == nil { return false }\n    rv := reflect.ValueOf(v)\n    return rv.IsValid() && rv.Type().AssignableTo(t)\n}","tryCatchPattern":"if err := t.Execute(w, data); err != nil {\n    if strings.Contains(err.Error(), \"value is nil; should be of type\") {\n        // provide defaults for nil fields and retry once\n        data = withDefaults(data)\n        err = t.Execute(w, data)\n    }\n}","preventionTips":["Prefer interface{} params in FuncMap funcs when nil is possible.","Guard template calls with {{if .Field}}...{{end}}.","Populate non-nil defaults in the data layer."],"tags":["go","text-template","funcs","reflect","type-coercion"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}