gohugoio/hugo · error
is not a valid duration unit
Error message
%q is not a valid duration unit
What it means
Thrown by time.Duration when the unit string is not in the allowed set: nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m, hour/h. The lookup map durationUnits rejects anything else, printing the offending unit via %q.
Solutions
- Use one of the supported units exactly: ns, us, µs, ms, s, m, h, or the full names.
- For plural intent, use the singular full name ('second', 'minute').
- If you need a Go duration string like '5m30s', use time.ParseDuration via the Duration function's other overload instead.
Example fix
// before:
{{ time.Duration "sec" 5 }}
// after:
{{ time.Duration "second" 5 }} Defensive patterns
Strategy: validation
Validate before calling
{{ $validUnits := slice "ns" "us" "µs" "ms" "s" "m" "h" }}
{{ if in $validUnits $unit }}{{ time.Duration $unit $n }}{{ else }}0{{ end }} Prevention
- Use only the documented unit strings (ns/us/µs/ms/s/m/h or full names).
- Avoid pluralized forms.
- Whitelist-check dynamic unit values against the allowed set before calling.
When it happens
Trigger: Calling {{ time.Duration "sec" 5 }} or any unit not in the allowed set. The function casts unit to string then checks the map; a miss raises this error before the numeric conversion.
Common situations: Using pluralized units ('seconds', 'minutes'), abbreviations outside the set ('sec', 'min'), or passing a non-unit string by mistake. Also seen when the unit variable is nil/empty.
Related errors
- cannot convert to time.Duration
- config needs a filename
- format not supported
- 'increment' must be > 0
- 'increment' must be < 0
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/d384f635c32c96c7.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/time/time.go:143
"ms": time.Millisecond,
"second": time.Second,
"s": time.Second,
"minute": time.Minute,
"m": time.Minute,
"hour": time.Hour,
"h": time.Hour,
}
// Duration converts the given number to a time.Duration.
// Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h.
func (ns *Namespace) Duration(unit any, number any) (time.Duration, error) {
unitStr, err := cast.ToStringE(unit)
if err != nil {
return 0, err
}
unitDuration, found := durationUnits[unitStr]
if !found {
return 0, fmt.Errorf("%q is not a valid duration unit", unit)
}
n, err := cast.ToInt64E(number)
if err != nil {
return 0, err
}
return time.Duration(n) * unitDuration, nil
}
View on GitHub (pinned to 52c9bd7908)