-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
package.json conditional exports in dependencies resolve incorrectly #4971
Comments
I experimented with your reproduction and I think the issue is somewhat related to the fact about "source files" and "external files" explained in #4365 (comment) The resolution discrepancy occurs with To mitigate this issue currently on Vitest, it's necessary to explicitly set https://stackblitz.com/edit/github-b8xjmk?file=vitest.config.ts export default defineConfig({
test: {
environment: 'jsdom',
// this
poolOptions: {
threads: {
execArgv: ['--conditions=development'],
},
},
// or this
server: {
deps: {
inline: ['vcir-package-b'],
},
},
},
// or this (essentially same as 1st one since https://github.com/vitest-dev/vitest/blob/42be8249eac3cadcfaed41f20e2a0147ccb4785d/packages/vitest/src/node/pool.ts#L63)
resolve: {
conditions: process.env.VITEST ? ['development'] : undefined,
},
}); I feel either approach is still clumsy (if you're a library author and needs to ask users to do this), but asking Vitest to automatically inject |
Thank you for the pointers on how to resolve this (can confirm I’m certainly no expert on the internals of Vitest or its thread pooling, but as I heavy user I suppose my expectation is that Vitest always attempts to recreate Vite’s environment as accurately as possible (reusing it where possible). It would be ideal (I think?) if Vitest used the same conditionals that vite was using. |
I totally see that point as well and I was thinking maybe Vitest can replicate some code from Vite https://github.com/vitejs/vite/blob/af2aa09575229462635b7cbb6d248ca853057ba2/packages/vite/src/node/plugins/resolve.ts#L1056-L1080 But, from Vitest standpoint, suddenly injecting |
Vitest already injects But to improve the interoperability we should align conditions the same way they are resolved in Vite (except |
Ok! I’ll take your word for it @sheremet-va Is there a recommended work around we can suggest for our users when we ship these updates (until this issue is resolved). Something like this? export default defineConfig({
resolve: {
conditions: ['development'],
},
test: {
// ...
}
}) |
Yes. Maybe if users don't have it already, it's better to have an if check there if it's not already in a conditions: process.env.VITEST ? ['development'] : undefined |
Describe the bug
👋 First thank you for saving the testing world 😂
I’m a library maintainer (FormKit) and we are gearing up to ship development versions of our packages (for improved error messages etc). We do this by specifying in the package.json’s
exports
a conditional export for "development". This works great in Vite providing development versions of the package in dev, and production ones when built for prod.However, Vitest appears to be inconsistent in which module is resolved even when running with an explicit
NODE_ENV
ofdevelopment
or anconfig.test.env.NODE_ENV
of development. This leads to errors where module scoped state is not consistent.It appears that vite’s
*.spec.ts
files resolve thedevelopment
version properly and the initial dependency in the resolution chain is correct too, but sub-dependencies resolve the production version only. I thought this may have something to do with threads, but have been unable to validate that.Reproducing this can be a bit tricky so I created, and have provided, 2 packages with the following structure:
Vitest Conditional Import Reproduction (vcir)
|--vcir-package-b
|----vcir-package-a
Each of these packages has a package.json that includes an
exports
block something like this:Package A in this scenario has a module scoped variable that gets incremented by the exported
count
method. Package B contains a Vue component that callscount
from Packge A and then renders the value in a Vue component. Every time the component is rendered the value should increment. This works great in the when running vite, but fails in vitest. Using the debugger you can see that thevcir-package-a
uses bothindex.mjs
andindex.mjs
Reproduction
Steps to reproduce this:
npm run dev
(notice the count should start at 2 becausecount()
is called manually in the setup function)System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: