{"record":{"id":"aa1d0202c6d4e762","repo":"mikefarah/yq","slug":"bad-expression-could-not-find-matching","errorCode":null,"errorMessage":"bad expression, could not find matching `]`","messagePattern":"bad expression, could not find matching `\\]`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/expression_postfix.go","lineNumber":30,"sourceCode":"\ntype expressionPostFixerImpl struct {\n}\n\nfunc newExpressionPostFixer() expressionPostFixer {\n\treturn &expressionPostFixerImpl{}\n}\n\nfunc popOpToResult(opStack []*token, result []*Operation) ([]*token, []*Operation) {\n\tvar newOp *token\n\topStack, newOp = opStack[0:len(opStack)-1], opStack[len(opStack)-1]\n\tlog.Debugf(\"popped %v from opstack to results\", newOp.toString(true))\n\treturn opStack, append(result, newOp.Operation)\n}\n\nfunc validateNoOpenTokens(token *token) error {\n\tswitch token.TokenType {\n\tcase openCollect:\n\t\treturn fmt.Errorf((\"bad expression, could not find matching `]`\"))\n\tcase openCollectObject:\n\t\treturn fmt.Errorf((\"bad expression, could not find matching `}`\"))\n\tcase openBracket:\n\t\treturn fmt.Errorf((\"bad expression, could not find matching `)`\"))\n\t}\n\treturn nil\n}\n\nfunc (p *expressionPostFixerImpl) ConvertToPostfix(infixTokens []*token) ([]*Operation, error) {\n\tvar result []*Operation\n\t// surround the whole thing with brackets\n\tvar opStack = []*token{{TokenType: openBracket}}\n\tvar tokens = append(infixTokens, &token{TokenType: closeBracket})\n\n\tfor _, currentToken := range tokens {\n\t\tlog.Debugf(\"postfix processing currentToken %v\", currentToken.toString(true))\n\t\tswitch currentToken.TokenType {\n\t\tcase openBracket, openCollect, openCollectObject:","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/expression_postfix.go#L12-L48","documentation":"During infix-to-postfix conversion, validateNoOpenTokens detects an unclosed openCollect token ('[') at the end of the token stream. This means a collect/array bracket was opened but never closed, so the expression cannot be evaluated.","triggerScenarios":"Running `yq '[.a' file.yml` or any expression with `[` without matching `]`, e.g. `yq '.a[.b'` or an incomplete map/collect in a select.","commonSituations":"Hand-written collect expressions in CI scripts, truncated expressions from shell quoting, forgetting to close a multi-line expression.","solutions":["Add the matching `]` to close the collect (e.g. `[.a, .b]`)","Quote the expression properly so the shell doesn't eat brackets (single quotes)","If using multi-line expressions, verify the final line closes all brackets"],"exampleFix":"// before\nyq '[.a' file.yml\n// after\nyq '[.a]' file.yml","handlingStrategy":"validation","validationCode":"// naive balance check before invoking yq\ndepth := 0\nfor _, r := range expr {\n    switch r {\n    case '[': depth++\n    case ']': depth--\n    }\n    if depth < 0 { break }\n}\nif depth != 0 {\n    return fmt.Errorf(\"unbalanced [ ] in expression %q\", expr)\n}","typeGuard":null,"tryCatchPattern":"out, err := runYq(expr, file)\nif err != nil && strings.Contains(err.Error(), \"could not find matching `]`\") {\n    return fmt.Errorf(\"unclosed [ in expression %q\", expr)\n}","preventionTips":["Single-quote expressions so shell does not strip brackets","Close collects immediately after opening: `[` ... `]` before filling in the middle","Add a bracket-balance lint step for scripts that build expressions dynamically"],"tags":["expression","bracket","syntax"],"backgroundTag":"unclosed-bracket-expression","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"}