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

Build Error: Selector ":root,:host" is not pure (nextjs/postcss) #16725

Closed
gene-git opened this issue Feb 21, 2025 · 12 comments · Fixed by #16774
Closed

Build Error: Selector ":root,:host" is not pure (nextjs/postcss) #16725

gene-git opened this issue Feb 21, 2025 · 12 comments · Fixed by #16774

Comments

@gene-git
Copy link

versions:

   node: '23.8.0',
    "@tailwindcss/typography": "^0.5.16",
    "@tailwindcss/postcss": "^4.0.8",
    "tailwindcss": "^4.0.8"
    "postcss": "^8.5.3",

     npm: '11.1.0',
    "next": "^15.1.7",
    "react": "^19.0.0",

While I have not changed any code of mine, it is always possible there's something I am not doing correctly. That said here's what I am seeing.

Issue:

Our current production server is running tailwindcss 4.0.6 and postcss 8.5.2 with same versions of next and react and builds and runs without problems.
After updating to above versions, I am now getting this error when building

./src/app/page.module.css:2:14
Syntax error: Selector ":root,:host" is not pure (pure selectors must contain at least one local class or id)

The file page.module.css is the file imported to provide css everwhere.
It contains:

 @reference "./globals.css";

/* shared css defined below */
.container {
    @apply md:flex ...
}
all common definitions in same file

globals.css contains:

@import 'tailwindcss';
@plugin "@tailwindcss/typography";

@layer base {
  *,
  ::after,
  ::before,
  ::backdrop,
  ::file-selector-button {
    border-color: var(--color-gray-200, currentColor);
  }
}


@theme {
   --shadow-3xl: 0 35px 60px rgba(0, 0, 0, 0.3);
   --shadow-4xl: 0 70px 60px rgba(0, 0, 0, 0.3);
}

When running dev using updated packages, it seems to work fine but when building for production it generates this error:

   Creating an optimized production build ...
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|javascript/auto|xxx/node_modules/next/dist/build/webpack/loaders/next-flight-css-loader.js??ruleSet[1].rules[13].oneOf[5].use[0]!xxx/node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[13].oneOf[5].use[1]!xxx/node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[13].oneOf[5].use[2]!xxx/src/app/page.module.css|rsc': No serializer registered for CssSyntaxError
<w> while serializing webpack/lib/cache/PackFileCacheStrategy.PackContentItems -> webpack/lib/NormalModule -> webpack/lib/ModuleBuildError -> CssSyntaxError
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|javascript/auto|xxx/node_modules/next/dist/build/webpack/loaders/next-flight-css-loader.js??ruleSet[1].rules[13].oneOf[5].use[0]!xxx/node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[13].oneOf[5].use[1]!xxx/node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[13].oneOf[5].use[2]!xxx/src/app/page.module.css|ssr': No serializer registered for CssSyntaxError
<w> while serializing webpack/lib/cache/PackFileCacheStrategy.PackContentItems -> webpack/lib/NormalModule -> webpack/lib/ModuleBuildError -> CssSyntaxError
Failed to compile.

./src/app/page.module.css:2:14
Syntax error: Selector ":root,:host" is not pure (pure selectors must contain at least one local class or id)

@wongjn
Copy link
Contributor

wongjn commented Feb 21, 2025

Seems like it is due to #16676, essentially:

So given the following input CSS:

@reference "tailwindcss";
.text-red {
  @apply text-red-500;
}

We will now compile this to:

@layer theme {
  :root, :host {
    --text-red-500: oklch(0.637 0.237 25.331);
  }
}
.text-red {
  color: var(--text-red-500);
}

Where the @reference now outputs a :root, :host rule that the build is complaining about.

@gene-git
Copy link
Author

Thanks ! The comments suggest bug is fixed in 4.0.8 but thats the version I am using- do you know if the fix is supposed to be in 4.0.8?

@wongjn
Copy link
Contributor

wongjn commented Feb 21, 2025

PR #16676 is in 4.0.8. It seems like the changes in PR #16676 is what is causing your issue, not that it fixes it.

@philipp-spiess
Copy link
Member

@gene-git Thanks for the bug report, we're looking into it.

@gene-git
Copy link
Author

Thank you!

@gavan1
Copy link

gavan1 commented Feb 24, 2025

same issue here.

@rifatnear
Copy link

rifatnear commented Feb 24, 2025

I am also getting same issue, i have a request anyone know when it's will be solved ?

Image

@gavan1
Copy link

gavan1 commented Feb 25, 2025

I did a quite a few things to solve the issue.. I think these were the ones the that fixed it.

my issue seemed to be a CSS module that had an @reference , and @apply .

I commented those out as well as the

@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
@import "tailwindcss/utilities.css" layer(utilities);

Ran the build and it seems to have fixed itself.. and was abe to uncomment ad build again.

Hope this helps others.

@glucn
Copy link

glucn commented Feb 25, 2025

Got the same issue

philipp-spiess added a commit that referenced this issue Feb 25, 2025
…larations (#16774)

Fixes #16725

When using `@reference "tailwindcss";` inside a separate CSS root (e.g.
Svelte `<style>` components, CSS modules, etc.), we have no guarantee
that the CSS variables will be defined in the main stylesheet (or if
there even is one). To work around potential issues with this we decided
in #16676 that we would emit all used CSS variables from the `@theme`
inside the `@reference` block.

However, this is not only a bit surprising but also unexpected in CSS
modules and Next.js that **requires CSS module files to only create
scope-able declarations**. To fix this issue, we decided to not emit CSS
variables but instead ensure all `var(…)` calls we create for theme
values in reference mode will simply have their fallback value added.

This ensures styles work as-expected even if the root Tailwind file does
not pick up the variable as being used or _if you don't add a root at
all_. Furthermore we do not duplicate any variable declarations across
your stylesheets and you still have the ability to change variables at
runtime.

## Test plan

- Updated snapshots everywhere (see diff)
- New Next.js CSS modules integration test
@rifatnear
Copy link

@gavan1 thank you very much for your help.

i updated @tailwindcss/postcss and tailwindcss version 4.0.9 then this problem solved.

@gene-git
Copy link
Author

@philipp-spiess thank you. Confirm 4.0.9 resolves this issue.

@philipp-spiess
Copy link
Member

Oh yeah sorry forgot to post an update here but this should be resolved now with the latest patch release 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants