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
- Guard for emptiness first: `{{ if $nums }}{{ math.Sum $nums }}{{ else }}0{{ end }}`.
- Seed the call with an identity value: `{{ math.Sum 0 $nums }}` (or `1` for Product).
- 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 passing a slice to math.Max/math.Min/math.Sum/math.Product, verify it is non-empty first
- Empty front-matter arrays and filtered-out results are the usual cause — check after `where`/`first` filtering
- Decide a meaningful empty-case value in the template rather than relying on the function
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
- errMustTwoNumbersError
- need at least 2 arguments to append
- must provide a Resource and optionally an options map
- operator argument must be string type
- can't evaluate the array by no match argument or more than o
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/3a7d926c1ef9b7b5.json.
Report an issue: GitHub ↗.