Skip to content

fix(css): never externalize css at-import specifiers (fix #23096) - #23099

Closed
ayaangazali wants to merge 1 commit into
vitejs:mainfrom
ayaangazali:fix/23096-css-at-import-noexternal
Closed

ayaangazali wants to merge 1 commit into
vitejs:mainfrom
ayaangazali:fix/23096-css-at-import-noexternal

Conversation

@ayaangazali

Copy link
Copy Markdown

fixes #23096

A CSS @import "somepkg" that resolves through the package's style export condition works in a client build but dies in a server build with ENOENT ... open '<projectRoot>/somepkg'. The at-import resolvers in createCSSResolvers (packages/vite/src/node/plugins/css.ts:1361) are built with createBackCompatIdResolver, so in a server environment the bare specifier gets externalized and comes back unchanged. The postcss-import resolve callback at css.ts:1637 only checks that the value is truthy, so it takes the success branch and path.resolves the bare specifier against the root. That is also why the Unable to resolve \@import`` message never appears, which made the ENOENT point at a path nobody wrote.

These three resolvers only exist to turn a specifier into a file we then read off disk, so externalization has no meaning for them, and noExternal: true is what the resolve options already offer for exactly that. I applied it to the sass and less resolvers too since they are built the same way and hit the same path. The alternative I tried first was rejecting non-absolute results in the postcss-import callback, but that only turns the confusing ENOENT into a clearer error and leaves the import unresolved, so the build still fails where a client build succeeds.

Worth noting the existing css test in environment.spec.ts only passed because it sets resolve.noExternal: true in the user config, which is the workaround this bug forces on people. The new test builds the same fixture with externalization left at its default and fails on main with the ENOENT above. Gates run locally: pnpm build, pnpm lint, pnpm typecheck, the css/environment unit specs, plus test-serve and test-build for the css, ssr and tailwind playgrounds.


quick note on process: I'm a college freshman and still pretty new to this codebase, so apologies in advance if I've missed something obvious. I worked through the root cause and the fix with Claude Code (we traced the resolver path together, I reproduced it in a standalone project first, and I ran the gates above locally). Happy to change the approach if you'd rather see the guard live in the postcss-import callback, and genuinely glad to learn from anything I got wrong here.

Copilot AI review requested due to automatic review settings July 28, 2026 21:54

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ayaangazali
ayaangazali force-pushed the fix/23096-css-at-import-noexternal branch from 4b5c2b7 to aee2e32 Compare July 28, 2026 22:28
@ayaangazali

Copy link
Copy Markdown
Author

CI caught a real problem with my first push, so I've narrowed the fix.

I had also passed noExternal: true to the sass and less resolvers on the theory that they were built the same way and shared the defect. That was speculation on my part and it was wrong: noExternal: true also stops node builtins from being externalized, which broke playground/resolve/sass-node-builtin-clash (@use 'util' where util is a local sass directory found through loadPaths). It failed with Cannot bundle Node.js built-in "util" on all six matrix jobs.

So this now only touches the css resolver, which is the one the reported bug actually goes through (postcss-import reads the resolved id off disk). Sass and less keep their current behavior, and their preprocessor-level loadPaths fallback makes the externalized case survivable there in a way it isn't for postcss-import. If a maintainer thinks sass and less should be handled too, it likely needs the builtins question answered separately rather than the same blunt flag.

Re-ran locally on the amended commit: the previously failing resolve playground passes in both modes, the full pnpm test-unit suite passes, plus css, ssr and tailwind playgrounds in serve and build. I also re-confirmed the new test still fails on main with the original ENOENT ... open '<root>/@vitejs/test-dep-conditions'.

@sapphi-red

Copy link
Copy Markdown
Member

Why did the repro work in v7?

@ayaangazali

Copy link
Copy Markdown
Author

Good question, and the answer turned out to be about a missing opt-in rather than anything CSS specific.

In v7, createIdResolver built its plugin container with the JS resolvePlugin, and that plugin only externalized when it was explicitly handed externalize: true. The option is documented as "Defaults to false (only for createResolver)" (v7.3.6 resolve.ts:138-142) and the bare-import externalization branch is gated on it (options.externalize && ... consumer === 'server', v7.3.6 resolve.ts:352). The only caller that set it was the main build pipeline (v7.3.6 plugins/index.ts:67). idResolver.ts never set it, so the CSS at-import resolver always did plain file resolution, in every environment.

In v8, createIdResolver builds with oxcResolvePlugin instead, which passes environmentConsumer: partialEnv.config.consumer plus external and noExternal straight through to the native resolver (resolve.ts:281-285), with no externalize gate. Its options are {...partialEnv.config.resolve, ...resolveOptions}, so a server environment's externalization settings now reach the CSS resolver as well. The resolver hands back the bare specifier unchanged, postcss-import sees a truthy value and treats it as resolved, and path.resolve turns it into <root>/tailwindcss.

So the v7 behavior came from an opt-in that protected internal resolvers, and the native path does not have it. That raises a fair question about my patch: the more general fix would be to honour externalize in oxcResolvePlugin for createIdResolver callers, which would also cover the optimizer's resolver in optimizer/resolve.ts rather than just CSS. I went with the local noExternal: true because it was the smallest change I could fully prove with a test, but if you would prefer the gate restored at the resolver layer I am happy to redo it that way.

@sapphi-red

Copy link
Copy Markdown
Member

Its options are {...partialEnv.config.resolve, ...resolveOptions}, so a server environment's externalization settings now reach the CSS resolver as well.

I think that's the same with v7.

const options: InternalResolveOptions = {
isRequire,
...currentEnvironmentOptions.resolve,
...resolveOptions, // plugin options + resolve options overrides
scan: resolveOpts.scan ?? resolveOptions.scan,
}

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.

CSS @import of a bare package specifier resolves in client builds but fails in SSR builds

3 participants