gohugoio/hugo · error

failed to load data

Error message

failed to load data: %w

What it means

Sent via SendError when HugoSites.Data() triggers the data init resetter (loading /data folders) and it fails. The %w is the underlying data-load error; Data() returns nil and forwards this error to the error channel.

Solutions

  1. Unwrap the %w — it typically points to 'data: failed to open' or a parse error with file/line context.
  2. Validate every file in /data with the relevant linter (yamllint, jq, toml-cli) and fix syntax errors.
  3. Confirm file encodings are UTF-8 (not UTF-8-BOM / binary) for all /data files.
  4. Temporarily move recently-edited data files aside to isolate the offender.

Example fix

# before: /data/config.yaml with bad indentation
site:
 name: Demo
enabled true   # missing colon

# after
site:
 name: Demo
enabled: true
Defensive patterns

Strategy: validation

Validate before calling

// Lint all data files before building: yamllint, jq, toml-cli per extension.
for _, f := range dataFiles { if err := lintByExt(f); err != nil { return err } }

Try / catch

if err := h.init.data.Do(ctx); err != nil {
    // data load failed; surface but allow build to proceed if data is non-critical
    h.Log.Errorf("data load failed (continuing): %v", err)
}

Prevention

When it happens

Trigger: Produced at hugo_sites.go:339 when h.init.data.Do(ctx) returns a non-nil error — i.e. the lazy one-shot loader that walks and parses /data files fails (open/parse/unmarshal of any data file).

Common situations: A malformed YAML/JSON/TOML file in /data; a data file that cannot be opened (permissions/encoding); a syntax error introduced by an editor; very large or binary data files causing parse failures.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/b0c44fb34cd40022. Report an issue: GitHub.

Appendix: source

Thrown at hugolib/hugo_sites.go:339

	defer f.mu.Unlock()
	return f.err
}

func (f *fatalErrorHandler) Done() <-chan bool {
	return f.donec
}

type hugoSitesInit struct {
	// Loads the data from all of the /data folders.
	data hsync.FuncResetter

	// Loads the Git info and CODEOWNERS for all the pages if enabled.
	gitInfo hsync.FuncResetter
}

func (h *HugoSites) Data() map[string]any {
	if err := h.init.data.Do(context.Background()); err != nil {
		h.SendError(fmt.Errorf("failed to load data: %w", err))
		return nil
	}
	return h.data
}

// Pages returns all pages for all sites.
func (h *HugoSites) Pages() page.Pages {
	key := "pages"
	v, err := h.cachePages.GetOrCreate(key, func(string) (page.Pages, error) {
		var pages page.Pages
		for _, s := range h.Sites {
			pages = append(pages, s.Pages()...)
		}
		page.SortByDefault(pages)
		return pages, nil
	})
	if err != nil {
		panic(err)

View on GitHub (pinned to 52c9bd7908)