mikefarah/yq · error

reduce must be given a variables assignment, got %v instead

Error message

reduce must be given a variables assignment, got %v instead

What it means

The `reduce` operator requires its expression to have the shape `$var := (...)` — the left-hand side must be a variable assignment operation. This check fires when the LHS operation is something else (a plain expression, comparison, etc.). yq throws it because reduce's accumulator update semantics depend on the variable-assignment form.

Source

Thrown at pkg/yqlib/operator_reduce.go:19

package yqlib

import (
	"container/list"
	"fmt"
)

func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	log.Debugf("reduceOp")
	//.a as $var reduce (0; . + $var)
	//lhs is the assignment operator
	//rhs is the reduce block
	// '.' refers to the current accumulator, initialised to 0
	// $var references a single element from the .a

	//ensure lhs is actually an assignment
	//and rhs is a block (empty)
	if expressionNode.LHS.Operation.OperationType != assignVariableOpType {
		return Context{}, fmt.Errorf("reduce must be given a variables assignment, got %v instead", expressionNode.LHS.Operation.OperationType.Type)
	} else if expressionNode.RHS.Operation.OperationType != blockOpType {
		return Context{}, fmt.Errorf("reduce must be given a block, got %v instead", expressionNode.RHS.Operation.OperationType.Type)
	}

	arrayExpNode := expressionNode.LHS.LHS
	array, err := d.GetMatchingNodes(context, arrayExpNode)

	if err != nil {
		return Context{}, err
	}

	variableName := expressionNode.LHS.RHS.Operation.StringValue

	initExp := expressionNode.RHS.LHS

	accum, err := d.GetMatchingNodes(context, initExp)
	if err != nil {
		return Context{}, err

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Use the full correct form: `reduce <arrayExpr> as $var (<init>; <update>)`.
  2. Ensure the part before `as` is the array expression, not the assignment — the assignment is the whole `as $var` clause.
  3. Compare against working examples in yq docs (e.g. `reduce .[] as $item (0; . + $item)`).
  4. Check operator precedence — parenthesize the array expression if it contains operators.

Example fix

// before
yq 'reduce .a = $x (0; . + $x)' file.yml
// after
yq 'reduce .a[] as $x (0; . + $x)' file.yml
Defensive patterns

Strategy: try-catch

Validate before calling

echo "$expr" | grep -Eq '^reduce .+ as \\$[a-zA-Z_]+ \\(' || echo "reduce must use the form: reduce <array> as \$var (init; update)"

Try / catch

out=$(yq "$expr" file.yml 2>&1) || { echo "$out"; case "$out" in *"variables assignment"*) echo "fix reduce LHS: add 'as \$var'";; esac; }

Prevention

When it happens

Trigger: `yq 'reduce .a as $x (0; ...)'` is correct, but writing malformed reduce syntax such as `reduce .a (0; . + 1)` or `reduce (.a + 1) as $x (0; ...)` where the LHS is not an assignment; also inner misplacements like `reduce .a = $x (0; ...)` using the wrong assignment operator.

Common situations: Translating jq reduce syntax incorrectly; typos dropping the `as $var` clause; older/mistyped examples; confusing `=` with `:=`/`as` assignment forms.

Related errors


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