Skip to content

Canonicalization: don't treat different @scope variants as the same - #20715

Merged
RobinMalfait merged 7 commits into
tailwindlabs:mainfrom
benface:benface/canonicalize-scope-variant-signatures
Nov 17, 2026
Merged

RobinMalfait merged 7 commits into
tailwindlabs:mainfrom
benface:benface/canonicalize-scope-variant-signatures

Conversation

@benface

@benface benface commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #20714

Summary

Since #20369, variants that generate @scope rules wrap them in context nodes. The canonicalizer computes a variant's signature by printing the @apply result with toCss, which prints context nodes as an empty string. So every @scope variant had the signature .x {}, the same as a variant that generates nothing, and canonicalization replaced it with whichever of those variants came first (e.g. [@scope_(.card)]:flex → in-blue:flex, where in-blue scopes to [data-theme='blue']).

This PR unwraps the context nodes before computing the signature, so it includes the @scope rule again, as in v4.3.3.

Test plan

  • Added a regression test: [@scope_(.card)]:flex stays as is next to an unrelated @scope custom variant, and the equivalent arbitrary variant still canonicalizes to that custom variant. The first assertion fails without this PR.
  • vitest run --project tailwindcss passes.
  • In our design system, which has many @scope variants, @scope-has-nested-focus-visible/tab-set-panel:outline canonicalized to @scope-not-has-first:outline before this PR, and stays as is after.

🤖 Generated with Claude Code

@RobinMalfait
RobinMalfait self-requested a review November 16, 2026 17:35
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
@RobinMalfait
RobinMalfait force-pushed the benface/canonicalize-scope-variant-signatures branch from a738400 to ea49ac9 Compare November 16, 2026 17:37
@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.

@RobinMalfait
RobinMalfait marked this pull request as ready for review November 16, 2026 17:53
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 16, 2026 17:53
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; no new actionable issues were found.

Reviews (8) · Last reviewed commit: "Treat CRLF after a hex escape in at-rule..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Variant signature generation now unwraps context nodes before serialization, so generated @scope rules are included in signatures. At-rule parameter normalization preserves whitespace that distinguishes descendant selectors from compound selectors. Regression tests cover scope matching, selector spacing, escaped characters, and media-query canonicalization. A changelog entry documents the fixes.

Severity of issue fixed: Medium

Merge Risk: Minimal · previous review 4adbc

Canonicalization now keeps different @scope variants and descendant versus compound selectors distinct, and regression tests cover these cases. No concrete merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: canonicalization no longer treats different @scope variants as the same. The raw summary confirms the change unwraps context nodes so @scope rules contribute to …
Description check Passed The description states the problem (since #20369, context nodes print as empty strings, so every @scope variant gets the signature .x {}), the fix (unwrap context nodes before computing the si…
Linked Issues check Passed [#20714] requires that variants generating different @scope CSS are not canonicalized as equivalent. The whole-PR diff (merge base bbd48b6 to head 4adbce2) adds a context unwrap in `createVarian…
Out of Scope Changes check Passed Changed files in the whole-PR diff are packages/tailwindcss/src/canonicalize-candidates.ts, its test file, and .changelog/canonicalize-scope-variants.md. The context unwrap directly implements […
Full details: Title check

Explanation

The title names the main change: canonicalization no longer treats different @scope variants as the same. The raw summary confirms the change unwraps context nodes so @scope rules contribute to variant signatures, and the objectives describe the same fix for issue #20714. The title is specific and concise.

Full details: Description check

Explanation

The description states the problem (since #20369, context nodes print as empty strings, so every @scope variant gets the signature .x {}), the fix (unwrap context nodes before computing the signature), and the test plan. It links issue #20714. It matches the changeset and the objectives.

Full details: Linked Issues check

Explanation

[#20714] requires that variants generating different @scope CSS are not canonicalized as equivalent. The whole-PR diff (merge base bbd48b6 to head 4adbce2) adds a context unwrap in createVariantSignatureCache (packages/tailwindcss/src/canonicalize-candidates.ts). The unwrap runs before toCss, so @scope rules generated inside context nodes enter the variant signature. The PR adds a regression test. The test checks that [@scope_(.card)]:flex stays unchanged next to the in-blue custom variant, and that the equivalent arbitrary variant still canonicalizes to in-blue:flex. The current diff also replaces blanket space removal in at-rule params with normalizeAtRuleParamsWhitespace. This keeps descendant combinators in @scope and @supports selector(...) significant, so .foo .bar and .foo.bar no longer share a signature. The code supports the claimed behavior. The author-reported vitest run --project tailwindcss result was not re-executed in this review. Incremental changes since the previous reviewed heads were assessed through the whole-PR diff, which the previous assessment also used.

Full details: Out of Scope Changes check

Explanation

Changed files in the whole-PR diff are packages/tailwindcss/src/canonicalize-candidates.ts, its test file, and .changelog/canonicalize-scope-variants.md. The context unwrap directly implements [#20714]. The normalizeAtRuleParamsWhitespace change affects signature generation for all at-rule variants. It addresses the same defect class as the issue: distinct variants compare as equal. Its descendant-combinator handling is tied to @scope and selector-bearing at-rules. The hex-escape handling in the commit messages is tied to signature mismatches of equivalent selectors, and it has tests. The @media/@container colon rule, which makes (pointer :fine) and (pointer:fine) canonicalize alike, is a related normalizer change with its own test and changelog entry. Residual risk: the normalizer affects every at-rule variant. Its effect on unrelated at-rule canonicalization is verified only through the added tests.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4c8753c0-e3c0-4ff3-a322-a7913d6a7467
Commits

Reviewing files that changed from the base of the PR and between b0b3a7c and 4adbce2.

Files selected for processing: 2

  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Major · Preserve descendant combinators in @scope parameters. · canonicalize-candidates.ts:3421-3422

packages/tailwindcss/src/canonicalize-candidates.ts:3421-3422
Major · Functional Correctness · Quick win

Preserve descendant combinators in @scope parameters.

The new unwrapping path exposes custom @scope rules to this whitespace removal. A custom variant using @scope (.card.title) now shares a signature with [@scope_(.card_.title)]:flex, because .card .title becomes .card.title. Canonicalization can therefore replace the arbitrary variant with a custom variant that has a different scope.

Preserve meaningful whitespace in @scope parameters. Add a regression test that distinguishes these two selectors.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 00098583-352f-4b1e-b8eb-638a105c762c
Commits

Reviewing files that changed from the base of the PR and between bbd48b6 and ea49ac9.

Files selected for processing: 3

  • .changelog/canonicalize-scope-variants.md
  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4649ca88-cfa3-44be-9e3a-55b43714306c
Commits

Reviewing files that changed from the base of the PR and between ea49ac9 and e8b45e1.

Files selected for processing: 3

  • .changelog/canonicalize-scope-variants.md
  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts
Files skipped from review as they are similar to previous changes: 1

  • .changelog/canonicalize-scope-variants.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1931126b-eb65-40c9-ae09-147ad62fd69f
Commits

Reviewing files that changed from the base of the PR and between e8b45e1 and 6cd72eb.

Files selected for processing: 2

  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 16, 2026
A hex escape like `\2b` consumes one whitespace character after it. So
`.foo\2b  .bar` is a descendant selector, but `.foo\2b .bar` is the
compound `.foo\+.bar`. The whitespace normalization only consumed the
first character after the backslash, then collapsed both spaces into one,
so the two selectors got the same variant signature.

Reported by CodeRabbit on tailwindlabs#20715.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a9508e93-8efb-42aa-8230-7fe5d2e65ac1
Commits

Reviewing files that changed from the base of the PR and between 6cd72eb and b0b3a7c.

Files selected for processing: 2

  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 17, 2026
The whitespace that ends a hex escape is only needed when a hex digit or
another space follows it. Keeping it everywhere meant that
`.foo\2b>.bar` and `.foo\2b >.bar` got different variant signatures,
so `[@scope_(.foo\2b_>.bar)]:flex` was no longer replaced by an
equivalent custom variant.

Reported by Greptile on tailwindlabs#20715.
RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 17, 2026
CSS treats CRLF as one newline, so a hex escape consumes both
characters. The whitespace normalization consumed only the CR, and the
LF became a descendant combinator. So a plugin variant like
`@scope (.foo\2b\r\n.bar) to (.limit)` (a compound selector) got the same
signature as `[@scope_(.foo\2b__.bar)_to_(.limit)]:flex` (a descendant
selector).

The stylesheet parser already normalizes CRLF, so only plugin variants
keep it in their params.

Reported by CodeRabbit on tailwindlabs#20715.
@RobinMalfait
RobinMalfait force-pushed the benface/canonicalize-scope-variant-signatures branch from 4adbce2 to 395d4f5 Compare November 17, 2026 12:10
RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 17, 2026
A hex escape like `\2b` consumes one whitespace character after it. So
`.foo\2b  .bar` is a descendant selector, but `.foo\2b .bar` is the
compound `.foo\+.bar`. The whitespace normalization only consumed the
first character after the backslash, then collapsed both spaces into one,
so the two selectors got the same variant signature.

Reported by CodeRabbit on tailwindlabs#20715.
RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 17, 2026
The whitespace that ends a hex escape is only needed when a hex digit or
another space follows it. Keeping it everywhere meant that
`.foo\2b>.bar` and `.foo\2b >.bar` got different variant signatures,
so `[@scope_(.foo\2b_>.bar)]:flex` was no longer replaced by an
equivalent custom variant.

Reported by Greptile on tailwindlabs#20715.
RobinMalfait added a commit to benface/tailwindcss that referenced this pull request Nov 17, 2026
CSS treats CRLF as one newline, so a hex escape consumes both
characters. The whitespace normalization consumed only the CR, and the
LF became a descendant combinator. So a plugin variant like
`@scope (.foo\2b\r\n.bar) to (.limit)` (a compound selector) got the same
signature as `[@scope_(.foo\2b__.bar)_to_(.limit)]:flex` (a descendant
selector).

The stylesheet parser already normalizes CRLF, so only plugin variants
keep it in their params.

Reported by CodeRabbit on tailwindlabs#20715.
@RobinMalfait
RobinMalfait enabled auto-merge (squash) November 17, 2026 12:11

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Good catch that different @scope variants could be treated as the same during canonicalization.

Sent by an AI assistant on behalf of @RobinMalfait.

benface and others added 6 commits November 17, 2026 13:17
Variants that generate `@scope` rules wrap them in `context` nodes since tailwindlabs#20369, and `toCss` prints `context` nodes as an empty string. So every `@scope` variant had the same signature, and canonicalization rewrote them to whichever `@scope` variant came first. Unwrap the `context` nodes before computing the signature.

Fixes tailwindlabs#20714

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The variant signature removed every space from at-rule params, so
`@scope (.foo .bar)` and `@scope (.foo.bar)` got the same signature, and
`[@scope_(.foo_.bar)]:flex` was replaced with a variant for `.foo.bar`.
`@supports selector(…)` had the same problem.

Now only whitespace next to delimiters is removed (e.g. `(width >= 64rem)`
→ `(width>=64rem)`), and other whitespace is collapsed to a single space.
This happens in a single pass that keeps strings and escapes as-is.
In `@scope (.foo\+ .bar)`, the `+` is part of the class name and the space
is a descendant combinator. The whitespace normalization treated the
escaped `+` as a delimiter and dropped the space, so the signature matched
`@scope (.foo\+.bar)`.
Whitespace before a `:` is kept in at-rule params, because it can be a
descendant combinator in a selector (`.foo :hover`). `@media` and
`@container` params can't contain selectors, so `(pointer :fine)` and
`(pointer:fine)` are the same there. This lets `[@media_(pointer_:fine)]:flex`
canonicalize to `pointer-fine:flex` again.
A hex escape like `\2b` consumes one whitespace character after it. So
`.foo\2b  .bar` is a descendant selector, but `.foo\2b .bar` is the
compound `.foo\+.bar`. The whitespace normalization only consumed the
first character after the backslash, then collapsed both spaces into one,
so the two selectors got the same variant signature.

Reported by CodeRabbit on tailwindlabs#20715.
The whitespace that ends a hex escape is only needed when a hex digit or
another space follows it. Keeping it everywhere meant that
`.foo\2b>.bar` and `.foo\2b >.bar` got different variant signatures,
so `[@scope_(.foo\2b_>.bar)]:flex` was no longer replaced by an
equivalent custom variant.

Reported by Greptile on tailwindlabs#20715.
CSS treats CRLF as one newline, so a hex escape consumes both
characters. The whitespace normalization consumed only the CR, and the
LF became a descendant combinator. So a plugin variant like
`@scope (.foo\2b\r\n.bar) to (.limit)` (a compound selector) got the same
signature as `[@scope_(.foo\2b__.bar)_to_(.limit)]:flex` (a descendant
selector).

The stylesheet parser already normalizes CRLF, so only plugin variants
keep it in their params.

Reported by CodeRabbit on tailwindlabs#20715.
@RobinMalfait
RobinMalfait force-pushed the benface/canonicalize-scope-variant-signatures branch from 395d4f5 to 80332f4 Compare November 17, 2026 12:18
@RobinMalfait
RobinMalfait merged commit 9db5a3f into tailwindlabs:main Nov 17, 2026
13 checks passed
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.

Canonicalization treats different @scope variants as the same since v4.3.4

2 participants