gohugoio/hugo · error

errMustOneNumberError

errMustOneNumberError

Error message

must provide at least one number

What it means

errMustOneNumberError is the companion sentinel for variadic math functions that require at least one operand, such as math.Sum and math.Product. If the flattened argument list is empty, there is nothing to aggregate and Hugo returns this error rather than a fabricated identity value.

Source

Thrown at tpl/math/math.go:31

// Package math provides template functions for mathematical operations.
package math

import (
	"errors"
	"fmt"
	"math"
	"math/rand"
	"reflect"

	_math "github.com/gohugoio/hugo/common/math"
	"github.com/gohugoio/hugo/deps"
	"github.com/spf13/cast"
)

var (
	errMustTwoNumbersError = errors.New("must provide at least two numbers")
	errMustOneNumberError  = errors.New("must provide at least one number")
)

// New returns a new instance of the math-namespaced template functions.
func New(d *deps.Deps) *Namespace {
	return &Namespace{
		d: d,
	}
}

// Namespace provides template functions for the "math" namespace.
type Namespace struct {
	d *deps.Deps
}

// Abs returns the absolute value of n.
func (ns *Namespace) Abs(n any) (float64, error) {
	af, err := cast.ToFloat64E(n)
	if err != nil {

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Guard for emptiness first: `{{ if $nums }}{{ math.Sum $nums }}{{ else }}0{{ end }}`.
  2. Seed the call with an identity value: `{{ math.Sum 0 $nums }}` (or `1` for Product).
  3. Check the upstream filter (`where`, `.Pages`) actually yields elements in all render contexts.

Example fix

<!-- before -->
{{ math.Sum $weights }}
<!-- after -->
{{ math.Sum 0 $weights }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $nums := .Params.scores | default slice }}
{{ if gt (len $nums) 0 }}{{ math.Max $nums }}{{ end }}

Try / catch

{{ with try (math.Min $nums) }}{{ if .Err }}{{ warnf "no numbers: %s" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ math.Sum slice }}` or `{{ math.Product }}` — calling an aggregate math function with no arguments, or with only empty slices that flatten to zero numbers.

Common situations: Summing a collected slice that is empty because a `where` filter matched nothing; iterating pages with no numeric param set and building an empty argument list; templates that run on sections with zero children.

Related errors


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