gohugoio/hugo · error

intersect does not support slices or arrays of uncomparable…

Error message

intersect does not support slices or arrays of uncomparable types

What it means

Intersect (collections.go:288-290) builds a seen map keyed by element value (intersector.seen is map[any]bool at line 283), which requires l1's element type to be comparable. Slices, maps, funcs as elements are not comparable and are rejected per-element.

Solutions

  1. Intersect only comparable element types (strings, numbers, comparable structs)
  2. Map elements to a comparable key (e.g. .File.Path) before intersecting
  3. Reformulate using Uniq + In over a flattened comparable collection

Example fix

// before
{{ intersect $sliceOfSlices $b }}
// after
{{ intersect (apply $sliceOfSlices "string") (apply $b "string") }}
Defensive patterns

Strategy: type-guard

Validate before calling

{{/* project to a comparable key (string) before intersect */}}
{{ $a := apply $a "string" }}
{{ $b := apply $b "string" }}
{{ intersect $a $b }}

Type guard

{{/* Go-side */}}
func elemTypeIsComparable(v any) bool {
  t := reflect.TypeOf(v)
  if t.Kind() != reflect.Slice && t.Kind() != reflect.Array {
    return false
  }
  return t.Elem().Comparable()
}

Prevention

When it happens

Trigger: {{ intersect $sliceOfSlices $other }} where l1 element type is []int, []string, map[...], or any non-comparable type.

Common situations: Intersecting collections of slices; Pages whose struct contains a slice field making the type non-comparable; [][]int.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/e7d546a69f4cfb51. Report an issue: GitHub.

Appendix: source

Thrown at tpl/collections/collections.go:289

func (ns *Namespace) Intersect(l1, l2 any) (any, error) {
	if l1 == nil || l2 == nil {
		return make([]any, 0), nil
	}

	var ins *intersector

	l1v := reflect.ValueOf(l1)
	l2v := reflect.ValueOf(l2)

	switch l1v.Kind() {
	case reflect.Array, reflect.Slice:
		ins = &intersector{r: reflect.MakeSlice(l1v.Type(), 0, 0), seen: make(map[any]bool)}
		switch l2v.Kind() {
		case reflect.Array, reflect.Slice:
			for i := range l1v.Len() {
				l1vv := l1v.Index(i)
				if !l1vv.Type().Comparable() {
					return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types")
				}

				for j := range l2v.Len() {
					l2vv := l2v.Index(j)
					if !l2vv.Type().Comparable() {
						return make([]any, 0), errors.New("intersect does not support slices or arrays of uncomparable types")
					}

					ins.handleValuePair(l1vv, l2vv)
				}
			}
			return ins.r.Interface(), nil
		default:
			return nil, errors.New("can't iterate over " + reflect.ValueOf(l2).Type().String())
		}
	default:
		return nil, errors.New("can't iterate over " + reflect.ValueOf(l1).Type().String())
	}

View on GitHub (pinned to 52c9bd7908)