gohugoio/hugo · error

invalid path

Error message

invalid path %q

What it means

Raised by pagesFromDataTemplateContext.toPathSitesMap (pagesfromgotmpl.go:74) when the `path` field of the map passed to AddPage/AddResource cannot be converted to a string by cast.ToStringE. The path field is mandatory for pages created from a _content.gotmpl content adapter.

Solutions

  1. Ensure the map passed to AddPage contains a `path` key whose value is a string.
  2. Convert non-string sources explicitly: `printf "/posts/%d.md" .ID`.
  3. Use the lowercase key `path` exactly (map keys are case-sensitive here).

Example fix

{{/* before */}}
{{ .AddPage (dict "Path" "foo.md" "title" "Foo") }}

{{/* after */}}
{{ .AddPage (dict "path" "/foo.md" "title" "Foo") }}
Defensive patterns

Strategy: validation

Type guard

{{/* In _content.gotmpl, validate path before AddPage */}}
{{ $path := .path }}
{{ if and $path (reflect.IsMap .) }}
  {{ .AddPage (dict "path" (printf "/%s" $path) "title" .title) }}
{{ else }}
  {{ warnf "AddPage requires a string 'path'" }}
{{ end }}

Prevention

When it happens

Trigger: Calling `AddPage` in a _content.gotmpl template with a map that omits `path` or sets it to a non-string (number, slice, map, bool). The cast fails and the invalid (often empty) path value is reported.

Common situations: Building a content adapter and forgetting the path key; passing a path computed from a numeric ID without converting to string; typos like `Path` (capitalized) instead of `path`.

Related errors


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

Appendix: source

Thrown at hugolib/pagesfromdata/pagesfromgotmpl.go:74

	// The return value will always be an empty string.
	EnableAllLanguages() string
}

var _ PagesFromDataTemplateContext = (*pagesFromDataTemplateContext)(nil)

type pagesFromDataTemplateContext struct {
	p *PagesFromTemplate
}

func (p *pagesFromDataTemplateContext) toPathSitesMap(v any) (string, map[string]any, map[string]any, error) {
	m, err := hmaps.ToStringMapE(v)
	if err != nil {
		return "", nil, nil, err
	}

	path, err := cast.ToStringE(m["path"])
	if err != nil {
		return "", nil, nil, fmt.Errorf("invalid path %q", path)
	}

	sites := hmaps.ToStringMap(m["sites"])

	return path, sites, m, nil
}

func (p *pagesFromDataTemplateContext) AddPage(v any) (string, error) {
	path, sites, m, err := p.toPathSitesMap(v)
	if err != nil {
		return "", err
	}

	hash, hasChanged := p.p.buildState.checkHasChangedAndSetSourceInfo(path, sites, m)
	if !hasChanged {
		return "", nil
	}

View on GitHub (pinned to 52c9bd7908)