From 0c13c39c2ddc5cd3deea0636d64f24d95fc93bdd Mon Sep 17 00:00:00 2001 From: Adam Hines Date: Thu, 31 Aug 2023 05:51:34 -0600 Subject: [PATCH] fix: resolving dep optimizer issues with workspace (#4036) --- packages/vitest/src/node/cache/index.ts | 11 ++++++++--- packages/vitest/src/node/config.ts | 2 +- packages/vitest/src/node/plugins/utils.ts | 7 +++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 98877d8d0d0f..a63c2ef5d8cf 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -1,4 +1,5 @@ import fs from 'node:fs' +import crypto from 'node:crypto' import { findUp } from 'find-up' import { resolve } from 'pathe' import { loadConfigFromFile } from 'vite' @@ -20,8 +21,11 @@ export class VitestCache { return this.stats.getStats(key) } - static resolveCacheDir(root: string, dir: string | undefined) { - return resolve(root, slash(dir || 'node_modules/.vitest')) + static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) { + const baseDir = slash(dir || 'node_modules/.vitest') + return projectName + ? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex')) + : resolve(root, baseDir) } static async clearCache(options: CliOptions) { @@ -38,11 +42,12 @@ export class VitestCache { : undefined const cache = config?.test?.cache + const projectName = config?.test?.name if (cache === false) throw new Error('Cache is disabled') - const cachePath = VitestCache.resolveCacheDir(root, cache?.dir) + const cachePath = VitestCache.resolveCacheDir(root, cache?.dir, projectName) let cleared = false diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index ae10f25d3858..00f64eaecd3c 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -319,7 +319,7 @@ export function resolveConfig( resolved.cache ??= { dir: '' } if (resolved.cache) - resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir) + resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name) resolved.sequence ??= {} as any if (!resolved.sequence?.sequencer) { diff --git a/packages/vitest/src/node/plugins/utils.ts b/packages/vitest/src/node/plugins/utils.ts index f9ca637ff39f..f855a2041efe 100644 --- a/packages/vitest/src/node/plugins/utils.ts +++ b/packages/vitest/src/node/plugins/utils.ts @@ -2,6 +2,7 @@ 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' export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, testConfig: InlineConfig) { const testOptions = _testOptions || {} @@ -22,7 +23,8 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u } } else { - const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : null + const root = testConfig.root ?? process.cwd() + const cacheDir = testConfig.cache !== false ? testConfig.cache?.dir : undefined const currentInclude = (testOptions.include || viteOptions?.include || []) const exclude = [ 'vitest', @@ -34,7 +36,8 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u exclude.push(...runtime) const include = (testOptions.include || viteOptions?.include || []).filter((n: string) => !exclude.includes(n)) - newConfig.cacheDir = cacheDir ?? 'node_modules/.vitest' + + newConfig.cacheDir = cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name) newConfig.optimizeDeps = { ...viteOptions, ...testOptions,