mikefarah/yq · error

%v (%v) cannot be added to a %v (%v)

Error message

%v (%v) cannot be added to a %v (%v)

What it means

Fired by the add operator's type guard when merging nodes: the left-hand operand is a mapping (!!map) but the right-hand operand is not, so yq cannot merge them. Only map+map (merge), string+string (concat), number+number (arithmetic), and array+array (append) combinations are supported; any map paired with a non-map hits this guard.

Source

Thrown at pkg/yqlib/operator_add.go:66

func add(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
	lhsNode := lhs

	if lhs == nil && rhs == nil {
		return nil, nil
	} else if lhs == nil {
		return rhs.Copy(), nil
	} else if rhs == nil {
		return lhs.Copy(), nil
	} else if lhsNode.Tag == "!!null" {
		return lhs.CopyAsReplacement(rhs), nil
	}

	target := lhs.CopyWithoutContent()

	switch lhsNode.Kind {
	case MappingNode:
		if rhs.Kind != MappingNode {
			return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
		}
		addMaps(target, lhs, rhs)
	case SequenceNode:
		addSequences(target, lhs, rhs)
	case ScalarNode:
		if rhs.Kind != ScalarNode {
			return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
		}
		target.Kind = ScalarNode
		target.Style = lhsNode.Style
		if err := addScalars(context, target, lhsNode, rhs); err != nil {
			return nil, err
		}
	}
	return target, nil
}

func addScalars(context Context, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Ensure both operands are maps, e.g. `.a + .b` where both `.a` and `.b` are mappings
  2. Convert the right-hand side before adding, e.g. `.a + (.b | to_entries | from_entries)` if it was parsed as a different type
  3. Select a specific key to add instead of the whole node, e.g. `.target + .source.someField`
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/yqlib/operator_add.go:66 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/f02fc42c5586a18c. Report an issue: GitHub.