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: don't change the working directory when loading workspace projects #6811

Merged
merged 5 commits into from
Nov 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})