Skip to content

Detect unbalanced outer braces in brace expansion - #20740

Open
ragib-mrb wants to merge 1 commit into
tailwindlabs:mainfrom
ragib-mrb:fix/brace-expansion-unbalanced-nested
Open

ragib-mrb wants to merge 1 commit into
tailwindlabs:mainfrom
ragib-mrb:fix/brace-expansion-unbalanced-nested

Conversation

@ragib-mrb

Copy link
Copy Markdown
Contributor

Problem

In packages/tailwindcss/src/utils/brace-expansion.ts, expand() initializes endIndex to rest.lastIndexOf('}') before scanning for the matching closing brace at depth === 0:

let depth = 0
let endIndex = rest.lastIndexOf('}')

When an outer brace is unclosed while a nested brace is closed (e.g. a{b,{c}), the loop ends with depth > 0 without finding a matching }, but endIndex retains the index of the inner } instead of -1. As a result, expand() misses the unbalanced outer group and recurses into the sliced inner content, throwing a truncated error (The pattern \{c` is not balanced.) instead of reporting the unbalanced pattern (The pattern `a{b,{c}` is not balanced.`).

Fix

  • Initialize endIndex = -1 (which also avoids an extra lastIndexOf('}') scan on every call) so endIndex === -1 reliably detects when no matching closing brace exists.
  • Add a regression test in packages/tailwindcss/src/utils/brace-expansion.test.ts.

Verification

  • .\node_modules\.bin\vitest.cmd run packages/tailwindcss/src/utils/brace-expansion.test.ts (22/22 passing; fails before fix)
  • Prettier formatted and verified

@ragib-mrb
ragib-mrb requested a review from a team as a code owner November 21, 2026 11:16
@RobinMalfait
RobinMalfait self-requested a review November 25, 2026 15:22
@RobinMalfait RobinMalfait self-assigned this Nov 25, 2026
@RobinMalfait
RobinMalfait force-pushed the fix/brace-expansion-unbalanced-nested branch from 7c75b42 to 603f02b Compare November 25, 2026 15:24
@RobinMalfait

Copy link
Copy Markdown
Contributor

I rebased this PR on the latest main and force pushed it.

Sent by an AI assistant on behalf of @RobinMalfait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants