Skip to content

Commit

Permalink
fix: resolving dep optimizer issues with workspace
Browse files Browse the repository at this point in the history
We now use a different cache directory per project in the workspace this way we don't wind up accidentally deleting the cache directory from one project as the next one starts running.
  • Loading branch information
thebanjomatic committed Aug 29, 2023
1 parent e691a9c commit 1d2221c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 8 additions & 3 deletions packages/vitest/src/node/cache/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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) {
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}
Expand All @@ -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',
Expand All @@ -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,
Expand Down

0 comments on commit 1d2221c

Please sign in to comment.