{"record":{"id":"3ad928aa6ec472a8","repo":"mikefarah/yq","slug":"rhs-of-as-operator-must-be-a-variable-name-e-g","errorCode":null,"errorMessage":"RHS of 'as' operator must be a variable name e.g. $foo","messagePattern":"RHS of 'as' operator must be a variable name e\\.g\\. \\$foo","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_variables.go","lineNumber":60,"sourceCode":"\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tresult, err := variableLoopSingleChild(d, context.SingleChildContext(el.Value.(*CandidateNode)), originalExp)\n\t\tif err != nil {\n\t\t\treturn Context{}, err\n\t\t}\n\t\tresults.PushBackList(result.MatchingNodes)\n\t}\n\treturn context.ChildContext(results), nil\n}\n\nfunc variableLoopSingleChild(d *dataTreeNavigator, context Context, originalExp *ExpressionNode) (Context, error) {\n\n\tvariableExp := originalExp.LHS\n\tlhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), variableExp.LHS)\n\tif err != nil {\n\t\treturn Context{}, err\n\t}\n\tif variableExp.RHS.Operation.OperationType.Type != \"GET_VARIABLE\" {\n\t\treturn Context{}, fmt.Errorf(\"RHS of 'as' operator must be a variable name e.g. $foo\")\n\t}\n\tvariableName := variableExp.RHS.Operation.StringValue\n\n\tprefs := variableExp.Operation.Preferences.(assignVarPreferences)\n\n\tresults := list.New()\n\n\t// now we loop over lhs, set variable to each result and calculate originalExp.Rhs\n\tfor el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tlog.Debugf(\"PROCESSING VARIABLE: %v\", NodeToString(el.Value.(*CandidateNode)))\n\t\tvar variableValue = list.New()\n\t\tif prefs.IsReference {\n\t\t\tvariableValue.PushBack(el.Value)\n\t\t} else {\n\t\t\tcandidateCopy := el.Value.(*CandidateNode).Copy()\n\t\t\tvariableValue.PushBack(candidateCopy)\n\t\t}\n\t\tnewContext := context.ChildContext(context.MatchingNodes)","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_variables.go#L42-L78","documentation":"variableLoopSingleChild inspects the RHS of the 'as' expression (e.g. '. as $foo') and its guard found the right-hand side is not a variable binding — the parse produced something other than a $name token. This is a syntax-shape check: 'as' must be followed immediately by a variable name for the loop to bind each result.","triggerScenarios":"Writing `yq '.a as x | ...'` (missing `$`), `.a as ($x + 1) | ...`, or otherwise placing a computed expression where only a variable name is allowed.","commonSituations":"Typos omitting the `$` prefix, copying jq patterns where yq's grammar is stricter, or shell interpolation accidentally mangling the `$x` (single vs double quoting in bash eats `$x`).","solutions":["Use a bare dollar variable as RHS: `.a as $x | ...`","If using double quotes in bash, escape the variable (`\\$x`) or switch to single quotes so yq receives the literal `$x`","Compute derived values after the binding, e.g. `.a as $x | $x + 1`, not `as ($x + 1)`","Check lexer parsing: ensure the variable token wasn't split by spaces or special characters"],"exampleFix":"// before\nyq '.a as x | .b + x' file.yaml\n// after\nyq '.a as $x | .b + $x' file.yaml\n","handlingStrategy":"validation","validationCode":"// Ensure RHS of 'as' is a bare $variable\nre := regexp.MustCompile(`\\bas\\s+\\$\\w+\\s*\\|`)\nif strings.Contains(expr, \" as \") && !re.MatchString(expr) {\n\treturn fmt.Errorf(\"RHS of 'as' must be a $variable\")\n}","typeGuard":"func isVarRef(tok string) bool { return regexp.MustCompile(`^\\$\\w+$`).MatchString(tok) }","tryCatchPattern":"out, err := yqEval(expr, doc)\nif err != nil && strings.Contains(err.Error(), \"must be a variable name\") {\n\t// rewrite binding as plain $var then compute after pipe\n}","preventionTips":["Use single quotes around expressions in bash so $x is not expanded","Only place plain $variables after `as`; compute derived values after the pipe","Grep CI configs for `as ` expressions lacking $ variables"],"tags":["yaml","variables","expression-syntax"],"backgroundTag":"expression-syntax-invalid","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"}