gohugoio/hugo · error

can't evaluate an invalid value

Error message

can't evaluate an invalid value

What it means

Raised by evaluateSubElem in Hugo's `where` template function when the reflect.Value it is asked to descend into is invalid (the zero reflect.Value). This happens when a dotted key path references data that doesn't exist at all — reflection cannot inspect a value that was never there. Hugo fails fast rather than silently treating the missing value as a non-match at this stage of sub-element resolution.

Source

Thrown at tpl/collections/where.go:314

		if svp != nil && smvp != nil {
			re, err := hstrings.GetOrCompileRegexp(*smvp)
			if err != nil {
				return false, err
			}
			if re.MatchString(*svp) {
				return true, nil
			}
			return false, nil
		}
	default:
		return false, errors.New("no such operator")
	}
	return false, nil
}

func evaluateSubElem(ctx, obj reflect.Value, elemName string) (reflect.Value, error) {
	if !obj.IsValid() {
		return zero, errors.New("can't evaluate an invalid value")
	}

	typ := obj.Type()
	obj, isNil := hreflect.Indirect(obj)

	// first, check whether obj has a method. In this case, obj is
	// a struct or its pointer. If obj is a struct,
	// to check all T and *T method, use obj pointer type Value
	objPtr := obj
	if !hreflect.IsInterfaceOrPointer(objPtr.Kind()) && objPtr.CanAddr() {
		objPtr = objPtr.Addr()
	}

	mt := hreflect.GetMethodByNameForType(objPtr.Type(), elemName)
	if mt.Func.IsValid() {
		// Receiver is the first argument.
		args := []reflect.Value{objPtr}
		num := mt.Type.NumIn()

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Check the key path spelling in the `where` call against the actual data structure.
  2. Ensure every record in the collection has the nested field, or pre-filter: `where (where $c "address" "!=" nil) "address.city" "Oslo"`.
  3. Normalize your data files so all entries share the same schema.

Example fix

<!-- before -->
{{ range where .Site.Data.people "address.city" "Oslo" }}
<!-- after: filter out entries missing the nested map first -->
{{ $withAddr := where .Site.Data.people "address" "!=" nil }}
{{ range where $withAddr "address.city" "Oslo" }}
Defensive patterns

Strategy: validation

Validate before calling

{{/* skip nil/invalid entries before where */}}
{{ $items := slice }}
{{ range $c }}{{ if . }}{{ $items = $items | append . }}{{ end }}{{ end }}
{{ $r := where $items "Field" "v" }}

Prevention

When it happens

Trigger: Calling `where` (or `Site.Pages.GroupByParam`-style helpers routed through evaluateSubElem) with a dotted key like `where .Site.Data.people "address.city" "Oslo"` where an intermediate element (`address`) is a missing map key returning an invalid reflect.Value, or the collection contains untyped nil interface entries.

Common situations: Data files (data/*.yaml/json) where some records omit a nested field used in the where key path; front matter params missing on some pages; typo in the nested key path; iterating heterogeneous slices where some elements are nil interfaces.

Related errors


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