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(vitest): always resolve vitest to the root version #6369

Merged
merged 2 commits into from
Aug 20, 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
2 changes: 0 additions & 2 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
hijackVitePluginInject,
resolveFsAllow,
} from './utils'
import { VitestResolver } from './vitestResolver'
import { VitestOptimizer } from './optimizer'
import { NormalizeURLPlugin } from './normalizeURL'

Expand Down Expand Up @@ -256,7 +255,6 @@ export async function VitestPlugin(
CoverageTransform(ctx),
options.ui ? await UIPlugin() : null,
...MocksPlugins(),
VitestResolver(ctx),
VitestOptimizer(),
NormalizeURLPlugin(),
].filter(notNullish)
Expand Down
14 changes: 9 additions & 5 deletions packages/vitest/src/node/plugins/vitestResolver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { Plugin } from 'vite'
import { join } from 'pathe'
import type { Vitest } from '../core'

export function VitestResolver(ctx: Vitest): Plugin {
return {
const plugin: Plugin = {
name: 'vitest:resolve-root',
enforce: 'pre',
async resolveId(id) {
async resolveId(id, _, { ssr }) {
if (id === 'vitest' || id.startsWith('@vitest/')) {
return this.resolve(id, join(ctx.config.root, 'index.html'), {
skipSelf: true,
// always redirect the request to the root vitest plugin since
// it will be the one used to run Vitest
const resolved = await ctx.server.pluginContainer.resolveId(id, undefined, {
skip: new Set([plugin]),
ssr,
})
return resolved
}
},
}
return plugin
}
3 changes: 3 additions & 0 deletions test/workspaces/space_3/fake-vitest/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.defineProject = (c) => {
return c
}
1 change: 1 addition & 0 deletions test/workspaces/space_3/fake-vitest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('should not import from fake vitest')
8 changes: 8 additions & 0 deletions test/workspaces/space_3/fake-vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "vitest",
"type": "commonjs",
"exports": {
".": "./index.js",
"./config": "./config.js"
}
}
8 changes: 7 additions & 1 deletion test/workspaces/space_3/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"name": "@vitest/space_3",
"private": true
"private": true,
"scripts": {
"test": "vitest"
},
"dependencies": {
"vitest": "link:./fake-vitest"
}
}
Loading