Skip to content

Commit a83f3bf

Browse files
authored
fix: correctly resolve vitest import if inline: true is set (#7856)
1 parent 8b2b61f commit a83f3bf

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

packages/vitest/src/runtime/execute.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ function listenForErrors(state: () => WorkerGlobalState) {
9191
})
9292
}
9393

94+
const relativeIds: Record<string, string> = {}
95+
96+
function getVitestImport(id: string, state: () => WorkerGlobalState) {
97+
if (externalizeMap.has(id)) {
98+
return { externalize: externalizeMap.get(id)! }
99+
}
100+
// always externalize Vitest because we import from there before running tests
101+
// so we already have it cached by Node.js
102+
const root = state().config.root
103+
const relativeRoot = relativeIds[root] ?? (relativeIds[root] = normalizedDistDir.slice(root.length))
104+
if (
105+
// full dist path
106+
id.includes(distDir)
107+
|| id.includes(normalizedDistDir)
108+
// "relative" to root path:
109+
// /node_modules/.pnpm/vitest/dist
110+
|| (relativeRoot && relativeRoot !== '/' && id.startsWith(relativeRoot))
111+
) {
112+
const { path } = toFilePath(id, root)
113+
const externalize = pathToFileURL(path).toString()
114+
externalizeMap.set(id, externalize)
115+
return { externalize }
116+
}
117+
if (bareVitestRegexp.test(id)) {
118+
externalizeMap.set(id, id)
119+
return { externalize: id }
120+
}
121+
return null
122+
}
123+
94124
export async function startVitestExecutor(options: ContextExecutorOptions): Promise<VitestExecutor> {
95125
const state = (): WorkerGlobalState =>
96126
// @ts-expect-error injected untyped global
@@ -109,20 +139,9 @@ export async function startVitestExecutor(options: ContextExecutorOptions): Prom
109139

110140
return await createVitestExecutor({
111141
async fetchModule(id) {
112-
if (externalizeMap.has(id)) {
113-
return { externalize: externalizeMap.get(id)! }
114-
}
115-
// always externalize Vitest because we import from there before running tests
116-
// so we already have it cached by Node.js
117-
if (id.includes(distDir) || id.includes(normalizedDistDir)) {
118-
const { path } = toFilePath(id, state().config.root)
119-
const externalize = pathToFileURL(path).toString()
120-
externalizeMap.set(id, externalize)
121-
return { externalize }
122-
}
123-
if (bareVitestRegexp.test(id)) {
124-
externalizeMap.set(id, id)
125-
return { externalize: id }
142+
const vitest = getVitestImport(id, state)
143+
if (vitest) {
144+
return vitest
126145
}
127146

128147
const result = await rpc().fetch(id, getTransformMode())

0 commit comments

Comments
 (0)