gohugoio/hugo · error

readData: failed to open data file: %w

Error message

readData: failed to open data file: %w

What it means

Returned by HugoSites.readData() when opening the data file to read its bytes fails. Similar to the handleDataFile open error but occurs in the parse path (readData), wrapping with 'readData:' prefix.

Source

Thrown at hugolib/hugo_sites.go:810

				"higher precedence %T data already in the data tree", data, r.Path(), higherPrecedentData)
		}

	default:
		h.Log.Errorf("unexpected data type %T in file %s", data, r.LogicalName())
	}

	return nil
}

func (h *HugoSites) errWithFileContext(err error, f *source.File) error {
	realFilename := f.FileInfo().Meta().Filename
	return herrors.NewFileErrorFromFile(err, realFilename, h.Fs.Source, nil)
}

func (h *HugoSites) readData(f *source.File) (any, error) {
	file, err := f.FileInfo().Meta().Open()
	if err != nil {
		return nil, fmt.Errorf("readData: failed to open data file: %w", err)
	}
	defer file.Close()
	content := helpers.ReaderToBytes(file)

	format := metadecoders.FormatFromString(f.Ext())
	return metadecoders.Default.Unmarshal(content, format)
}

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Reproduce with a clean build (no concurrent editor) to rule out a watch race.
  2. Verify the file named in the error trace is present and readable.
  3. Check for symlinked data files whose target is missing.
  4. If data is on a network/afero mount, ensure stability/retry at the source.

Example fix

// before: broken symlink in data/
ln -s ../shared/langs.yaml data/langs.yaml
# shared/langs.yaml later removed

// after: remove or fix the symlink
rm data/langs.yaml
cp ../shared/langs.yaml data/langs.yaml
Defensive patterns

Strategy: try-catch

Validate before calling

// Re-stat the file just before opening to detect vanished files.
if _, err := os.Stat(filename); err != nil { return nil, fmt.Errorf("data file gone: %w", err) }

Try / catch

file, err := f.FileInfo().Meta().Open()
if err != nil {
    if errors.Is(err, fs.ErrNotExist) { h.Log.Warnf("transient: %v", err); return nil, nil }
    return nil, fmt.Errorf("readData: failed to open data file: %w", err)
}

Prevention

When it happens

Trigger: Raised at hugo_sites.go:810 when f.FileInfo().Meta().Open() fails inside readData() — the file handle could not be obtained for reading content to unmarshal.

Common situations: Same family as 329 but encountered slightly later in the pipeline (after the walker's open succeeded earlier in a race); file removed between walk and read; permission revoked between operations; network/afero-backed data source temporarily unavailable.

Related errors


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