gohugoio/hugo · error
strings: negative Repeat count
Error message
strings: negative Repeat count
What it means
Thrown by strings.Repeat when the count argument n is negative (tpl/strings/strings.go:601-602). Repeat returns n copies of a string; Go's strings.Repeat panics on negative counts, so Hugo guards explicitly and returns this error instead of crashing the build.
Solutions
- Clamp the count to a minimum of zero: {{ repeat (math.Max 0 .N) "ab" }}.
- Guard with an if: {{ if ge .N 0 }}{{ repeat .N "ab" }}{{ end }}.
- Fix upstream logic so the count cannot be negative.
Example fix
// before
{{ repeat .N "ab" }}
// after
{{ if ge .N 0 }}{{ repeat .N "ab" }}{{ end }} Defensive patterns
Strategy: validation
Validate before calling
{{ $n := math.Max 0 .N }}
{{ repeat $n "ab" }} Prevention
- Clamp repeat counts to >= 0 before calling.
- Guard computed counts with {{ if ge .N 0 }}.
When it happens
Trigger: Calling {{ repeat -1 "ab" }} or {{ "ab" | repeat (sub 0 5) }} where n evaluates to a negative integer.
Common situations: Computing a repeat count from an expression that can go negative (e.g. padding math); passing a negated variable; defaulting an unset param to -1.
Related errors
- ellipsis must be a string
- length argument must be an integer
- start argument must be an integer
- text must be a string
- too many arguments passed to truncate
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/9f8a29c648b14096.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/strings/strings.go:602
}
return strings.TrimSuffix(ss, sx), nil
}
// Repeat returns a new string consisting of n copies of the string s.
func (ns *Namespace) Repeat(n, s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}
sn, err := cast.ToIntE(n)
if err != nil {
return "", err
}
if sn < 0 {
return "", errors.New("strings: negative Repeat count")
}
return strings.Repeat(ss, sn), nil
}
View on GitHub (pinned to 52c9bd7908)