Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with Tailwind modifying global state #9294

Merged
merged 7 commits into from
Sep 9, 2022
Merged

Fix issue with Tailwind modifying global state #9294

merged 7 commits into from
Sep 9, 2022

Conversation

remcohaszing
Copy link
Contributor

When running Tailwind, it modifies the plugin defaults parameters. As a result Tailwind using a Tailwind plugin in the same process twice yields different results.

For reproduction, run the following script (Use native ESM):

import resolveConfig from "tailwindcss/lib/util/resolveConfig.js"
import typography from '@tailwindcss/typography'

const config = (color) => resolveConfig.default([{
  plugins: [typography],
  corePlugins: [],
  theme: {
    colors: {},
    extend: {
      typography: {
        DEFAULT: {
          css: {
            a: {
              color
            },
          },
        },
      },
    },
  },
}])

console.log("Config", false, JSON.stringify(config("#123456")).includes("#abcdef"))
console.log("Config", true, JSON.stringify(config("#abcdef")).includes("#abcdef"))
console.log("Config", false, JSON.stringify(config("#123456")).includes("#abcdef"))

As you can see, in the output, #abcdef is present in the third run, because the second run has added it to some internal state.

I have narrowed down the issue to these lines:

return normalizeConfig(
defaults(
{
theme: resolveFunctionKeys(
mergeExtensions(mergeThemes(allConfigs.map((t) => t?.theme ?? {})))
),

I have refrained from writing a test yet, because although these changes fix it and all tests pass, it feels like it should be fixed somewhere else.

When running Tailwind, it modifies the plugin defaults parameters. As a
result Tailwind using a Tailwind plugin in the same process twice yields
different results.
@thecrypticace thecrypticace self-assigned this Sep 9, 2022
@thecrypticace
Copy link
Contributor

@remcohaszing So defaults is meant to mutate the object passed in. What was happening is that, in recursive calls to mergeWith, we were overwriting the properties of an object. We always called it with a fresh object to merge into in every case except one: when it called itself recursively. Fixing that fixes the bug.

@thecrypticace thecrypticace merged commit 88e98f5 into tailwindlabs:master Sep 9, 2022
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