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

fix(vitest): Fix cacheDir not being honored if deps.optimizer.{mode}.enabled is true #6902

Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/vitest/src/node/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function createVitest(
// this will make "mode": "test" | "benchmark" inside defineConfig
mode: options.mode || mode,
plugins: await VitestPlugin(options, ctx),
test: options,
}

const server = await createViteServer(
Expand Down
4 changes: 0 additions & 4 deletions packages/vitest/src/node/plugins/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ export function VitestOptimizer(): Plugin {
const webOptimizer = resolveOptimizerConfig(
testConfig.deps?.optimizer?.web,
viteConfig.optimizeDeps,
testConfig,
)
const ssrOptimizer = resolveOptimizerConfig(
testConfig.deps?.optimizer?.ssr,
viteConfig.ssr?.optimizeDeps,
testConfig,
)

viteConfig.cacheDir
= webOptimizer.cacheDir || ssrOptimizer.cacheDir || viteConfig.cacheDir
viteConfig.optimizeDeps = webOptimizer.optimizeDeps
viteConfig.ssr ??= {}
viteConfig.ssr.optimizeDeps = ssrOptimizer.optimizeDeps
Expand Down
12 changes: 2 additions & 10 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import type {
ResolvedConfig,
UserConfig as ViteConfig,
} from 'vite'
import type { DepsOptimizationOptions, InlineConfig } from '../types/config'
import type { DepsOptimizationOptions } from '../types/config'
import { dirname } from 'pathe'
import { searchForWorkspaceRoot, version as viteVersion } from 'vite'
import { rootDir } from '../../paths'
import { VitestCache } from '../cache'

export function resolveOptimizerConfig(
_testOptions: DepsOptimizationOptions | undefined,
viteOptions: DepOptimizationOptions | undefined,
testConfig: InlineConfig,
) {
const testOptions = _testOptions || {}
const newConfig: { cacheDir?: string; optimizeDeps: DepOptimizationOptions }
const newConfig: { optimizeDeps: DepOptimizationOptions }
= {} as any
const [major, minor, fix] = viteVersion.split('.').map(Number)
const allowed
Expand All @@ -32,17 +30,13 @@ export function resolveOptimizerConfig(
testOptions.enabled ??= false
}
if (!allowed || testOptions?.enabled !== true) {
newConfig.cacheDir = undefined
newConfig.optimizeDeps = {
// experimental in Vite >2.9.2, entries remains to help with older versions
disabled: true,
entries: [],
}
}
else {
const root = testConfig.root ?? process.cwd()
const cacheDir
= testConfig.cache !== false ? testConfig.cache?.dir : undefined
const currentInclude = testOptions.include || viteOptions?.include || []
const exclude = [
'vitest',
Expand All @@ -60,8 +54,6 @@ export function resolveOptimizerConfig(
(n: string) => !exclude.includes(n),
)

newConfig.cacheDir
= cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name)
newConfig.optimizeDeps = {
...viteOptions,
...testOptions,
Expand Down
Loading