mikefarah/yq · error

'%v' expects 2 args but there is %v

Error message

'%v' expects 2 args but there is %v

What it means

A postfix operation with arity 2 (e.g. arithmetic, comparison, assignment) found fewer than 2 operands on the stack. The error reports how many args were actually present. Raised during expression compilation in createExpressionTree.

Source

Thrown at pkg/yqlib/expression_parser.go:71

			numArgs := Operation.OperationType.NumArgs
			switch numArgs {
			case 1:
				if len(stack) < 1 {
					// Allow certain unary ops to accept zero args by interpreting missing RHS as nil
					// TODO - make this more general on OperationType
					if Operation.OperationType == firstOpType {
						// no RHS provided; proceed without popping
						break
					}
					return nil, fmt.Errorf("'%v' expects 1 arg but received none", strings.TrimSpace(Operation.StringValue))
				}
				remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
				newNode.RHS = rhs
				rhs.Parent = &newNode
				stack = remaining
			case 2:
				if len(stack) < 2 {
					return nil, fmt.Errorf("'%v' expects 2 args but there is %v", strings.TrimSpace(Operation.StringValue), len(stack))
				}
				remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
				newNode.LHS = lhs
				lhs.Parent = &newNode

				newNode.RHS = rhs
				rhs.Parent = &newNode

				stack = remaining
			}
		}
		stack = append(stack, &newNode)
	}
	if len(stack) != 1 {
		return nil, fmt.Errorf("bad expression, please check expression syntax")
	}
	return stack[0], nil
}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Provide both operands for the operator (e.g. `.a + .b`)
  2. Quote the whole expression so the shell doesn't split or drop parts
  3. Count operands around the operator named in the error

Example fix

// before
yq '.a +' file.yml    # '+' expects 2 args but there is 1
// after
yq '.a + .b' file.yml
Defensive patterns

Strategy: validation

Validate before calling

expr := ".a + .b"
if strings.TrimSpace(expr) == "" {
    return errors.New("empty expression")
}
// reject trailing binary operators
if regexp.MustCompile(`(\+|-|\*|/|==|!=|and|or)\s*$`).MatchString(expr) {
    return fmt.Errorf("expression %q is missing an operand", expr)
}

Try / catch

out, err := runYq(expr, file)
if err != nil && strings.Contains(err.Error(), "expects 2 args") {
    return fmt.Errorf("binary operator in %q lacks operands: %w", expr, err)
}

Prevention

When it happens

Trigger: Expressions like `yq '.a + ' f.yml` (trailing operator, one operand), or `yq '. and'` — any binary operator missing one or both operands so len(stack) < 2 when it is reduced.

Common situations: Broken shell interpolation dropping one side of an expression, hand-edited CI expressions, truncation from improperly quoted expressions containing spaces or special chars.

Related errors


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