Skip to content

Commit 708c05d

Browse files
authored
feat(cache): add fs module cache env variables (#11859)
1 parent 9c044db commit 708c05d

4 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎docs/config/fsmodulecache.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ In watch mode, Vitest caches all transformed files in memory, which makes reruns
1313

1414
A single cache directory is shared by every project in the workspace. By default it lives in `node_modules` at the workspace root (so it is naturally invalidated when dependencies are reinstalled); use [`fsModuleCachePath`](/config/fsmodulecachepath) to change its location. You can delete the cache by running [`vitest --clearCache`](/guide/cli#clearcache).
1515

16+
If the config and the CLI do not set this option, Vitest reads it from the `VITEST_FS_MODULE_CACHE` environment variable. Set it to `false` to disable the cache without changing the project:
17+
18+
```shell
19+
VITEST_FS_MODULE_CACHE=false vitest
20+
```
21+
1622
::: warning BROWSER SUPPORT
1723
At the moment, this option does not affect [the browser](/guide/browser/).
1824
:::

‎docs/config/fsmodulecachepath.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This can be set per project; projects that don't override it fall back to the ro
1515

1616
By default Vitest stores the cache inside `node_modules` at the workspace root. The root is based on your package manager's lockfile (for example, `.package-lock.json`, `.yarn-state.yml`, `.pnpm/lock.yaml` and so on). Keeping it inside `node_modules` means the cache is naturally invalidated whenever dependencies are reinstalled.
1717

18+
If the config and the CLI do not set this option, Vitest reads it from the `VITEST_FS_MODULE_CACHE_PATH` environment variable. A relative path is resolved from the [root](/config/root).
19+
1820
```ts
1921
import { defineConfig } from 'vitest/config'
2022

‎packages/vitest/src/node/config/resolveConfig.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,16 @@ export function resolveTestConfig(
10921092
resolved.fsModuleCachePath = legacyExperimental.fsModuleCachePath
10931093
}
10941094
}
1095+
// the config and the CLI take priority over the environment
1096+
const fsModuleCacheEnv = process.env.VITEST_FS_MODULE_CACHE
1097+
if (resolved.fsModuleCache == null && fsModuleCacheEnv) {
1098+
resolved.fsModuleCache = fsModuleCacheEnv !== 'false' && fsModuleCacheEnv !== '0'
1099+
resolved.providedOptions.fsModuleCache = true
1100+
}
10951101
resolved.fsModuleCache ??= true
1096-
if (resolved.fsModuleCachePath) {
1097-
resolved.fsModuleCachePath = resolve(resolved.root, resolved.fsModuleCachePath)
1102+
const fsModuleCachePath = resolved.fsModuleCachePath || process.env.VITEST_FS_MODULE_CACHE_PATH
1103+
if (fsModuleCachePath) {
1104+
resolved.fsModuleCachePath = resolve(resolved.root, fsModuleCachePath)
10981105
}
10991106

11001107
return resolved

‎test/e2e/test/caching.test.ts‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { expect, test } from 'vitest'
2-
import { runInlineTests, runVitest, useFS } from '../../test-utils'
1+
import { existsSync } from 'node:fs'
2+
import { join } from 'pathe'
3+
import { expect, onTestFinished, test, vi } from 'vitest'
4+
import { runInlineTests, runVitest, runVitestCli, useFS, useTmpFS } from '../../test-utils'
35

46
test("if file has import.meta.glob, it's not cached", async () => {
57
const { createFile } = useFS(
@@ -602,6 +604,63 @@ test('fsModuleCache does not fail the run when the cache directory cannot be cre
602604
expect(stderr.match(/could not be created/g)).toHaveLength(1)
603605
})
604606

607+
test('VITEST_FS_MODULE_CACHE=false disables the cache when the config does not set it', async () => {
608+
const args = [
609+
'run',
610+
'--root',
611+
'./fixtures/caching/unwritable-cache',
612+
'--fsModuleCachePath',
613+
'./not-a-directory/cache',
614+
]
615+
const env = { VITEST_FS_MODULE_CACHE: 'false' }
616+
617+
const disabled = await runVitestCli({ nodeOptions: { env } }, ...args)
618+
expect(disabled.stderr).toBe('')
619+
expect(disabled.exitCode).toBe(0)
620+
621+
// the CLI option has priority over the environment
622+
const enabled = await runVitestCli({ nodeOptions: { env } }, ...args, '--fsModuleCache')
623+
expect(enabled.stderr.match(/could not be created/g)).toHaveLength(1)
624+
expect(enabled.exitCode).toBe(0)
625+
})
626+
627+
test('VITEST_FS_MODULE_CACHE_PATH moves the cache when the config does not set it', async () => {
628+
vi.stubEnv('VITEST_FS_MODULE_CACHE_PATH', './custom-cache')
629+
onTestFinished(() => {
630+
vi.unstubAllEnvs()
631+
})
632+
const structure = () => ({
633+
'sum.js': `export const sum = (a, b) => a + b`,
634+
'sum.test.js': /* js */ `
635+
import { expect, test } from "vitest"
636+
import { sum } from "./sum.js"
637+
test("adds", () => {
638+
expect(sum(1, 2)).toBe(3)
639+
})
640+
`,
641+
})
642+
643+
const fromEnv = useTmpFS(structure())
644+
const fromEnvRun = await runVitest({ root: fromEnv.root })
645+
expect(fromEnvRun.stderr).toBe('')
646+
expect(existsSync(join(fromEnv.root, 'custom-cache/CACHEDIR.TAG'))).toBe(true)
647+
648+
const fromConfig = useTmpFS({
649+
...structure(),
650+
'vitest.config.js': /* js */ `
651+
export default {
652+
test: {
653+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
654+
},
655+
}
656+
`,
657+
})
658+
const fromConfigRun = await runVitest({ root: fromConfig.root })
659+
expect(fromConfigRun.stderr).toBe('')
660+
expect(existsSync(join(fromConfig.root, 'custom-cache'))).toBe(false)
661+
expect(existsSync(join(fromConfig.root, 'node_modules/.vitest-fs-cache/CACHEDIR.TAG'))).toBe(true)
662+
})
663+
605664
declare module 'vitest' {
606665
export interface ProvidedContext {
607666
generated: string[]

0 commit comments

Comments
 (0)