mikefarah/yq · error

can only use merge anchors with maps (!!map) or sequences…

Error message

can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got sequence containing %v

What it means

While exploding a merge-key ('<<') anchor whose value is a sequence, fixedReconstructAliasedMap found an element of that sequence that is not itself a map. YAML merge semantics require each merged entry to be a mapping (or a single map), so a sequence containing scalars/arrays cannot be merged into the parent map and the explode fails.

Solutions

  1. Make every element of the merge sequence a mapping
  2. Use a single map as the merge value: <<: *anchor
  3. If the sequence of non-maps is intentional, merge it manually with e.g. *a + *b instead of '<<'
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/yqlib/operator_anchors_aliases.go:178 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/8bd48c0e5013b700. Report an issue: GitHub.

Appendix: source

Thrown at pkg/yqlib/operator_anchors_aliases.go:178

			sequence := valueNode
			if sequence.Kind == AliasNode {
				sequence = sequence.Alias
			}
			if sequence.Kind != SequenceNode {
				sequence = &CandidateNode{Content: []*CandidateNode{sequence}}
			}
			for index := 0; index < len(sequence.Content); index = index + 1 {
				// for merge anchors, we only set them if the key is not already in node or the newContent
				mergeNodeSeq := sequence.Content[index]
				if mergeNodeSeq.Kind == AliasNode {
					mergeNodeSeq = mergeNodeSeq.Alias
				}
				mergeNodeSeq = mergeNodeSeq.Copy()
				if err := explodeNode(mergeNodeSeq, Context{}); err != nil {
					return err
				}
				if mergeNodeSeq.Kind != MappingNode {
					return fmt.Errorf("can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got sequence containing %v", mergeNodeSeq.Tag)
				}
				itemsToAdd := mergeNodeSeq.FilterMapContentByKey(func(keyNode *CandidateNode) bool {
					return getContentValueByKey(node.Content, keyNode.Value) == nil &&
						getContentValueByKey(newContent, keyNode.Value) == nil
				})

				for _, item := range itemsToAdd {
					copied := item.Copy()
					copied.Parent = node
					newContent = append(newContent, copied)
				}
			}
		}
	}
	node.Content = newContent
	return nil
}

View on GitHub (pinned to 8b5af0694b)