gohugoio/hugo · error

call of nil

Error message

call of nil

What it means

Raised by the `call` template action (`{{call .Fn args}}`) when the function value is invalid (funcs.go:313-315) — i.e. untyped nil. `call` requires an actual function value; a nil interface cannot be invoked.

Source

Thrown at tpl/internal/go_templates/texttemplate/funcs.go:315

	switch item.Kind() {
	case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
		return item.Len(), nil
	}
	return 0, fmt.Errorf("len of type %s", item.Type())
}

// Function invocation

func emptyCall(fn reflect.Value, args ...reflect.Value) reflect.Value {
	panic("unreachable") // implemented as a special case in evalCall
}

// call returns the result of evaluating the first argument as a function.
// The function must return 1 result, or 2 results, the second of which is an error.
func call(name string, fn reflect.Value, args ...reflect.Value) (reflect.Value, error) {
	fn = indirectInterface(fn)
	if !fn.IsValid() {
		return reflect.Value{}, fmt.Errorf("call of nil")
	}
	typ := fn.Type()
	if typ.Kind() != reflect.Func {
		return reflect.Value{}, fmt.Errorf("non-function %s of type %s", name, typ)
	}

	if err := goodFunc(name, typ); err != nil {
		return reflect.Value{}, err
	}
	numIn := typ.NumIn()
	var dddType reflect.Type
	if typ.IsVariadic() {
		if len(args) < numIn-1 {
			return reflect.Value{}, fmt.Errorf("wrong number of args for %s: got %d want at least %d", name, len(args), numIn-1)
		}
		dddType = typ.In(numIn - 1).Elem()
	} else {
		if len(args) != numIn {

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Guard: `{{with .Fn}}{{call .}}{{end}}`.
  2. Provide a default no-op function in Go.
  3. Verify the name/value is set in the FuncMap or context.

Example fix

// before
{{call .Render}}   // .Render is nil

// after
{{with .Render}}{{call .}}{{end}}
Defensive patterns

Strategy: validation

Validate before calling

// Guard the callable:
//   {{with .Fn}}{{call .}}{{end}}
// Provide a no-op default in Go if the value may be absent.

Type guard

func isCallable(v interface{}) bool {
    return v != nil && reflect.TypeOf(v).Kind() == reflect.Func
}

Prevention

When it happens

Trigger: `{{call .Handler}}` where `.Handler` is nil or absent from the context; passing a missing map key as a function; calling a variable that a conditional branch left unset.

Common situations: Optional callbacks; function maps where the name was misspelled; conditional logic that only assigns the callable in some branches.

Related errors


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