mikefarah/yq · error

bad expression - probably missing close bracket on %v

Error message

bad expression - probably missing close bracket on %v

What it means

ConvertToPostfix ends with a non-empty operator stack, meaning an operator or open token never got reduced by a closer. The error names the last remaining token ('probably missing close bracket on %v') to point at the offending construct.

Source

Thrown at pkg/yqlib/expression_postfix.go:133

			for len(opStack) > 0 &&
				opStack[len(opStack)-1].TokenType == operationToken &&
				opStack[len(opStack)-1].Operation.OperationType.Precedence > currentPrecedence {
				opStack, result = popOpToResult(opStack, result)
			}
			// add this operator to the opStack
			opStack = append(opStack, currentToken)
			log.Debugf("put %v onto the opstack", currentToken.toString(true))
		}
	}

	log.Debugf("opstackLen: %v", len(opStack))
	if len(opStack) > 0 {
		log.Debugf("opstack:")
		for _, token := range opStack {
			log.Debugf("- %v", token.toString(true))
		}

		return nil, fmt.Errorf("bad expression - probably missing close bracket on %v", opStack[len(opStack)-1].toString(false))
	}

	if log.IsEnabledFor(slog.LevelDebug) {
		log.Debugf("PostFix Result:")
		for _, currentToken := range result {
			log.Debugf("> %v", currentToken.toString())
		}
	}

	return result, nil
}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Read the reported token in the message and add its matching closer
  2. Rewrite the expression more simply / split with pipes to reduce nesting
  3. Validate bracket balance programmatically before invoking yq in scripts

Example fix

// before
yq '.a[.b(select(.c' file.yml
// after
yq '.a[.b(select(.c))]' file.yml
Defensive patterns

Strategy: validation

Validate before calling

for _, pair := range [][2]string{"[]", "{}", "()"} {
    if strings.Count(expr, pair[:1]) != strings.Count(expr, pair[1:]) {
        return fmt.Errorf("unbalanced %s in %q", pair, expr)
    }
}

Try / catch

out, err := runYq(expr, file)
if err != nil && strings.Contains(err.Error(), "probably missing close bracket") {
    // the message names the leftover token; surface it to the user
    return fmt.Errorf("unbalanced expression %q: %w", expr, err)
}

Prevention

When it happens

Trigger: Expressions with an unbalanced structural token that slips past specific checks, e.g. `yq '.[]'` variants with missing closers in complex nesting like `yq '.a[.b(' — the leftover token is reported in the message.

Common situations: Deeply nested collect/select expressions in automation scripts, expressions built by string concatenation where a closer was conditional and skipped.

Related errors


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