Skip to content

fix: cache transform results in getTestDependencies across projects (fix #9855)#9936

Open
seanogdev wants to merge 2 commits intovitest-dev:mainfrom
seanogdev:related-transform-cache
Open

fix: cache transform results in getTestDependencies across projects (fix #9855)#9936
seanogdev wants to merge 2 commits intovitest-dev:mainfrom
seanogdev:related-transform-cache

Conversation

@seanogdev
Copy link

Summary

When running vitest related with multiple projects, getTestDependencies calls transformRequest on shared files through each project's Vite server independently. Plugins with module-level caches get corrupted when the same file is transformed by multiple server instances.

The problem

In a workspace with two projects - one using jsdom for unit tests, one using browser/Playwright for component tests (Storybook) - both sharing @vitejs/plugin-vue via a common Vite config:

@vitejs/plugin-vue uses a module-level descriptorCache (a Map keyed by filename, shared across all servers in the process). When the second server transforms a file the first already processed, the stale descriptor causes compiled output to differ from a clean transform. For <script setup> components, template-only imports get dropped - components used only in <template> become undefined at runtime.

Running vitest related with both projects caused widespread browser test failures. Running vitest related --project <storybook-only> passed cleanly, confirming the cross-project interaction.

The fix

A shared Map<string, string[]> caches each file's resolved dependency list across all specs, so each file is only transformed once. The dependency list is the same regardless of which server does the transform.

Test coverage

The regression test verifies multi-project related dependency resolution. The cache poisoning itself requires @vitejs/plugin-vue with browser mode to reproduce - this was verified against a real project but not added as a test dependency.

Closes #9855

When running `vitest related` with multiple projects, `getTestDependencies`
called `transformRequest` for each project on shared files. This caused
plugins with module-level caches (like `@vitejs/plugin-vue`'s
`descriptorCache`) to be poisoned when the same file was transformed by
multiple Vite server instances.

Adds a `transformCache` that shares dependency results across all specs
within a single `filterTestsBySource` call, so each file is only
transformed once during dependency analysis.

Closes vitest-dev#9855
@netlify
Copy link

netlify bot commented Mar 21, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0a31f3f
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69be95321aa7e900086ac380
😎 Deploy Preview https://deploy-preview-9936--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

if (!transformed) {
return
let dependencies: string[]
const cached = transformCache?.get(filepath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this cause issues if two Vite plugins transform the same file differently? For example if your Node tests dropped some browser specific module imports, the browser tests would get the cached version and see incorrect test dependencies.

Prevents cross-project cache poisoning when different Vite plugins
transform the same file differently per project.
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.

vitest related with multiple projects corrupts Vue SFC compilation via shared plugin cache

2 participants