gohugoio/hugo · error

input was nil

Error message

input was nil

What it means

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.

Source

Thrown at parser/frontmatter.go:35

	"encoding/json"
	"errors"
	"io"

	"github.com/gohugoio/hugo/parser/metadecoders"

	toml "github.com/pelletier/go-toml/v2"

	xml "github.com/clbanning/mxj/v2"
)

const (
	yamlDelimLf = "---\n"
	tomlDelimLf = "+++\n"
)

func InterfaceToConfig(in any, format metadecoders.Format, w io.Writer) error {
	if in == nil {
		return errors.New("input was nil")
	}

	switch format {
	case metadecoders.YAML:
		b, err := metadecoders.MarshalYAML(in)
		if err != nil {
			return err
		}

		_, err = w.Write(b)
		return err

	case metadecoders.TOML:
		enc := toml.NewEncoder(w)
		enc.SetIndentTables(true)
		return enc.Encode(in)
	case metadecoders.JSON:
		b, err := json.MarshalIndent(in, "", "   ")

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Check the value before calling: skip serialization or substitute an empty map (map[string]any{}) when the front matter is nil.
  2. When using `hugo convert`, add minimal front matter (even an empty --- block) to content files that lack it.
  3. Trace upstream why the input is nil — often a prior Unmarshal error was ignored — and handle that error instead of passing nil through.

Example fix

// before
err := parser.InterfaceToFrontMatter(fm, metadecoders.TOML, w)
// after
if fm == nil {
    fm = map[string]any{}
}
err := parser.InterfaceToFrontMatter(fm, metadecoders.TOML, w)
Defensive patterns

Strategy: type-guard

Validate before calling

if r == nil {
    return errors.New("no input reader")
}

Type guard

func hasInput(r io.Reader) bool { return r != nil }
// also guard typed-nil readers:
// var f *os.File; io.Reader(f) != nil but f is nil — check the concrete value before wrapping

Try / catch

pf, err := pageparser.ParseFrontMatterAndContent(r)
if err != nil {
    return fmt.Errorf("parse %s: %w", filename, err)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/06178fa1a7ce81d6.json. Report an issue: GitHub ↗.