{"record":{"id":"8d60a7e3c67bb913","repo":"mikefarah/yq","slug":"setpath-expected-single-path-but-found-v-results","errorCode":null,"errorMessage":"SETPATH: expected single path but found %v results instead","messagePattern":"SETPATH: expected single path but found (.+?) results instead","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_path.go","lineNumber":57,"sourceCode":"\treturn path, nil\n}\n\n// SETPATH(pathArray; value)\nfunc setPathOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {\n\tlog.Debugf(\"SetPath\")\n\n\tif expressionNode.RHS.Operation.OperationType != blockOpType {\n\t\treturn Context{}, fmt.Errorf(\"SETPATH must be given a block (;), got %v instead\", expressionNode.RHS.Operation.OperationType.Type)\n\t}\n\n\tlhsPathContext, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS.LHS)\n\n\tif err != nil {\n\t\treturn Context{}, err\n\t}\n\n\tif lhsPathContext.MatchingNodes.Len() != 1 {\n\t\treturn Context{}, fmt.Errorf(\"SETPATH: expected single path but found %v results instead\", lhsPathContext.MatchingNodes.Len())\n\t}\n\tlhsValue := lhsPathContext.MatchingNodes.Front().Value.(*CandidateNode)\n\n\tlhsPath, err := getPathArrayFromNode(\"SETPATH\", lhsValue)\n\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 {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_path.go#L39-L75","documentation":"The first argument of SETPATH must evaluate to exactly one path array. This error reports how many results the LHS-of-semicolon expression produced when that count is not 1 (0 when the expression matches nothing, >1 when it fans out over multiple nodes).","triggerScenarios":"'SETPPATH' with a path expression matching multiple nodes, e.g. 'SETPATH(.items[]; v)' or 'SETPATH(.a[], .b[]; ...)' style expressions, or matching nothing ('SETPATH(.nonexistent[]; v)' -> 0 results).","commonSituations":"Using wildcard/iterators (.[], .[]) in the path expression, documents whose shape differs from what the script assumed, missing 'first' to limit matches.","solutions":["Select exactly one array: 'SETPATH(.paths | first; v)' or add select filters to narrow to one match","Use the array literal form 'SETPATH([\"a\",\"b\"]; v)' so exactly one path array is produced","Check the path expression output count with 'yq '<expr> | length'' and fix the document/expression"],"exampleFix":"// before\nyq 'SETPATH(.paths[]; \"x\")' f.yml    // multiple matches\n// after\nyq 'SETPATH(.paths | first; \"x\")' f.yml","handlingStrategy":"validation","validationCode":"yq '<pathExpr> | length' f.yml  # must print exactly 1","typeGuard":"yq 'select((<pathExpr> | length) == 1)' f.yml","tryCatchPattern":"// shell\nout=$(yq 'SETPATH(.p[]; v)' f.yml 2>&1) || { echo \"not a single path: $out\"; exit 1; }","preventionTips":["Avoid iterators (.[]) in SETPATH path expressions","Use array literals or 'first' to guarantee one result","Check result counts with '| length' during script development"],"tags":["yq","setpath","path-expression","cardinality"],"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"}