Skip to content

Commit 0c13c39

Browse files
authoredAug 31, 2023
fix: resolving dep optimizer issues with workspace (#4036)
1 parent 807418f commit 0c13c39

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed
 

‎packages/vitest/src/node/cache/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs'
2+
import crypto from 'node:crypto'
23
import { findUp } from 'find-up'
34
import { resolve } from 'pathe'
45
import { loadConfigFromFile } from 'vite'
@@ -20,8 +21,11 @@ export class VitestCache {
2021
return this.stats.getStats(key)
2122
}
2223

23-
static resolveCacheDir(root: string, dir: string | undefined) {
24-
return resolve(root, slash(dir || 'node_modules/.vitest'))
24+
static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) {
25+
const baseDir = slash(dir || 'node_modules/.vitest')
26+
return projectName
27+
? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'))
28+
: resolve(root, baseDir)
2529
}
2630

2731
static async clearCache(options: CliOptions) {
@@ -38,11 +42,12 @@ export class VitestCache {
3842
: undefined
3943

4044
const cache = config?.test?.cache
45+
const projectName = config?.test?.name
4146

4247
if (cache === false)
4348
throw new Error('Cache is disabled')
4449

45-
const cachePath = VitestCache.resolveCacheDir(root, cache?.dir)
50+
const cachePath = VitestCache.resolveCacheDir(root, cache?.dir, projectName)
4651

4752
let cleared = false
4853

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export function resolveConfig(
319319

320320
resolved.cache ??= { dir: '' }
321321
if (resolved.cache)
322-
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir)
322+
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
323323

324324
resolved.sequence ??= {} as any
325325
if (!resolved.sequence?.sequencer) {

‎packages/vitest/src/node/plugins/utils.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { searchForWorkspaceRoot, version as viteVersion } from 'vite'
22
import type { DepOptimizationOptions, ResolvedConfig, UserConfig as ViteConfig } from 'vite'
33
import { dirname } from 'pathe'
44
import type { DepsOptimizationOptions, InlineConfig } from '../../types'
5+
import { VitestCache } from '../cache'
56

67
export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) {
78
const testOptions = _testOptions || {}
@@ -22,7 +23,8 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
2223
}
2324
}
2425
else {
25-
const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : null
26+
const root = testConfig.root ?? process.cwd()
27+
const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : undefined
2628
const currentInclude = (testOptions.include || viteOptions?.include || [])
2729
const exclude = [
2830
'vitest',
@@ -34,7 +36,8 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u
3436
exclude.push(...runtime)
3537

3638
const include = (testOptions.include || viteOptions?.include || []).filter((n: string) => !exclude.includes(n))
37-
newConfig.cacheDir = cacheDir ?? 'node_modules/.vitest'
39+
40+
newConfig.cacheDir = cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name)
3841
newConfig.optimizeDeps = {
3942
...viteOptions,
4043
...testOptions,

0 commit comments

Comments
 (0)
Please sign in to comment.