mikefarah/yq · error

'%v' expects 1 arg but received none

Error message

'%v' expects 1 arg but received none

What it means

createExpressionTree rejects a postfix operation whose arity is 1 but whose argument stack is empty. Certain unary operators (like first) are special-cased to allow a missing argument; everything else errors instead of building a node with nil RHS. It is returned from ParseExpression, so the whole expression fails to compile.

Source

Thrown at pkg/yqlib/expression_parser.go:63

	if len(postFixPath) == 0 {
		return nil, nil
	}

	for _, Operation := range postFixPath {
		var newNode = ExpressionNode{Operation: Operation}
		log.Debugf("pathTree %v ", Operation.toString())
		if Operation.OperationType.NumArgs > 0 {
			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
			}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Supply the missing operand before the operator (e.g. `yq '.a * 2'`)
  2. Quote the expression in shell and check that interpolated variables are non-empty
  3. Check the error's operator name to see which part of the expression is truncated

Example fix

// before
VAL=""; yq ".a * $VAL" file.yml   # '*' expects 1 arg but received none
// after
VAL="2"; yq ".a * $VAL" file.yml
Defensive patterns

Strategy: validation

Validate before calling

expr := ".a * 2"
// sanity check: expression should not end with a lone binary/unary operator
if regexp.MustCompile(`[+\-*/%]$`).MatchString(strings.TrimSpace(expr)) {
    return fmt.Errorf("expression %q ends with an operator", expr)
}

Try / catch

out, err := runYq(expr, file)
if err != nil && strings.Contains(err.Error(), "expects 1 arg but received none") {
    // log expr and prompt user to supply the missing operand
    return fmt.Errorf("malformed expression %q: missing operand", expr)
}

Prevention

When it happens

Trigger: Running yq with an expression that starts with (or contains) a unary/binary operator with no preceding value, e.g. `yq '.a *' f.yml`, `yq 'sort_keys'` on contexts where the op requires an arg, or an expression like `yq '+ 1'` where '+' gets no LHS.

Common situations: Typos in shell scripts where a value got dropped by an unquoted/empty shell variable (e.g. `yq ".a $VAL"` with VAL empty becoming just an operator), copy-pasted expressions missing an operand.

Related errors


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