Skip to content

Commit 06852f1

Browse files
authored
fix: allow workspace without a config in the root (#3173)
1 parent cce4549 commit 06852f1

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

packages/vitest/src/node/core.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ export class Vitest {
152152
}
153153

154154
private async resolveWorkspace(options: UserConfig, cliOptions: UserConfig) {
155-
const configDir = dirname(this.server.config.configFile || this.config.root)
155+
const configDir = this.server.config.configFile
156+
? dirname(this.server.config.configFile)
157+
: this.config.root
156158
const rootFiles = await fs.readdir(configDir)
157159
const workspaceConfigName = workspaceFiles.find((configFile) => {
158160
return rootFiles.includes(configFile)
@@ -161,21 +163,21 @@ export class Vitest {
161163
if (!workspaceConfigName)
162164
return [await this.createCoreWorkspace(options)]
163165

164-
const workspacesConfigPath = join(configDir, workspaceConfigName)
166+
const workspaceConfigPath = join(configDir, workspaceConfigName)
165167

166-
const workspacesModule = await this.runner.executeFile(workspacesConfigPath) as {
168+
const workspaceModule = await this.runner.executeFile(workspaceConfigPath) as {
167169
default: (string | UserWorkspaceConfig)[]
168170
}
169171

170-
if (!workspacesModule.default || !Array.isArray(workspacesModule.default))
171-
throw new Error(`Workspace config file ${workspacesConfigPath} must export a default array of project paths.`)
172+
if (!workspaceModule.default || !Array.isArray(workspaceModule.default))
173+
throw new Error(`Workspace config file ${workspaceConfigPath} must export a default array of project paths.`)
172174

173-
const workspacesGlobMatches: string[] = []
175+
const workspaceGlobMatches: string[] = []
174176
const projectsOptions: UserWorkspaceConfig[] = []
175177

176-
for (const project of workspacesModule.default) {
178+
for (const project of workspaceModule.default) {
177179
if (typeof project === 'string')
178-
workspacesGlobMatches.push(project.replace('<rootDir>', this.config.root))
180+
workspaceGlobMatches.push(project.replace('<rootDir>', this.config.root))
179181
else
180182
projectsOptions.push(project)
181183
}
@@ -189,7 +191,7 @@ export class Vitest {
189191
ignore: ['**/node_modules/**'],
190192
}
191193

192-
const workspacesFs = await fg(workspacesGlobMatches, globOptions)
194+
const workspacesFs = await fg(workspaceGlobMatches, globOptions)
193195
const resolvedWorkspacesPaths = await Promise.all(workspacesFs.filter((file) => {
194196
if (file.endsWith('/')) {
195197
// if it's a directory, check that we don't already have a workspace with a config inside
@@ -204,7 +206,7 @@ export class Vitest {
204206
if (filepath.endsWith('/')) {
205207
const filesInside = await fs.readdir(filepath)
206208
const configFile = configFiles.find(config => filesInside.includes(config))
207-
return configFile || filepath
209+
return configFile ? join(filepath, configFile) : filepath
208210
}
209211
return filepath
210212
}))
@@ -233,11 +235,11 @@ export class Vitest {
233235
this.server.config.configFile === workspacePath
234236
)
235237
return this.createCoreWorkspace(options)
236-
return initializeProject(workspacePath, this, { test: cliOverrides })
238+
return initializeProject(workspacePath, this, { workspaceConfigPath, test: cliOverrides })
237239
})
238240

239241
projectsOptions.forEach((options, index) => {
240-
projects.push(initializeProject(index, this, mergeConfig(options, { test: cliOverrides })))
242+
projects.push(initializeProject(index, this, mergeConfig(options, { workspaceConfigPath, test: cliOverrides }) as any))
241243
})
242244

243245
if (!projects.length)

packages/vitest/src/node/workspace.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ import { isBrowserEnabled, resolveConfig } from './config'
1414
import { WorkspaceVitestPlugin } from './plugins/workspace'
1515
import { VitestServer } from './server'
1616

17-
interface InitializeOptions {
17+
interface InitializeServerOptions {
1818
server?: VitestServer
1919
runner?: ViteNodeRunner
2020
}
2121

22-
export async function initializeProject(workspacePath: string | number, ctx: Vitest, options: (UserWorkspaceConfig & { extends?: string }) = {}) {
22+
interface InitializeProjectOptions extends UserWorkspaceConfig {
23+
workspaceConfigPath: string
24+
extends?: string
25+
}
26+
27+
export async function initializeProject(workspacePath: string | number, ctx: Vitest, options: InitializeProjectOptions) {
2328
const project = new WorkspaceProject(workspacePath, ctx)
2429

2530
const configFile = options.extends
26-
? resolve(ctx.config.root, options.extends)
31+
? resolve(dirname(options.workspaceConfigPath), options.extends)
2732
: (typeof workspacePath === 'number' || workspacePath.endsWith('/'))
2833
? false
2934
: workspacePath
@@ -143,7 +148,7 @@ export class WorkspaceProject {
143148
this.browser = await createBrowserServer(this, options)
144149
}
145150

146-
async setServer(options: UserConfig, server: ViteDevServer, params: InitializeOptions = {}) {
151+
async setServer(options: UserConfig, server: ViteDevServer, params: InitializeServerOptions = {}) {
147152
this.config = resolveConfig(this.ctx.mode, options, server.config)
148153
this.server = server
149154

0 commit comments

Comments
 (0)