{"record":{"id":"17ae395dd73fb23d","repo":"mikefarah/yq","slug":"setpath-expected-single-value-on-rhs-but-found-v","errorCode":null,"errorMessage":"SETPATH: expected single value on RHS but found %v","messagePattern":"SETPATH: expected single value on RHS but found (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_path.go","lineNumber":80,"sourceCode":"\n\tif err != nil {\n\t\treturn Context{}, err\n\t}\n\n\tlhsTraversalTree := createTraversalTree(lhsPath, traversePreferences{}, false)\n\n\tassignmentOp := &Operation{OperationType: assignOpType}\n\n\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tcandidate := el.Value.(*CandidateNode)\n\n\t\ttargetContextValue, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS.RHS)\n\t\tif err != nil {\n\t\t\treturn Context{}, err\n\t\t}\n\n\t\tif targetContextValue.MatchingNodes.Len() != 1 {\n\t\t\treturn Context{}, fmt.Errorf(\"SETPATH: expected single value on RHS but found %v\", targetContextValue.MatchingNodes.Len())\n\t\t}\n\n\t\trhsOp := &Operation{OperationType: referenceOpType, CandidateNode: targetContextValue.MatchingNodes.Front().Value.(*CandidateNode)}\n\n\t\tassignmentOpNode := &ExpressionNode{\n\t\t\tOperation: assignmentOp,\n\t\t\tLHS:       lhsTraversalTree,\n\t\t\tRHS:       &ExpressionNode{Operation: rhsOp},\n\t\t}\n\n\t\t_, err = d.GetMatchingNodes(context.SingleChildContext(candidate), assignmentOpNode)\n\n\t\tif err != nil {\n\t\t\treturn Context{}, err\n\t\t}\n\n\t}\n\treturn context, nil","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_path.go#L62-L98","documentation":"The value part (RHS after the semicolon) of SETPATH must evaluate to exactly one node per document, since that single node is assigned at the given path. This error is thrown when the value expression produces zero or multiple results for a candidate document.","triggerScenarios":"'SETPATH([\"a\"]; .items[])' where .items has 0 or many elements, 'SETPATH([\"a\"]; .missing[])' (0 results), or any iterator/optional expression on the value side.","commonSituations":"Value expressions using .[] or select that fan out or match nothing, documents with unexpected shapes (empty arrays), scripts ported from contexts where multi-value assignment was fine (plain '=' behaves differently).","solutions":["Wrap the value with an aggregator that yields one result: '.items | first', '(.items[]) as such — use [ ... ] | first' or '... // \"default\"'","Use a scalar/construct expression: 'SETPATH([\"a\"]; [.items[]])' assigns the whole array as one value","Verify the value expression returns exactly one node with 'yq '<expr> | length''"],"exampleFix":"// before\nyq 'SETPATH([\"names\"]; .people[].name)' f.yml   // multiple results\n// after\nyq 'SETPATH([\"names\"]; [.people[].name])' f.yml  // single array value","handlingStrategy":"validation","validationCode":"yq '<valueExpr> | length' f.yml  # must print exactly 1 per document","typeGuard":"yq 'select((<valueExpr> | length) == 1)' f.yml","tryCatchPattern":"// shell\nout=$(yq 'SETPATH([\"a\"]; .items[])' f.yml 2>&1) || { echo \"RHS not single: $out\"; exit 1; }","preventionTips":["Wrap fan-out expressions into arrays: [...] to make one value","Provide defaults with '// fallback' so zero matches become one value","Test value expressions standalone before embedding in SETPATH"],"tags":["yq","setpath","cardinality","path-expression"],"backgroundTag":"unexpected-match-count","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}