Browser mode: `--changed` and `related` crash in `vite:css` when the changed source imports a CSS Module
Describe the bug
With browser mode on, vitest run --changed <ref> and vitest related <file> --run crash before any test runs when the changed file imports a CSS Module:
TypeError: Cannot read properties of undefined (reading 'set')
A plain vitest run on the same project passes. With browser mode off, all three commands pass.
The line that throws is moduleCache.set(id, modules) in the transform hook of vite:css. moduleCache only gets assigned in that plugin's buildStart, so my guess is that the related-file detection transforms the CSS Module through a plugin container whose buildStart never ran.
Reproduction
package.json
{
"name": "vitest-browser-changed-repro",
"private": true,
"type": "module",
"devDependencies": {
"@vitest/browser-playwright": "5.0.3",
"playwright": "1.63.0",
"vite": "8.3.3",
"vitest": "5.0.3"
}
}
vite.config.ts
import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['src/**/*.browser.test.ts'],
browser: {
enabled: true,
headless: true,
provider: playwright(),
instances: [{ browser: 'chromium' }]
}
}
})
src/button.module.css
.button {
color: red;
}
src/button.ts
import styles from './button.module.css'
export function buttonClass(): string {
return styles.button
}
src/button.browser.test.ts
import { expect, test } from 'vitest'
import { buttonClass } from './button'
test('applies the module class', () => {
expect(buttonClass()).toContain('button')
})
Commit those files, then:
bun install
npx playwright install chromium
npx vitest run # passes
printf '\nexport const touched = true\n' >> src/button.ts # leave it uncommitted
npx vitest run --changed HEAD # crashes, exit 1
npx vitest related src/button.ts --run # crashes, exit 1
I ran each failing command three times, with and without node_modules/.vite, and got the crash every time.
I lost the repro for a while during the reduction: an untracked second vite.config.* matches the default forceRerunTriggers, so --changed reruns everything and the crash goes away. related crashes either way.
Full output:
⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading 'set')
❯ TransformPluginContext.handler node_modules/vite/dist/node/chunks/node.js:29836:30
❯ EnvironmentPluginContainer.transform node_modules/vite/dist/node/chunks/node.js:8545:14
❯ loadAndTransform node_modules/vite/dist/node/chunks/node.js:20157:26
Serialized Error: { plugin: 'vite:css', id: '<repo>/src/button.module.css', pluginCode: '.button {
color: red;
}
' }
System info
macOS 27.0.1 (arm64)
Node.js 24.21.0
vitest 5.0.3
vite 8.3.3
@vitest/browser-playwright 5.0.3
playwright 1.63.0
I first hit this through vite-plus 1.1.0 (vp test run --changed HEAD), which bundles the versions above. That was a monorepo with projects, and only the browser project crashed.
Used package manager
bun 1.4.2 for the install. Vitest itself runs on Node.js 24.21.0.
I could reproduce this with 5.0.3 (with both Vite 8.3.3 and 8.0.11), but not on main. Bisecting between v5.0.3 and main points to #11432 as the fix. It was merged after 5.0.3, so it should be in the next release.
I also have a regression test for it, using runInlineTests with related. It fails on v5.0.3 with this error and passes on main.
Happy to share it if it's useful.
Thanks for investigating and bisecting this, @nomadical! Could you share the regression test? It would be useful to have this case covered in the test suite.
Here it is: https://github.com/vitest-dev/vitest/compare/main...nomadical:vitest:fix/browser-related-css-modules
It's a single test in test/browser/specs/related-css-module.test.ts that runs related on a file importing a CSS Module. It fails on v5.0.3 and passes on main. I can open a PR if a maintainer wants it in the suite.
Thanks for sharing, @nomadical! @sheremet-va, would you like them to open a PR with this regression test?