{"id":"06178fa1a7ce81d6","repo":"gohugoio/hugo","slug":"input-was-nil","errorCode":null,"errorMessage":"input was nil","messagePattern":"input was nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"parser/frontmatter.go","lineNumber":35,"sourceCode":"\t\"encoding/json\"\n\t\"errors\"\n\t\"io\"\n\n\t\"github.com/gohugoio/hugo/parser/metadecoders\"\n\n\ttoml \"github.com/pelletier/go-toml/v2\"\n\n\txml \"github.com/clbanning/mxj/v2\"\n)\n\nconst (\n\tyamlDelimLf = \"---\\n\"\n\ttomlDelimLf = \"+++\\n\"\n)\n\nfunc InterfaceToConfig(in any, format metadecoders.Format, w io.Writer) error {\n\tif in == nil {\n\t\treturn errors.New(\"input was nil\")\n\t}\n\n\tswitch format {\n\tcase metadecoders.YAML:\n\t\tb, err := metadecoders.MarshalYAML(in)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t_, err = w.Write(b)\n\t\treturn err\n\n\tcase metadecoders.TOML:\n\t\tenc := toml.NewEncoder(w)\n\t\tenc.SetIndentTables(true)\n\t\treturn enc.Encode(in)\n\tcase metadecoders.JSON:\n\t\tb, err := json.MarshalIndent(in, \"\", \"   \")","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/parser/frontmatter.go#L17-L53","documentation":"parser.InterfaceToConfig (and InterfaceToFrontMatter, which delegates to it) refuses a nil input value before marshalling to YAML/TOML/JSON/XML. Serializing nil would emit meaningless output like \"null\" or an empty document into a config or front matter block, so Hugo fails fast at the API boundary.","triggerScenarios":"Calling parser.InterfaceToConfig(nil, format, w) or parser.InterfaceToFrontMatter(nil, ...) — reached from code paths that serialize page front matter or config, such as `hugo convert toTOML/toYAML/toJSON` or archetype/new-content generation, when the parsed front matter came back nil.","commonSituations":"Running `hugo convert` over content files that have no front matter at all (parse yields nil); custom tooling built on Hugo's parser package passing an unchecked nil map; a nil `any`-typed variable produced by a failed earlier decode being fed straight into serialization.","solutions":["Check the value before calling: skip serialization or substitute an empty map (map[string]any{}) when the front matter is nil.","When using `hugo convert`, add minimal front matter (even an empty --- block) to content files that lack it.","Trace upstream why the input is nil — often a prior Unmarshal error was ignored — and handle that error instead of passing nil through."],"exampleFix":"// before\nerr := parser.InterfaceToFrontMatter(fm, metadecoders.TOML, w)\n// after\nif fm == nil {\n    fm = map[string]any{}\n}\nerr := parser.InterfaceToFrontMatter(fm, metadecoders.TOML, w)","handlingStrategy":"type-guard","validationCode":"if r == nil {\n    return errors.New(\"no input reader\")\n}","typeGuard":"func hasInput(r io.Reader) bool { return r != nil }\n// also guard typed-nil readers:\n// var f *os.File; io.Reader(f) != nil but f is nil — check the concrete value before wrapping","tryCatchPattern":"pf, err := pageparser.ParseFrontMatterAndContent(r)\nif err != nil {\n    return fmt.Errorf(\"parse %s: %w\", filename, err)\n}","preventionTips":["Check the error from os.Open/fs.Open before passing the reader on — a failed open is the usual source of a nil reader","Avoid typed-nil interface values (a nil *os.File stored in an io.Reader passes a != nil check at the interface level in some patterns)","Fail fast at the call site rather than passing possibly-nil readers down the stack"],"tags":["hugo","frontmatter","marshal","nil-check"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}