fix: cache transform results in getTestDependencies across projects (fix #9855)#9936
Open
seanogdev wants to merge 2 commits intovitest-dev:mainfrom
Open
fix: cache transform results in getTestDependencies across projects (fix #9855)#9936seanogdev wants to merge 2 commits intovitest-dev:mainfrom
getTestDependencies across projects (fix #9855)#9936seanogdev wants to merge 2 commits intovitest-dev:mainfrom
Conversation
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
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
AriPerkkio
reviewed
Mar 21, 2026
| if (!transformed) { | ||
| return | ||
| let dependencies: string[] | ||
| const cached = transformCache?.get(filepath) |
Member
There was a problem hiding this comment.
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.
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.
Summary
When running
vitest relatedwith multiple projects,getTestDependenciescallstransformRequeston 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-vuevia a common Vite config:@vitejs/plugin-vueuses a module-leveldescriptorCache(aMapkeyed 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>becomeundefinedat runtime.Running
vitest relatedwith both projects caused widespread browser test failures. Runningvitest 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
relateddependency resolution. The cache poisoning itself requires@vitejs/plugin-vuewith browser mode to reproduce - this was verified against a real project but not added as a test dependency.Closes #9855