mikefarah/yq · error

%v cannot be added to %v

Error message

%v cannot be added to %v

What it means

addScalars fell through every supported combination — its branches cover string+string, int/float+int/float, dates and custom-tag pairs. The left and right scalar tags (printed in the message, e.g. !!str and !!bool) have no addition defined, so the operator refuses rather than guessing a coercion.

Source

Thrown at pkg/yqlib/operator_add.go:145

		target.Value = fmt.Sprintf(format, sum)
	} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
		lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
		if err != nil {
			return err
		}
		rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
		if err != nil {
			return err
		}
		sum := lhsNum + rhsNum
		if lhsIsCustom {
			target.Tag = lhs.Tag
		} else {
			target.Tag = "!!float"
		}
		target.Value = fmt.Sprintf("%v", sum)
	} else {
		return fmt.Errorf("%v cannot be added to %v", lhsTag, rhsTag)
	}
	return nil
}

func addDateTimes(layout string, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {

	duration, err := time.ParseDuration(rhs.Value)
	if err != nil {
		return fmt.Errorf("unable to parse duration [%v]: %w", rhs.Value, err)
	}

	currentTime, err := parseDateTime(layout, lhs.Value)
	if err != nil {
		return err
	}

	newTime := currentTime.Add(duration)
	target.Value = newTime.Format(layout)

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Convert operands to compatible types first (tostring, tonumber)
  2. Check for accidental concatenation of string and number — use tostring on the number
  3. For dates, ensure both sides are datetimes or a datetime plus a duration string
Defensive patterns

Strategy: type-guard

When it happens

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