Skip to content

Commit 0b44722

Browse files
authored
fix: ignore importer when resolving Vitest (#6469)
1 parent 0223bb7 commit 0b44722

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

packages/vitest/src/node/core.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, promises as fs } from 'node:fs'
22
import type { Writable } from 'node:stream'
33
import type { ViteDevServer } from 'vite'
4-
import { dirname, join, normalize, relative, resolve } from 'pathe'
4+
import { dirname, join, normalize, relative } from 'pathe'
55
import mm from 'micromatch'
66
import { ViteNodeRunner } from 'vite-node/client'
77
import { SnapshotManager } from '@vitest/snapshot/manager'
@@ -12,10 +12,10 @@ import { version } from '../../package.json' with { type: 'json' }
1212
import { getTasks, hasFailed, noop, slash, toArray, wildcardPatternToRegExp } from '../utils'
1313
import { getCoverageProvider } from '../integrations/coverage'
1414
import { workspacesFiles as workspaceFiles } from '../constants'
15-
import { rootDir } from '../paths'
1615
import { WebSocketReporter } from '../api/setup'
1716
import type { SerializedCoverageConfig } from '../runtime/config'
1817
import type { ArgumentsType, OnServerRestartHandler, ProvidedContext, UserConsoleLog } from '../types/general'
18+
import { distDir } from '../paths'
1919
import type { ProcessPool, WorkspaceSpec } from './pool'
2020
import { createPool, getFilePoolName } from './pool'
2121
import { createBenchmarkReporters, createReporters } from './reporters/utils'
@@ -78,7 +78,7 @@ export class Vitest {
7878
private resolvedProjects: WorkspaceProject[] = []
7979
public projects: WorkspaceProject[] = []
8080

81-
public distPath!: string
81+
public distPath = distDir
8282

8383
private _cachedSpecs = new Map<string, WorkspaceSpec[]>()
8484

@@ -106,7 +106,6 @@ export class Vitest {
106106
this.pool = undefined
107107
this.coverageProvider = undefined
108108
this.runningPromise = undefined
109-
this.distPath = undefined!
110109
this._cachedSpecs.clear()
111110

112111
const resolved = resolveConfig(this.mode, options, server.config, this.logger)
@@ -560,20 +559,7 @@ export class Vitest {
560559
}
561560
}
562561

563-
private async initializeDistPath() {
564-
if (this.distPath) {
565-
return
566-
}
567-
568-
// if Vitest is running globally, then we should still import local vitest if possible
569-
const projectVitestPath = await this.vitenode.resolveId('vitest')
570-
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
571-
this.distPath = join(vitestDir, 'dist')
572-
}
573-
574562
async runFiles(specs: TestSpecification[], allTestsRun: boolean) {
575-
await this.initializeDistPath()
576-
577563
const filepaths = specs.map(spec => spec.moduleId)
578564
this.state.collectPaths(filepaths)
579565

@@ -638,8 +624,6 @@ export class Vitest {
638624
}
639625

640626
async collectFiles(specs: WorkspaceSpec[]) {
641-
await this.initializeDistPath()
642-
643627
const filepaths = specs.map(spec => spec.moduleId)
644628
this.state.collectPaths(filepaths)
645629

packages/vitest/src/node/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from './utils'
2424
import { VitestOptimizer } from './optimizer'
2525
import { NormalizeURLPlugin } from './normalizeURL'
26+
import { VitestCoreResolver } from './vitestResolver'
2627

2728
export async function VitestPlugin(
2829
options: UserConfig = {},
@@ -253,6 +254,7 @@ export async function VitestPlugin(
253254
SsrReplacerPlugin(),
254255
...CSSEnablerPlugin(ctx),
255256
CoverageTransform(ctx),
257+
VitestCoreResolver(ctx),
256258
options.ui ? await UIPlugin() : null,
257259
...MocksPlugins(),
258260
VitestOptimizer(),

packages/vitest/src/node/plugins/vitestResolver.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { Plugin } from 'vite'
2+
import { join, resolve } from 'pathe'
23
import type { Vitest } from '../core'
4+
import { distDir } from '../../paths'
35

4-
export function VitestResolver(ctx: Vitest): Plugin {
6+
export function VitestProjectResolver(ctx: Vitest): Plugin {
57
const plugin: Plugin = {
68
name: 'vitest:resolve-root',
79
enforce: 'pre',
810
async resolveId(id, _, { ssr }) {
9-
if (id === 'vitest' || id.startsWith('@vitest/')) {
11+
if (id === 'vitest' || id.startsWith('@vitest/') || id.startsWith('vitest/')) {
1012
// always redirect the request to the root vitest plugin since
1113
// it will be the one used to run Vitest
1214
const resolved = await ctx.server.pluginContainer.resolveId(id, undefined, {
@@ -19,3 +21,21 @@ export function VitestResolver(ctx: Vitest): Plugin {
1921
}
2022
return plugin
2123
}
24+
25+
export function VitestCoreResolver(ctx: Vitest): Plugin {
26+
return {
27+
name: 'vitest:resolve-core',
28+
enforce: 'pre',
29+
async resolveId(id) {
30+
if (id === 'vitest') {
31+
return resolve(distDir, 'index.js')
32+
}
33+
if (id.startsWith('@vitest/') || id.startsWith('vitest/')) {
34+
// ignore actual importer, we want it to be resolved relative to the root
35+
return this.resolve(id, join(ctx.config.root, 'index.html'), {
36+
skipSelf: true,
37+
})
38+
}
39+
},
40+
}
41+
}

packages/vitest/src/node/plugins/workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
hijackVitePluginInject,
1616
resolveFsAllow,
1717
} from './utils'
18-
import { VitestResolver } from './vitestResolver'
18+
import { VitestProjectResolver } from './vitestResolver'
1919
import { VitestOptimizer } from './optimizer'
2020
import { NormalizeURLPlugin } from './normalizeURL'
2121

@@ -140,7 +140,7 @@ export function WorkspaceVitestPlugin(
140140
...CSSEnablerPlugin(project),
141141
CoverageTransform(project.ctx),
142142
...MocksPlugins(),
143-
VitestResolver(project.ctx),
143+
VitestProjectResolver(project.ctx),
144144
VitestOptimizer(),
145145
NormalizeURLPlugin(),
146146
]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy