mikefarah/yq · error

SETPATH must be given a block (;), got %v instead

Error message

SETPATH must be given a block (;), got %v instead

What it means

SETPATH uses the syntax SETPATH(pathArray; value) where the semicolon forms a block operation separating the path from the value. This error is thrown when the RHS of the SETPATH call is not a block — i.e. the value part was omitted or a comma/other operator was used instead of a semicolon.

Source

Thrown at pkg/yqlib/operator_path.go:47

			number, err := parseInt(childNode.Value)
			if err != nil {
				return nil, fmt.Errorf("%v: could not parse %v as an int: %w", funcName, childNode.Value, err)
			}
			path[i] = number
		default:
			return nil, fmt.Errorf("%v: expected either a !!str or !!int in the path, found %v instead", funcName, childNode.Tag)
		}

	}
	return path, nil
}

// SETPATH(pathArray; value)
func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	log.Debugf("SetPath")

	if expressionNode.RHS.Operation.OperationType != blockOpType {
		return Context{}, fmt.Errorf("SETPATH must be given a block (;), got %v instead", expressionNode.RHS.Operation.OperationType.Type)
	}

	lhsPathContext, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS.LHS)

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

	if lhsPathContext.MatchingNodes.Len() != 1 {
		return Context{}, fmt.Errorf("SETPATH: expected single path but found %v results instead", lhsPathContext.MatchingNodes.Len())
	}
	lhsValue := lhsPathContext.MatchingNodes.Front().Value.(*CandidateNode)

	lhsPath, err := getPathArrayFromNode("SETPATH", lhsValue)

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

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Use the semicolon to separate path from value: 'SETPATH(["a"]; 3)'
  2. If you only wanted to compute/update a path, consider the plain assignment form '.a = 3' instead
  3. Check quoting in shell scripts — an unquoted ';' terminates the shell command; wrap the expression in single quotes

Example fix

// before (shell)
yq 'SETPATH(["a"], 3)' f.yml
// after
yq 'SETPATH(["a"]; 3)' f.yml
Defensive patterns

Strategy: validation

Validate before calling

# sanity check expression contains the ';' separator before invoking
[[ "$expr" == *SETPATH*";"* ]] || { echo "SETPATH needs SETPATH(path; value)"; exit 1; }

Try / catch

// shell
out=$(yq "$expr" f.yml 2>&1) || { echo "expression error: $out"; exit 1; }

Prevention

When it happens

Trigger: 'SETPATH(["a"])' (no value), 'SETPATH(["a"], "v")' (comma instead of semicolon), or 'SETPATH(["a"]; v; extra)' variants that parse into a different operation type.

Common situations: Copy-pasted jq syntax (jq uses ';' too but errors differ), typos where ';' became ',', users assuming SETPATH takes one argument, macro-generated expressions.

Related errors


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