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: resolving dep optimizer issues with workspace #4036

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions 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'
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
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
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