From f0aeaca8e5c7c8b2411d8e372fb6beb5426277f0 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 5 Nov 2024 12:53:56 +0100 Subject: [PATCH] fix: don't change the working directory when loading workspace projects (#6811) --- .../src/node/workspace/resolveWorkspace.ts | 65 ++++++------------- .../space_1/test/env-injected.spec.ts | 7 +- 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/packages/vitest/src/node/workspace/resolveWorkspace.ts b/packages/vitest/src/node/workspace/resolveWorkspace.ts index d62fbd6e2443..0d17233505fd 100644 --- a/packages/vitest/src/node/workspace/resolveWorkspace.ts +++ b/packages/vitest/src/node/workspace/resolveWorkspace.ts @@ -2,9 +2,9 @@ import type { Vitest } from '../core' import type { UserConfig, UserWorkspaceConfig, WorkspaceProjectConfiguration } from '../types/config' import type { WorkspaceProject } from '../workspace' import { existsSync, promises as fs } from 'node:fs' -import { isMainThread } from 'node:worker_threads' +import { limitConcurrency } from '@vitest/runner/utils' import fg from 'fast-glob' -import { dirname, relative, resolve } from 'pathe' +import { relative, resolve } from 'pathe' import { mergeConfig } from 'vite' import { configFiles as defaultConfigFiles } from '../../constants' import { initializeProject } from '../workspace' @@ -49,63 +49,40 @@ export async function resolveWorkspace( return acc }, {} as UserConfig) - const cwd = process.cwd() - - const projects: WorkspaceProject[] = [] + const projectPromises: Promise[] = [] const fileProjects = [...configFiles, ...nonConfigDirectories] + const concurrent = limitConcurrency(5) - try { - // we have to resolve them one by one because CWD should depend on the project - for (const filepath of fileProjects) { - // if file leads to the root config, then we can just reuse it because we already initialized it - if (vitest.server.config.configFile === filepath) { - const project = await vitest._createCoreProject() - projects.push(project) - continue - } - - const directory = filepath.endsWith('/') - ? filepath.slice(0, -1) - : dirname(filepath) - - if (isMainThread) { - process.chdir(directory) - } - projects.push( - await initializeProject( - filepath, - vitest, - { workspaceConfigPath, test: cliOverrides }, - ), - ) + for (const filepath of fileProjects) { + // if file leads to the root config, then we can just reuse it because we already initialized it + if (vitest.server.config.configFile === filepath) { + projectPromises.push(concurrent(() => vitest._createCoreProject())) + continue } - } - finally { - if (isMainThread) { - process.chdir(cwd) - } - } - const projectPromises: Promise[] = [] + projectPromises.push( + concurrent(() => initializeProject( + filepath, + vitest, + { workspaceConfigPath, test: cliOverrides }, + )), + ) + } projectConfigs.forEach((options, index) => { - // we can resolve these in parallel because process.cwd() is not changed - projectPromises.push(initializeProject( + projectPromises.push(concurrent(() => initializeProject( index, vitest, mergeConfig(options, { workspaceConfigPath, test: cliOverrides }) as any, - )) + ))) }) // pretty rare case - the glob didn't match anything and there are no inline configs - if (!projects.length && !projectPromises.length) { + if (!projectPromises.length) { return [await vitest._createCoreProject()] } - const resolvedProjects = await Promise.all([ - ...projects, - ...projectPromises, - ]) + const resolvedProjects = await Promise.all(projectPromises) const names = new Set() // project names are guaranteed to be unique diff --git a/test/workspaces/space_1/test/env-injected.spec.ts b/test/workspaces/space_1/test/env-injected.spec.ts index 6fb705198d2d..382c388c06ea 100644 --- a/test/workspaces/space_1/test/env-injected.spec.ts +++ b/test/workspaces/space_1/test/env-injected.spec.ts @@ -31,6 +31,9 @@ test('cwd is resolved correctly', () => { expect(process.env.ROOT_CWD_CONFIG).toBe(rootPath) expect(process.env.ROOT_CWD_SERVER).toBe(rootPath) - expect(process.env.SPACE_2_CWD_CONFIG).toBe(spaceRoot) - expect(process.env.SPACE_2_CWD_SERVER).toBe(spaceRoot) + + // ideally, it should be a `spaceRoot`, but support was reverted + // in https://github.com/vitest-dev/vitest/pull/6811 + expect(process.env.SPACE_2_CWD_CONFIG).toBe(rootPath) + expect(process.env.SPACE_2_CWD_SERVER).toBe(rootPath) })