Skip to content

Commit

Permalink
fix: don't change the working directory when loading workspace projec…
Browse files Browse the repository at this point in the history
…ts (#6811)
  • Loading branch information
sheremet-va authored Nov 5, 2024
1 parent cf0cbf6 commit f0aeaca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 46 deletions.
65 changes: 21 additions & 44 deletions packages/vitest/src/node/workspace/resolveWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -49,63 +49,40 @@ export async function resolveWorkspace(
return acc
}, {} as UserConfig)

const cwd = process.cwd()

const projects: WorkspaceProject[] = []
const projectPromises: Promise<WorkspaceProject>[] = []
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<WorkspaceProject>[] = []
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<string>()

// project names are guaranteed to be unique
Expand Down
7 changes: 5 additions & 2 deletions test/workspaces/space_1/test/env-injected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit f0aeaca

Please sign in to comment.