Repository navigation
Description
Describe the bug
A cache key generator registered through configureVitest({ defineCacheKeyGenerator }) never runs for the modules of a browser instance. A generator that returns false therefore can't keep a module out of fsModuleCache in browser projects, and a string key is never added to the hash either. In node and jsdom projects the same plugin works as documented.
The two sides use different config objects (code at main 1f950e4):
_attachProjectServersregisters each project's generators underproject.config(core.ts#L425-L426).- A browser instance is created by
TestProject._spawnSibling, which reuses the parent's_transformService(project.ts#L582). That service was built with the parent's config (project.ts#L108-L111) and looks generators up under it (transformService.ts#L309-L310, fsModuleCache.ts#L348). - With
instances, only the siblings are invitest.projects. The generators land under each instance's config, and the lookup under the parent's config finds none.
How we hit it: the cache stores a .svelte component's code but not its compile meta. When the component comes from the cache and its virtual CSS module (?svelte&type=style&lang.css) does not, vite-plugin-svelte fails with "failed to load virtual css module". We keep styled components out of the cache with a generator returning false. That works in node projects, but in browser projects getRelevantTestSpecifications / vitest related still reads them from the cache, counts them as failing to load, and adds their tests to every run.
Registering under the config the transform service actually uses fixes it for us. We carry this as a patch on 5.1.2:
-defineCacheKeyGenerator: (callback) => this.cache._modules.defineCacheKeyGenerator(project.config, callback),
+defineCacheKeyGenerator: (callback) => this.cache._modules.defineCacheKeyGenerator(project._transformService.config, callback),(and the same change for experimental_defineCacheKeyGenerator). With several instances, every instance's generators then run for all of them, since they share one transform service. That is fine for our case, but keying generators by project might be the cleaner fix.
Reproduction
https://gist.github.com/maxschwarzmueller/bd1a140481d416d71ad2fb683b26429f
git clone https://gist.github.com/maxschwarzmueller/bd1a140481d416d71ad2fb683b26429f repro && cd repro
pnpm install && pnpm exec playwright install chromium
DEBUG=vitest:cache:fs pnpm exec vitest related answer.ts --run --project browserThe config has a node project and a browser project with one chromium instance. Both test files import answer.ts. The plugin's generator logs and returns false:
configureVitest({ project, defineCacheKeyGenerator }) {
defineCacheKeyGenerator(({ id }) => {
console.log(`generator ran in ${project.name} for ${id.split('/').at(-1)}`)
return false
})
}Expected (what --project node does, and what --project browser prints with the patch above):
generator ran in browser (chromium) for browser.test.ts
generator ran in browser (chromium) for answer.ts
Test Files 1 passed (1)
Actual with --project browser:
vitest:cache:fs [write] <root>/browser.test.ts is cached in <root>/node_modules/.vitest-cache/fea1e39310d5d6ced321bafa51ab7b25c5205f6c
vitest:cache:fs [write] <root>/answer.ts is cached in <root>/node_modules/.vitest-cache/4f7de989ff009ed737f0eff8622c591804d580e3
Test Files 1 passed (1)
System Info
System:
OS: Linux 7.0 Ubuntu 26.04.1 LTS
CPU: (20) x64 Intel(R) Core(TM) Ultra 7 265
Memory: 54.01 GB / 62.39 GB
Container: Yes
Shell: 5.3.9 - /bin/bash
Binaries:
Node: 24.22.1
npm: 11.19.1
pnpm: 12.25.1
Browsers:
Chrome: 157.0.8103.38
npmPackages:
@vitest/browser-playwright: 5.1.2 => 5.1.2
playwright: 1.65.1 => 1.65.1
vitest: 5.1.2 => 5.1.2The source above is unchanged on main (1f950e4) and in 5.2.0-beta.1.
Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Activity
sheremet-va commentedon Dec 1, 2026
FsModuleCache doesn’t work in the browser, how are you even hitting it? AI hallucination?
maxschwarzmueller commentedon Dec 1, 2026
You're right that the browser run itself doesn't use it, and the docs say so. We hit it in
vitest related(and--changed/getRelevantTestSpecifications()). To find a browser project's affected tests, Vitest walks the module graph on the Node side, in the project'sssrenvironment, throughproject._transformService.transform(), and that call reads and writesfsModuleCache:environments.ssrforspec.pool === 'browser'this.project._transformServiceWith the gist and
DEBUG=vitest:cache:fs(same on 5.1.2 and 5.2.0-beta.1):I should have said that up front instead of in the "How we hit it" paragraph. Sorry for the confusion.
If the related walk isn't meant to use the fs cache for browser projects at all, skipping it there would fix our case just as well as running the generators.