Repository navigation
Conversation
Under `isolate: false` a worker is reused across test files, but `config.env`
is merged into `process.env` only once, when the worker is spawned:
- `packages/vitest/src/node/pool.ts` builds the worker env from
`...ctx.config.env, ...project.config.env`
- `packages/vitest/src/node/config/serializeConfig.ts` serializes
`env: { ...viteConfig?.env, ...config.env }`
Nothing reapplies those keys per file, so a file that assigns to one of them
leaks its value into every later file in the same worker. Vitest 4 reapplied
`test.env` before each file; 5.0.3 does not, and the 5.0 migration guide does
not mention the change.
`runBaseTests.run` now calls `applyConfigEnv(config.env)` inside the per-file
loop. The placement matters: with `isolate: false` and `maxWorkers: 1`,
`groupSpecs` batches every file into a single run request, so a per-run-call
reapplication would never fire between files.
`applyConfigEnv` only restores keys the config actually declares. Keys the
config does not mention still leak between files, which is the documented
behaviour of `isolate: false` and is unchanged.
Values are stringified the same way the spawn-time merge does, because Vite's
own keys (`PROD`, `DEV`, `SSR`) reach `config.env` as booleans and assigning a
boolean to `process.env` does not produce `'1'` / `''`.
Fixes vitest-dev#11423.
|
Your account has been automatically flagged as likely to be created by a bot, LLM, or agent, These measures help us reduce maintenance burden and keep the team's work efficient. See our AI contributions policy for more context. |
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(runtime): reapply
test.envbefore every file in a reused workerUnder
isolate: falsea worker is reused across test files, butconfig.envis merged into
process.envonly once, when the worker is spawned:packages/vitest/src/node/pool.tsbuilds the worker env from...ctx.config.env, ...project.config.envpackages/vitest/src/node/config/serializeConfig.tsserializesenv: { ...viteConfig?.env, ...config.env }Nothing reapplies those keys per file, so a file that assigns to one of them
leaks its value into every later file in the same worker. Vitest 4 reapplied
test.envbefore each file; 5.0.3 does not, and the 5.0 migration guide doesnot mention the change.
Reproduction
8 files,
isolate: false,maxWorkers: 1so they share a worker. Each filereads
DATABASE_URLat module scope and the first one overwrites it:On 5.0.3: 6 failed | 2 passed (8). With this change: 8 passed (8).
The fix
runBaseTests.runnow callsapplyConfigEnv(config.env)inside the per-fileloop.
The placement matters. With
isolate: falseandmaxWorkers: 1,groupSpecs(packages/vitest/src/node/pool.ts) explicitly batches every fileinto a single run request, so a reapplication placed in the per-run setup would
never fire between files.
applyConfigEnvonly restores keys the config actually declares. Keys theconfig does not mention still leak between files, which is the documented
behaviour of
isolate: falseand is unchanged — there is a test pinning that.Values are stringified the same way the spawn-time merge does, because Vite's
own keys (
PROD,DEV,SSR) reachconfig.envas booleans and assigning aboolean to
process.envdoes not yield'1'/''.Tests
test/e2e/test/isolate-false-reapplies-env.test.ts:forksandthreads,isolate: false,maxWorkers: 1fails loudly instead of silently going vacuous if scheduling changes. With more
workers the files are split across them, the leak cannot occur, and the test
would pass with the bug still present.
Without this change the two
test.envcases fail and the unconfigured-keys casestill passes. Full unit suite is unchanged: the same 36 spec files fail before
and after, all pre-existing on a clean checkout.
Fixes #11423.