From 3275da54adb548ee940ce898909de8669221e430 Mon Sep 17 00:00:00 2001 From: Companion <3773910+Companion@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:38:40 +0000 Subject: [PATCH 1/6] Canonicalization: collapse static utilities regardless of earlier candidates --- .../canonicalize-collapse-static-utilities.md | 3 +++ .../src/canonicalize-candidates.test.ts | 26 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 6 +++++ 3 files changed, 35 insertions(+) create mode 100644 .changelog/canonicalize-collapse-static-utilities.md diff --git a/.changelog/canonicalize-collapse-static-utilities.md b/.changelog/canonicalize-collapse-static-utilities.md new file mode 100644 index 000000000000..b6f411de5022 --- /dev/null +++ b/.changelog/canonicalize-collapse-static-utilities.md @@ -0,0 +1,3 @@ +### Fixed + +- Canonicalization: collapse static utilities without depending on the candidates that were canonicalized before (e.g. `overflow-x-hidden overflow-y-hidden` → `overflow-hidden`) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 9bdbe825762d..3682ca274a4a 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1804,6 +1804,32 @@ describe('regressions', () => { }, ) + // https://github.com/tailwindlabs/tailwindcss/issues/20602 + test.each([ + [['overflow-x-hidden', 'overflow-y-hidden'], ['overflow-hidden']], + [['overflow-x-clip', 'overflow-y-clip'], ['overflow-clip']], + [['overscroll-x-none', 'overscroll-y-none'], ['overscroll-none']], + ])( + 'collapsing static utilities does not depend on previously canonicalized candidates', + async (candidates, expected) => { + // A new design system, so that no other candidates were canonicalized before + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + `, + { base: __dirname }, + ) + + let options: CanonicalizeOptions = { + collapse: true, + logicalToPhysical: true, + rem: 16, + } + + expect(designSystem.canonicalizeCandidates(candidates, options)).toEqual(expected) + }, + ) + // https://github.com/tailwindlabs/tailwindcss/issues/20051 test( 'does not crash when plugin matchComponents rejects speculative values during collapse', diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index c14960c484d0..2f10162cfc76 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -283,6 +283,12 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st let themeVariables = designSystem.storage[UTILITY_THEME_VARIABLES_KEY].get(signatureOptions) let dynamicUtilities = designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions) + // The static utilities are indexed while pre-computing the utilities. Make + // sure that happened, otherwise the result depends on whether a previous + // candidate needed them. E.g.: `overflow-x-hidden overflow-y-hidden` → + // `overflow-hidden` + designSystem.storage[PRE_COMPUTED_UTILITIES_KEY].get(signatureOptions) + // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem` // // NOTE: Currently assuming we are dealing with static utilities only. This From 210be7cf0d2ad38375f7ddae6bd2499c0ee34404 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 24 Nov 2026 12:06:02 +0100 Subject: [PATCH 2/6] Skip collapsing groups with a single candidate A group with one candidate can never collapse, but it still pre-computed all utilities (~750 ms with the default theme) to fill the static index. E.g.: the first `flex hover:underline` took ~760 ms instead of ~6 ms. --- packages/tailwindcss/src/canonicalize-candidates.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 2f10162cfc76..d409db040c4d 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -276,6 +276,10 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st return Array.from(result) function collapseGroup(candidates: string[]) { + // A single candidate can't be collapsed with anything. Bail early, so we + // don't pre-compute all utilities for nothing. E.g.: `flex hover:underline` + if (candidates.length < 2) return candidates + let signatureOptions = options.signatureOptions let computeUtilitiesPropertiesLookup = designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions) From 15d7d548edf63f6f69d6c123cc95a48d913930ed Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 24 Nov 2026 12:44:19 +0100 Subject: [PATCH 3/6] Pre-compute only the static utilities to collapse a group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-computing all utilities to collapse a group made the first collapse of a class list with only static utilities take ~800 ms with the default theme, e.g. `flex items-center`, which took ~2 ms before. A group of static utilities only needs the static utilities in the index, e.g. `overflow-x-hidden overflow-y-hidden` → `overflow-hidden`. So compute the properties of the 889 static utilities instead, once per design system, which adds them to the index. This takes ~4 ms (~15 ms with a cold JIT). With the default theme, no group of static utilities collapses into a utility with a value. Candidates with a value, like `p-4`, already pre-compute all utilities while they are canonicalized, so groups with them work as before. --- .../src/canonicalize-candidates.test.ts | 41 +++++++++++++++++ .../src/canonicalize-candidates.ts | 44 ++++++++++++++++--- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 3682ca274a4a..6a4d639a1146 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1809,6 +1809,8 @@ describe('regressions', () => { [['overflow-x-hidden', 'overflow-y-hidden'], ['overflow-hidden']], [['overflow-x-clip', 'overflow-y-clip'], ['overflow-clip']], [['overscroll-x-none', 'overscroll-y-none'], ['overscroll-none']], + [['overflow-x-auto', 'overflow-y-auto'], ['overflow-auto']], + [['hover:overflow-x-hidden', 'hover:overflow-y-hidden'], ['hover:overflow-hidden']], ])( 'collapsing static utilities does not depend on previously canonicalized candidates', async (candidates, expected) => { @@ -1827,6 +1829,45 @@ describe('regressions', () => { } expect(designSystem.canonicalizeCandidates(candidates, options)).toEqual(expected) + + // A candidate with a value pre-computes all utilities, which must not + // change the result + designSystem.canonicalizeCandidates(['p-4'], options) + expect(designSystem.canonicalizeCandidates(candidates, options)).toEqual(expected) + }, + ) + + test.each([ + ['', ['overflow-x-hidden', 'overflow-y-hidden'], ['overflow-hidden']], + ['tw', ['tw:overflow-x-hidden', 'tw:overflow-y-hidden'], ['tw:overflow-hidden']], + ])( + 'collapsing static utilities does not pre-compute all utilities (prefix: "%s")', + async (prefix, candidates, expected) => { + let designSystem = await __unstable__loadDesignSystem( + prefix + ? css` + @theme prefix(${prefix}) { + } + @tailwind utilities; + ` + : css` + @tailwind utilities; + `, + { base: __dirname }, + ) + + // Pre-computing all utilities starts from the class list, and takes + // ~800ms with the default theme + let getClassList = vi.spyOn(designSystem, 'getClassList') + + let options: CanonicalizeOptions = { + collapse: true, + logicalToPhysical: true, + rem: 16, + } + + expect(designSystem.canonicalizeCandidates(candidates, options)).toEqual(expected) + expect(getClassList).not.toHaveBeenCalled() }, ) diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index d409db040c4d..31e7bdb437e7 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -102,6 +102,7 @@ interface DesignSystem extends BaseDesignSystem { SignatureOptions, DefaultMap>> > + [PRE_COMPUTED_STATIC_UTILITIES_KEY]: DefaultMap> [UTILITY_PROPERTIES_KEY]: DefaultMap< SignatureOptions, DefaultMap>> @@ -133,6 +134,8 @@ export function prepareDesignSystemStorage( designSystem.storage[UTILITY_THEME_VARIABLES_KEY] ??= createUtilityThemeVariablesCache(designSystem) designSystem.storage[STATIC_UTILITIES_KEY] ??= createStaticUtilitiesCache() + designSystem.storage[PRE_COMPUTED_STATIC_UTILITIES_KEY] ??= + createPreComputedStaticUtilitiesCache(designSystem) designSystem.storage[UTILITY_PROPERTIES_KEY] ??= createUtilityPropertiesCache(designSystem) designSystem.storage[DYNAMIC_UTILITIES_KEY] ??= createDynamicUtilitiesCache(designSystem) designSystem.storage[PRE_COMPUTED_UTILITIES_KEY] ??= createPreComputedUtilitiesCache(designSystem) @@ -276,8 +279,8 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st return Array.from(result) function collapseGroup(candidates: string[]) { - // A single candidate can't be collapsed with anything. Bail early, so we - // don't pre-compute all utilities for nothing. E.g.: `flex hover:underline` + // A single candidate can't be collapsed with anything, so there is nothing + // to do. E.g.: `flex hover:underline` if (candidates.length < 2) return candidates let signatureOptions = options.signatureOptions @@ -287,11 +290,13 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st let themeVariables = designSystem.storage[UTILITY_THEME_VARIABLES_KEY].get(signatureOptions) let dynamicUtilities = designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions) - // The static utilities are indexed while pre-computing the utilities. Make - // sure that happened, otherwise the result depends on whether a previous - // candidate needed them. E.g.: `overflow-x-hidden overflow-y-hidden` → - // `overflow-hidden` - designSystem.storage[PRE_COMPUTED_UTILITIES_KEY].get(signatureOptions) + // A utility is only added to the index of static utilities once its + // properties are computed, e.g. while pre-computing all utilities. That + // only happens for candidates with a value like `p-4`, so make sure that + // the static utilities are in there. Otherwise the result depends on the + // candidates that were canonicalized before. E.g.: `overflow-x-hidden + // overflow-y-hidden` → `overflow-hidden` + designSystem.storage[PRE_COMPUTED_STATIC_UTILITIES_KEY].get(signatureOptions) // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem` // @@ -3258,6 +3263,31 @@ function createStaticUtilitiesCache(): DesignSystem['storage'][typeof STATIC_UTI }) } +// Compute the properties of all static utilities, which adds them to the index +// of static utilities. This takes a few milliseconds, while pre-computing all +// utilities takes almost a second with the default theme. +const PRE_COMPUTED_STATIC_UTILITIES_KEY = Symbol() +function createPreComputedStaticUtilitiesCache( + designSystem: DesignSystem, +): DesignSystem['storage'][typeof PRE_COMPUTED_STATIC_UTILITIES_KEY] { + return new DefaultMap((options: SignatureOptions) => { + let computeUtilitiesPropertiesLookup = designSystem.storage[UTILITY_PROPERTIES_KEY].get(options) + let prefix = designSystem.theme.prefix ? `${designSystem.theme.prefix}:` : '' + + let utilities = new Set() + for (let utility of designSystem.utilities.keys('static')) { + // Use the same name as the candidates that we collapse, which have the + // prefix. E.g.: `tw:flex` + utility = `${prefix}${utility}` + if (computeUtilitiesPropertiesLookup.get(utility).size > 0) { + utilities.add(utility) + } + } + + return utilities + }) +} + const UTILITY_PROPERTIES_KEY = Symbol() function createUtilityPropertiesCache( designSystem: DesignSystem, From 884a12253652d1600f527f5e1fc08ceed3da803a Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 25 Nov 2026 13:16:25 +0100 Subject: [PATCH 4/6] Skip the static utilities pass when it can't change the result MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Collapsing a group of static utilities first computes the properties of all static utilities, so that the result does not depend on the candidates that were canonicalized before. That costs ~14ms on a fresh design system, but it isn't always needed: - A group with fewer than 2 utilities can't be collapsed, e.g. `group flex`, where `group` isn't a utility. Return early, before computing the static utilities (14ms → 1ms). - When all utilities were pre-computed already (for a candidate with a value like `p-4`), the static utilities are in the index already. With a prefix, we computed all of them a second time under their prefixed name (3-6ms → 0ms). The static pass now also skips the static utilities that the class list skips, like the deprecated `bg-gradient-to-r`. These are not computed when pre-computing all utilities either, so the index is the same in both orders, and a collapse never picks a deprecated name. --- .../src/canonicalize-candidates.test.ts | 60 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 35 +++++++---- 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 6a4d639a1146..69cee6fba92b 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1871,6 +1871,66 @@ describe('regressions', () => { }, ) + test('collapsing a group with a single utility does not compute the static utilities', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + `, + { base: __dirname }, + ) + + let options: CanonicalizeOptions = { + collapse: true, + logicalToPhysical: true, + rem: 16, + } + + using keys = vi.spyOn(designSystem.utilities, 'keys') + + // `group` is not a utility, so `flex` can't be collapsed with anything + expect(designSystem.canonicalizeCandidates(['group', 'flex'], options)).toEqual([ + 'group', + 'flex', + ]) + expect(keys).not.toHaveBeenCalledWith('static') + }) + + test.each([ + ['', ['p-4'], ['overflow-x-hidden', 'overflow-y-hidden'], ['overflow-hidden']], + ['tw', ['tw:p-4'], ['tw:overflow-x-hidden', 'tw:overflow-y-hidden'], ['tw:overflow-hidden']], + ])( + 'collapsing static utilities after pre-computing all utilities does not compute the static utilities again (prefix: "%s")', + async (prefix, before, candidates, expected) => { + let designSystem = await __unstable__loadDesignSystem( + prefix + ? css` + @theme prefix(${prefix}) { + } + @tailwind utilities; + ` + : css` + @tailwind utilities; + `, + { base: __dirname }, + ) + + let options: CanonicalizeOptions = { + collapse: true, + logicalToPhysical: true, + rem: 16, + } + + // A candidate with a value pre-computes all utilities, which includes the + // static utilities + designSystem.canonicalizeCandidates(before, options) + + using keys = vi.spyOn(designSystem.utilities, 'keys') + + expect(designSystem.canonicalizeCandidates(candidates, options)).toEqual(expected) + expect(keys).not.toHaveBeenCalledWith('static') + }, + ) + // https://github.com/tailwindlabs/tailwindcss/issues/20051 test( 'does not crash when plugin matchComponents rejects speculative values during collapse', diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 31e7bdb437e7..07fab1147985 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -290,14 +290,6 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st let themeVariables = designSystem.storage[UTILITY_THEME_VARIABLES_KEY].get(signatureOptions) let dynamicUtilities = designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions) - // A utility is only added to the index of static utilities once its - // properties are computed, e.g. while pre-computing all utilities. That - // only happens for candidates with a value like `p-4`, so make sure that - // the static utilities are in there. Otherwise the result depends on the - // candidates that were canonicalized before. E.g.: `overflow-x-hidden - // overflow-y-hidden` → `overflow-hidden` - designSystem.storage[PRE_COMPUTED_STATIC_UTILITIES_KEY].get(signatureOptions) - // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem` // // NOTE: Currently assuming we are dealing with static utilities only. This @@ -306,6 +298,22 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st computeUtilitiesPropertiesLookup.get(candidate), ) + // A candidate without properties, like a class that isn't a utility, can't + // be collapsed with anything. E.g.: `group flex` + if (candidatePropertiesValues.filter((propertyValues) => propertyValues.size > 0).length < 2) { + return candidates + } + + // A utility is only added to the index of static utilities once its + // properties are computed. Pre-computing all utilities does that, but it + // only happens for candidates with a value like `p-4`. So make sure that + // the static utilities are in there, otherwise the result depends on the + // candidates that were canonicalized before. E.g.: `overflow-x-hidden + // overflow-y-hidden` → `overflow-hidden` + if (!designSystem.storage[PRE_COMPUTED_UTILITIES_KEY].has(signatureOptions)) { + designSystem.storage[PRE_COMPUTED_STATIC_UTILITIES_KEY].get(signatureOptions) + } + // Hard-coded optimization: if any candidate sets `line-height` and another // candidate sets `font-size`, we pre-compute the `text-*` utilities with // this line-height to try and collapse to those combined values. @@ -3263,9 +3271,10 @@ function createStaticUtilitiesCache(): DesignSystem['storage'][typeof STATIC_UTI }) } -// Compute the properties of all static utilities, which adds them to the index -// of static utilities. This takes a few milliseconds, while pre-computing all -// utilities takes almost a second with the default theme. +// Compute the properties of the static utilities, which adds them to the index +// of static utilities. These are the static utilities that pre-computing all +// utilities computes as well, but this takes a few milliseconds, while +// pre-computing all utilities takes almost a second with the default theme. const PRE_COMPUTED_STATIC_UTILITIES_KEY = Symbol() function createPreComputedStaticUtilitiesCache( designSystem: DesignSystem, @@ -3276,6 +3285,10 @@ function createPreComputedStaticUtilitiesCache( let utilities = new Set() for (let utility of designSystem.utilities.keys('static')) { + // Skip the utilities that the class list skips, like deprecated ones. + // E.g.: `bg-gradient-to-r` + if (designSystem.utilities.getCompletions(utility).length === 0) continue + // Use the same name as the candidates that we collapse, which have the // prefix. E.g.: `tw:flex` utility = `${prefix}${utility}` From 1fd938a61d5ef4804de87545bf578c7ee65bfb3f Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 25 Nov 2026 16:04:33 +0100 Subject: [PATCH 5/6] Match the prefix with its `:` when canonicalizing utilities A prefix like `o` is also the start of `overflow-hidden`, so the check `startsWith(prefix)` skipped adding the prefix. Pre-computing all utilities then indexed `overflow-hidden` instead of `o:overflow-hidden`, and a later `o:overflow-x-hidden o:overflow-y-hidden` didn't collapse. --- packages/tailwindcss/src/canonicalize-candidates.test.ts | 2 ++ packages/tailwindcss/src/canonicalize-candidates.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 69cee6fba92b..416dff9f5ec7 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1898,6 +1898,8 @@ describe('regressions', () => { test.each([ ['', ['p-4'], ['overflow-x-hidden', 'overflow-y-hidden'], ['overflow-hidden']], ['tw', ['tw:p-4'], ['tw:overflow-x-hidden', 'tw:overflow-y-hidden'], ['tw:overflow-hidden']], + // A prefix that is also the start of a utility name, like `o` in `overflow-hidden` + ['o', ['o:p-4'], ['o:overflow-x-hidden', 'o:overflow-y-hidden'], ['o:overflow-hidden']], ])( 'collapsing static utilities after pre-computing all utilities does not compute the static utilities again (prefix: "%s")', async (prefix, before, candidates, expected) => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 07fab1147985..0d040a753d4e 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -2946,7 +2946,7 @@ function compileUtilitySignatureAst( ) { // Ensure the prefix is added to the utility if it is not already present. utility = - designSystem.theme.prefix && !utility.startsWith(designSystem.theme.prefix) + designSystem.theme.prefix && !utility.startsWith(`${designSystem.theme.prefix}:`) ? `${designSystem.theme.prefix}:${utility}` : utility @@ -3309,7 +3309,7 @@ function createUtilityPropertiesCache( return new DefaultMap((className) => { let localPropertyValueLookup = new DefaultMap((_property) => new Set()) - if (designSystem.theme.prefix && !className.startsWith(designSystem.theme.prefix)) { + if (designSystem.theme.prefix && !className.startsWith(`${designSystem.theme.prefix}:`)) { className = `${designSystem.theme.prefix}:${className}` } let parsed = designSystem.parseCandidate(className) From 39b2601e9e7a4dd83e6ac8b941393978850ca7e5 Mon Sep 17 00:00:00 2001 From: Companion <3773910+Companion@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:28:17 +0000 Subject: [PATCH 6/6] Match the prefix with its `:` when canonicalizing variants --- .../canonicalize-collapse-static-utilities.md | 1 + .../src/canonicalize-candidates.test.ts | 20 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/.changelog/canonicalize-collapse-static-utilities.md b/.changelog/canonicalize-collapse-static-utilities.md index b6f411de5022..03ac251704ad 100644 --- a/.changelog/canonicalize-collapse-static-utilities.md +++ b/.changelog/canonicalize-collapse-static-utilities.md @@ -1,3 +1,4 @@ ### Fixed - Canonicalization: collapse static utilities without depending on the candidates that were canonicalized before (e.g. `overflow-x-hidden overflow-y-hidden` → `overflow-hidden`) +- Canonicalization: canonicalize utilities and variants whose name starts with the prefix (e.g. `o:outline-[2px]` → `o:outline-2` and `f:[&:focus]:flex` → `f:focus:flex`) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 416dff9f5ec7..707262f6ac5e 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -1933,6 +1933,26 @@ describe('regressions', () => { }, ) + test.each([ + ['tw', 'tw:[&:focus]:flex', 'tw:focus:flex'], + // A prefix that is also the start of a variant name, like `f` in `focus` + ['f', 'f:[&:focus]:flex', 'f:focus:flex'], + ])( + 'canonicalizing variants with a prefix (prefix: "%s")', + async (prefix, candidate, expected) => { + let designSystem = await __unstable__loadDesignSystem( + css` + @theme prefix(${prefix}) { + } + @tailwind utilities; + `, + { base: __dirname }, + ) + + expect(designSystem.canonicalizeCandidates([candidate], { rem: 16 })).toEqual([expected]) + }, + ) + // https://github.com/tailwindlabs/tailwindcss/issues/20051 test( 'does not crash when plugin matchComponents rejects speculative values during collapse', diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 0d040a753d4e..71cac5acb3a4 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3461,7 +3461,7 @@ function createVariantSignatureCache( try { // Ensure the prefix is added to the utility if it is not already present. variant = - designSystem.theme.prefix && !variant.startsWith(designSystem.theme.prefix) + designSystem.theme.prefix && !variant.startsWith(`${designSystem.theme.prefix}:`) ? `${designSystem.theme.prefix}:${variant}` : variant