Skip to content

defineCacheKeyGenerator never runs for browser instances: generators are registered under the instance's config but looked up under the parent's #12154

Backlog

Description

@maxschwarzmueller

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):

  • _attachProjectServers registers each project's generators under project.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 in vitest.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 browser

The 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.2

The source above is unchanged on main (1f950e4) and in 5.2.0-beta.1.

Used Package Manager

pnpm

Validations

Activity

  1. sheremet-va commented on Dec 1, 2026

    @sheremet-va
    Member

    FsModuleCache doesn’t work in the browser, how are you even hitting it? AI hallucination?

  2. maxschwarzmueller commented on Dec 1, 2026

    @maxschwarzmueller
    Author

    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's ssr environment, through project._transformService.transform(), and that call reads and writes fsModuleCache:

    With the gist and DEBUG=vitest:cache:fs (same on 5.1.2 and 5.2.0-beta.1):

    pnpm exec vitest --run --project browser
    # no vitest:cache:fs output
    
    pnpm exec vitest related answer.ts --run --project browser
    # [write] <root>/browser.test.ts is cached in <root>/node_modules/.vitest-cache/…
    # [write] <root>/answer.ts is cached in <root>/node_modules/.vitest-cache/…
    # a second run logs [read] for both; the generator never logs

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions