Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): deprecate cache.dir option #5229

Merged
merged 19 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1732,18 +1732,10 @@ Test above this limit will be queued to run when available slot appears.

### cache<NonProjectOption />

- **Type**: `false | { dir? }`
- **Type**: `false`
- **CLI**: `--no-cache`, `--cache=false`

Options to configure Vitest cache policy. At the moment Vitest stores cache for test results to run the longer and failed tests first.

#### cache.dir

- **Type**: `string`
- **Default**: `node_modules/.vitest`
- **CLI**: `--cache.dir=./cache`

Path to cache directory.
Use this option if you want to disable the cache feature. At the moment Vitest stores cache file in Vite's [cacheDir](https://vitejs.dev/config/shared-options.html#cachedir) for test results to run the longer and failed tests first.

### sequence

Expand Down
10 changes: 4 additions & 6 deletions packages/vitest/src/node/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { resolve } from 'pathe'
import { loadConfigFromFile } from 'vite'
import { configFiles } from '../../constants'
import type { CliOptions } from '../cli/cli-api'
import { slash } from '../../utils'
import { FilesStatsCache } from './files'
import { ResultsCache } from './results'

Expand All @@ -21,11 +20,10 @@ export class VitestCache {
return this.stats.getStats(key)
}

static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) {
const baseDir = slash(dir || 'node_modules/.vitest')
static resolveCacheDir(dir: string, projectName: string | undefined) {
return projectName
? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'))
: resolve(root, baseDir)
? resolve(dir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'))
: dir
}

static async clearCache(options: CliOptions) {
Expand All @@ -47,7 +45,7 @@ export class VitestCache {
if (cache === false)
throw new Error('Cache is disabled')

const cachePath = VitestCache.resolveCacheDir(root, cache?.dir, projectName)
const cachePath = VitestCache.resolveCacheDir(config!.cacheDir!, projectName)

let cleared = false

Expand Down
6 changes: 1 addition & 5 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
description: 'Enable cache',
argument: '', // allow only boolean
subcommands: {
dir: {
description: 'Path to the cache directory',
argument: '<path>',
normalize: true,
},
dir: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to add a handler that throws an error? I think CLI will still propagate the value even if it's not defined in a schema

Copy link
Contributor Author

@fenghan34 fenghan34 Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed that, I'll update it later

},
// cache can only be "false" or an object
transform(cache) {
Expand Down
16 changes: 12 additions & 4 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { defaultBrowserPort, defaultPort, extraInlineDeps } from '../constants'
import { benchmarkConfigDefaults, configDefaults } from '../defaults'
import { isCI, stdProvider, toArray } from '../utils'
import type { BuiltinPool } from '../types/pool-options'
import { VitestCache } from './cache'
import { BaseSequencer } from './sequencers/BaseSequencer'
import { RandomSequencer } from './sequencers/RandomSequencer'
import type { BenchmarkBuiltinReporters } from './reporters'
import { builtinPools } from './pool'
import { VitestCache } from './cache'

function resolvePath(path: string, root: string) {
return normalize(
Expand Down Expand Up @@ -436,9 +436,17 @@ export function resolveConfig(
resolved.css.modules.classNameStrategy ??= 'stable'
}

resolved.cache ??= { dir: '' }
if (resolved.cache)
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
if (resolved.cache !== false) {
if (resolved.cache && resolved.cache.dir) {
console.warn(
c.yellow(
`${c.inverse(c.yellow(' Vitest '))} "cache.dir" is deprecated. Use vite's "cacheDir" instead.`,
),
)
}

resolved.cache = { dir: VitestCache.resolveCacheDir(viteConfig.cacheDir, resolved.name) }
}

resolved.sequence ??= {} as any
if (!resolved.sequence?.sequencer) {
Expand Down
5 changes: 1 addition & 4 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { searchForWorkspaceRoot, version as viteVersion } from 'vite'
import type { DepOptimizationOptions, ResolvedConfig, UserConfig as ViteConfig } from 'vite'
import { dirname } from 'pathe'
import type { DepsOptimizationOptions, InlineConfig } from '../../types'
import { VitestCache } from '../cache'
import { rootDir } from '../../paths'

export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) {
Expand All @@ -24,8 +23,6 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
}
}
else {
const root = testConfig.root ?? process.cwd()
const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to handle it during the deprecation period

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it. I'm new to the optimizer config, not sure if this is right change, could you please help to review again? Thank you so much!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect test.cache.dir to just set cacheDir, everything else should just refer to config.cacheDir

const currentInclude = (testOptions.include || viteOptions?.include || [])
const exclude = [
'vitest',
Expand All @@ -39,7 +36,7 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u

const include = (testOptions.include || viteOptions?.include || []).filter((n: string) => !exclude.includes(n))

newConfig.cacheDir = cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name)
newConfig.cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : undefined
newConfig.optimizeDeps = {
...viteOptions,
...testOptions,
Expand Down
10 changes: 8 additions & 2 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,13 @@ export interface InlineConfig {

/**
* Options for configuring cache policy.
* @default { dir: 'node_modules/.vitest' }
* @default { dir: 'node_modules/.vite' }
*/
cache?: false | {
dir?: string
/**
* @deprecated Use Vite's `cacheDir` instead.
*/
dir: string
}

/**
Expand Down Expand Up @@ -830,6 +833,9 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f
}

cache: {
/**
* @deprecated
*/
dir: string
} | false

Expand Down
7 changes: 2 additions & 5 deletions test/cache/vitest-custom.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { defineConfig } from 'vite'

export default defineConfig({
test: {
cache: {
dir: 'cache/.vitest-custom',
},
},
cacheDir: 'cache/.vitest-custom',
test: {},
})
4 changes: 1 addition & 3 deletions test/cache/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { defineConfig } from 'vite'

export default defineConfig({
cacheDir: 'cache/.vitest-base',
test: {
pool: 'forks',
cache: {
dir: 'cache/.vitest-base',
},
},
})
8 changes: 1 addition & 7 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,7 @@ test('cache is parsed correctly', () => {
expect(getCLIOptions('--cache')).toEqual({ cache: {} })
expect(getCLIOptions('--no-cache')).toEqual({ cache: false })

expect(getCLIOptions('--cache.dir=./test/cache.json')).toEqual({
cache: { dir: 'test/cache.json' },
})
expect(getCLIOptions('--cache.dir ./test/cache.json')).toEqual({
cache: { dir: 'test/cache.json' },
})
expect(getCLIOptions('--cache.dir .\\test\\cache.json')).toEqual({
expect(getCLIOptions('--cache.dir=./test/cache.json')).not.toEqual({
cache: { dir: 'test/cache.json' },
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/optimize-deps/test/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { importMetaUrl } from '@vitest/test-dep-url'
// TODO: flaky on Windows
// https://github.com/vitest-dev/vitest/pull/5215#discussion_r1492066033
test.skipIf(process.platform === 'win32')('import.meta.url', () => {
expect(importMetaUrl).toContain('/node_modules/.vitest/deps_ssr/')
expect(importMetaUrl).toContain('/node_modules/.vite/deps_ssr/')
})
2 changes: 1 addition & 1 deletion test/optimize-deps/test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { expect, test } from 'vitest'
import { importMetaUrl } from '@vitest/test-dep-url'

test('import.meta.url', () => {
expect(importMetaUrl).toContain('/node_modules/.vitest/deps/')
expect(importMetaUrl).toContain('/node_modules/.vite/deps/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change means that Vite and Vitest are going to share the same folder for pre-bundling (aka optimizeDeps).

Though Vitest side of pre-bundling is disabled by default since v1.3.0 #5156, would this mean that users will not be able to run vite dev and vitest in watch mode at the same time when they explicitly use deps.optimizer.enable and have different optimizeDeps settings between two?

Copy link
Contributor

@hi-ogawa hi-ogawa Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, in current main branch, running these two commands at the same time seems to work even with deps.optimizer.web.enabled: true. DEBUG=vite:deps is convenient to see what Vite does inside node_modules/.vite

DEBUG=vite:deps pnpm -C examples/react-testing-lib dev
DEBUG=vite:deps pnpm -C examples/react-testing-lib test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question and example, I tried running pnpm dlx concurrently "DEBUG=vite:deps pnpm -C examples/react-testing-lib dev" "DEBUG=vite:deps pnpm -C examples/react-testing-lib test" in this PR's branch, with the workspace's Vitest version, it seems to have worked as expected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I just tried and it looks like it actually works, but it might be a bit coincidental.

When running pnpm dev, Vite generates files such as examples/react-testing-lib/node_modules/.vite/deps/react.js, then when I run pnpm test, Vitest will remove those files. The reason why Vite can keep serving the files in .vite/deps might be because of Vite's in-memory cache for js requests (I tried "Disable cache" in browser devtools).

I think I need to dig into this further to see whether buggy behavior is reproducible.
Or maybe running Vite and Vitest at the same time in the same directory can be considered an unsupported feature (at least when combined with deps.optimizer.enabled).

Actually I think Vitest already uses node_modules/.vite when using Vitest browser mode https://vitest.dev/guide/browser.html#browser-mode, so this might not be an issue in practice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sheremet-va Do you know if it's safe to use a same directory node_modules/.vite/deps as Vite for deps optimization?

Copy link
Member

@sheremet-va sheremet-va Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's safe, that's why this option was introduced in the first place 🤔

The main reason back then was that we were not sure if .vite deletes everything when deps are changed, or just the deps folder. If it just deletes the deps, then we can have .vite/vitest/... cache folder

Copy link
Contributor

@hi-ogawa hi-ogawa Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Vite deletes only node_modules/.vite/deps, for example, this code is for --force flag https://github.com/vitejs/vite/blob/b20d54257e6105333c19676a403c574667878e0f/packages/vite/src/node/optimizer/index.ts#L382-L384

I agree we should avoid overlapping .vite/deps, so using node_modules/.vite/vitest sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, I'll update with .vite/vitest later.

})
Loading