Skip to content

Make createCompatConfig theme mappings safe to spread when a namespace returns a string - #20399

Merged
RobinMalfait merged 3 commits into
tailwindlabs:mainfrom
ibnlanre:fix/compat-config-string-theme-values
Aug 10, 2026
Merged

Make createCompatConfig theme mappings safe to spread when a namespace returns a string#20399
RobinMalfait merged 3 commits into
tailwindlabs:mainfrom
ibnlanre:fix/compat-config-string-theme-values

Conversation

@ibnlanre

@ibnlanre ibnlanre commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

When building the legacy config compat layer, namespace mappings like fontSizetext, boxShadowshadow, animationanimate, etc. spread the result of theme(namespace, {}) directly:

...(theme('text', {}) ?? {})

Some of these namespaces can return a string (e.g. theme('text', {})'1rem'). Spreading a string produces char-indexed keys ({ '0': '1', '1': 'r', ... }) instead of a { DEFAULT: ... } entry, silently corrupting the compat config output.

This PR adds a small spreadTheme helper that normalizes the return value before spreading:

  • string{ DEFAULT: value }
  • plain object → { ...value }
  • anything else → {}

and updates all namespace mappings to use it. Includes unit tests for the helper plus integration tests for the affected namespaces and the string-return regression cases.

@ibnlanre
ibnlanre requested a review from a team as a code owner August 8, 2026 13:42
@greptile-apps

greptile-apps Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (3): Last reviewed commit: "update changelog" | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 851ebebd-a118-4158-9aa6-c927f7b5eb6b

📥 Commits

Reviewing files that changed from the base of the PR and between 56a844b and 2f394f2.

📒 Files selected for processing (1)
  • CHANGELOG.md

Walkthrough

Compatibility theme lookups now preserve object results for namespace roots while unwrapping synthetic DEFAULT values for specific keys. Theme value resolution normalizes DEFAULT objects before processing other value forms. Tests cover lookup behavior and mappings for typography, shadows, animations, aspect ratios, radii, screens, tracking, leading, container widths, and transition defaults.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main compatibility-layer change for safely handling string-valued theme namespace results.
Description check ✅ Passed The description accurately explains the string-spreading issue, normalization behavior, affected mappings, and test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

Comment @coderabbitai help to get the list of available commands.

@RobinMalfait

Copy link
Copy Markdown
Member

What's a situation/use case where you ran into this kind of issue?

@ibnlanre

ibnlanre commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

What's a situation/use case where you ran into this kind of issue?

I ran into this while working on Jumi, a Tailwind CSS v4 animation plugin I maintain.

I was resolving a project's theme through the compat layer so Jumi could read the animation values, and for certain themes the output just looked… wrong. A value that should come back as { DEFAULT: 'spin 1s linear infinite' } was instead coming back as { '0': 's', '1': 'p', '2': 'i', … }. When a namespace only defines its DEFAULT token (e.g. @theme { --animate: … } with no --animate-* variants), theme('animate', {}) returns a plain string, and spreading a string gives you character-indexed keys instead of { DEFAULT: … }.

What made me think this could've gone unnoticed for a while is that it's completely silent. The config still resolves, so one would only notice when something (typically, a plugin) actually reads those theme values. I also had several cases of arbitrary class values returning as just "e" because of this bug.

…ject

In Tailwind CSS v3, each namespace inside the `theme` configuration has
to be an object. If you want to use `animate` instead of `animate-foo`,
then you have to define this as
```js
module.exports = {
  theme: {
    animate: {
      DEFAULT: '…'
    }
  }
}
```
If we then use `theme('animate')` in our JS config/plugin, then we
must receive the entire object, not the default value.

Additionally, in Tailwind CSS v4, if you use:
```css
@theme {
  --animate: …;
}
```

If you then use `theme('animate')` you also have to get the object with
the `DEFAULT` key in there.

If we don't do this, then places where we assume that it's an object
would break. E.g.:
```js
module.exports = {
  theme: {
    animation: ({ theme }) => ({
      ...theme('animate', {}),
    }),
  }
}
```

Luckily, even in the Tailwind CSS v3 days this returned an object. So if
you do want the value, you can use `theme('animate.DEFAULT')`. Which
also works today in both Tailwind CSS v3 and v4.

Side note, this is only for the `theme` function exposed to the JS based
plugins and config files. If you use `theme(animate)` as part of your
CSS, you will get the DEFAULT value, because you can't use objects in
your CSS anyway.
@RobinMalfait

Copy link
Copy Markdown
Member

What made me think this could've gone unnoticed for a while is that it's completely silent.

Yes and no, you would notice it given that some things just don't work if you do this. If you don't notice anything wrong, it means you're not using anything related to this and nothing will be broken.


Overal, I think the idea is good, but I don't like the spreadTheme function. Instead the theme('animate', {}) should use an object no matter what (at least in the JS plugins/configs). If --animate: … is set, it should contain the DEFAULT key.

This is how Tailwind CSS v3 behaves where a top-level namespace like this should result in an object.
Luckily, most places already use theme('animate.DEFAULT') if they just want to get the default value out. E.g.: https://sourcegraph.com/search?q=context:global+theme%5C%28%27.*%3F%5C.DEFAULT%27%5C%29&patternType=regexp&sm=0

There is a chance that returning an object instead of a string will break, but in that case the fix in user land would be to use theme('namespace.DEFAULT'). Right now, before this change, there is no user land workaround we can use here.

I pushed some changes to this branch.

@RobinMalfait
RobinMalfait enabled auto-merge (squash) August 10, 2026 15:17
@RobinMalfait
RobinMalfait disabled auto-merge August 10, 2026 15:18
@RobinMalfait
RobinMalfait enabled auto-merge (squash) August 10, 2026 15:18

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should do it, thanks for the PR!

@RobinMalfait
RobinMalfait merged commit 16e94cb into tailwindlabs:main Aug 10, 2026
9 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.

2 participants