Skip to content

Commit 163d762

Browse files
authored
fix(vitest): always resolve vitest to the root version (#6369)
1 parent 5388f0c commit 163d762

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
hijackVitePluginInject,
2222
resolveFsAllow,
2323
} from './utils'
24-
import { VitestResolver } from './vitestResolver'
2524
import { VitestOptimizer } from './optimizer'
2625
import { NormalizeURLPlugin } from './normalizeURL'
2726

@@ -256,7 +255,6 @@ export async function VitestPlugin(
256255
CoverageTransform(ctx),
257256
options.ui ? await UIPlugin() : null,
258257
...MocksPlugins(),
259-
VitestResolver(ctx),
260258
VitestOptimizer(),
261259
NormalizeURLPlugin(),
262260
].filter(notNullish)
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import type { Plugin } from 'vite'
2-
import { join } from 'pathe'
32
import type { Vitest } from '../core'
43

54
export function VitestResolver(ctx: Vitest): Plugin {
6-
return {
5+
const plugin: Plugin = {
76
name: 'vitest:resolve-root',
87
enforce: 'pre',
9-
async resolveId(id) {
8+
async resolveId(id, _, { ssr }) {
109
if (id === 'vitest' || id.startsWith('@vitest/')) {
11-
return this.resolve(id, join(ctx.config.root, 'index.html'), {
12-
skipSelf: true,
10+
// always redirect the request to the root vitest plugin since
11+
// it will be the one used to run Vitest
12+
const resolved = await ctx.server.pluginContainer.resolveId(id, undefined, {
13+
skip: new Set([plugin]),
14+
ssr,
1315
})
16+
return resolved
1417
}
1518
},
1619
}
20+
return plugin
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.defineProject = (c) => {
2+
return c
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('should not import from fake vitest')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "vitest",
3+
"type": "commonjs",
4+
"exports": {
5+
".": "./index.js",
6+
"./config": "./config.js"
7+
}
8+
}

test/workspaces/space_3/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
22
"name": "@vitest/space_3",
3-
"private": true
3+
"private": true,
4+
"scripts": {
5+
"test": "vitest"
6+
},
7+
"dependencies": {
8+
"vitest": "link:./fake-vitest"
9+
}
410
}

0 commit comments

Comments
 (0)