gohugoio/hugo · error
complement needs at least two arguments
Error message
complement needs at least two arguments
What it means
Thrown by the Complement template function when fewer than 2 arguments are provided (complement.go:33). Complement treats all args except the last as 'subtrahend' sets and the last as the 'universe' to filter; with <2 args there is no universe to subtract from, so it is rejected immediately.
Solutions
- Provide at least two arguments: a subtraction set and a universe, e.g. {{ $all | complement $exclude }}.
- If you only have one set and want it unchanged, do not call complement at all.
- Verify the pipe: {{ .Pages | complement $last4 }} yields args = [$last4, .Pages].
Example fix
// before
{{ complement $exclude }}
// after
{{ .Pages | complement $exclude }} Defensive patterns
Strategy: validation
Validate before calling
{{/* complement needs >=2 args; pipe form supplies them */}}
{{ .Pages | complement $exclude }} Prevention
- Always provide a universe AND at least one subtraction set.
- Use the pipe form {{ $all | complement $exclude }} to guarantee two args.
- Do not call complement with a single argument.
When it happens
Trigger: Calling {{ complement }} (no args) or {{ complement $only }} (one arg). In templates the pipe form {{ .Pages | complement $last4 }} supplies two args; forgetting the piped left-hand operand or calling with a literal single arg triggers it.
Common situations: Refactoring a pipeline so .Pages no longer flows in; using complement as a unary filter by mistake; removing the 'universe' argument.
Related errors
- invalid number of arguments to Seq
- arguments to complement must be slices or arrays
- both count and seq must be provided
- can't evaluate the array by no match argument or more than…
- cannot index slice/array with nil
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/126e4be5d53d0913.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/collections/complement.go:34
import (
"errors"
"fmt"
"reflect"
"github.com/gohugoio/hugo/common/hreflect"
)
// Complement gives the elements in the last element of ls that are not in
// any of the others.
//
// All elements of ls must be slices or arrays of comparable types.
//
// The reasoning behind this rather clumsy API is so we can do this in the templates:
//
// {{ $c := .Pages | complement $last4 }}
func (ns *Namespace) Complement(ls ...any) (any, error) {
if len(ls) < 2 {
return nil, errors.New("complement needs at least two arguments")
}
universe := ls[len(ls)-1]
as := ls[:len(ls)-1]
aset, err := collectIdentities(as...)
if err != nil {
return nil, err
}
v := reflect.ValueOf(universe)
switch v.Kind() {
case reflect.Array, reflect.Slice:
sl := reflect.MakeSlice(v.Type(), 0, 0)
for i := range v.Len() {
ev, _ := hreflect.Indirect(v.Index(i))
if _, found := aset[normalize(ev)]; !found {
sl = reflect.Append(sl, ev)View on GitHub (pinned to 52c9bd7908)