From 163d76248c9dfded7d57ad012bee5fe3eed28a09 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 20 Aug 2024 13:46:04 +0200 Subject: [PATCH] fix(vitest): always resolve vitest to the root version (#6369) --- packages/vitest/src/node/plugins/index.ts | 2 -- packages/vitest/src/node/plugins/vitestResolver.ts | 14 +++++++++----- test/workspaces/space_3/fake-vitest/config.js | 3 +++ test/workspaces/space_3/fake-vitest/index.js | 1 + test/workspaces/space_3/fake-vitest/package.json | 8 ++++++++ test/workspaces/space_3/package.json | 8 +++++++- 6 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 test/workspaces/space_3/fake-vitest/config.js create mode 100644 test/workspaces/space_3/fake-vitest/index.js create mode 100644 test/workspaces/space_3/fake-vitest/package.json diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index e30d1c64cda4..a22215c852bf 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -21,7 +21,6 @@ import { hijackVitePluginInject, resolveFsAllow, } from './utils' -import { VitestResolver } from './vitestResolver' import { VitestOptimizer } from './optimizer' import { NormalizeURLPlugin } from './normalizeURL' @@ -256,7 +255,6 @@ export async function VitestPlugin( CoverageTransform(ctx), options.ui ? await UIPlugin() : null, ...MocksPlugins(), - VitestResolver(ctx), VitestOptimizer(), NormalizeURLPlugin(), ].filter(notNullish) diff --git a/packages/vitest/src/node/plugins/vitestResolver.ts b/packages/vitest/src/node/plugins/vitestResolver.ts index 01e73ca60146..7a18d2078b02 100644 --- a/packages/vitest/src/node/plugins/vitestResolver.ts +++ b/packages/vitest/src/node/plugins/vitestResolver.ts @@ -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 } diff --git a/test/workspaces/space_3/fake-vitest/config.js b/test/workspaces/space_3/fake-vitest/config.js new file mode 100644 index 000000000000..39c2c1c756e4 --- /dev/null +++ b/test/workspaces/space_3/fake-vitest/config.js @@ -0,0 +1,3 @@ +exports.defineProject = (c) => { + return c +} diff --git a/test/workspaces/space_3/fake-vitest/index.js b/test/workspaces/space_3/fake-vitest/index.js new file mode 100644 index 000000000000..7de26dd0a5b9 --- /dev/null +++ b/test/workspaces/space_3/fake-vitest/index.js @@ -0,0 +1 @@ +throw new Error('should not import from fake vitest') diff --git a/test/workspaces/space_3/fake-vitest/package.json b/test/workspaces/space_3/fake-vitest/package.json new file mode 100644 index 000000000000..ad967a55c712 --- /dev/null +++ b/test/workspaces/space_3/fake-vitest/package.json @@ -0,0 +1,8 @@ +{ + "name": "vitest", + "type": "commonjs", + "exports": { + ".": "./index.js", + "./config": "./config.js" + } +} diff --git a/test/workspaces/space_3/package.json b/test/workspaces/space_3/package.json index db61b2ef3abf..571df112b07b 100644 --- a/test/workspaces/space_3/package.json +++ b/test/workspaces/space_3/package.json @@ -1,4 +1,10 @@ { "name": "@vitest/space_3", - "private": true + "private": true, + "scripts": { + "test": "vitest" + }, + "dependencies": { + "vitest": "link:./fake-vitest" + } }