[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: correctly resolve vitest import if `inline: true` is set (#7856)

authored by

Vladimir and committed by
GitHub
(Apr 24, 2025, 6:20 PM +0200) a83f3bf6 8b2b61f6

+33 -14
+33 -14
packages/vitest/src/runtime/execute.ts
··· 91 91 }) 92 92 } 93 93 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 + 94 124 export async function startVitestExecutor(options: ContextExecutorOptions): Promise<VitestExecutor> { 95 125 const state = (): WorkerGlobalState => 96 126 // @ts-expect-error injected untyped global ··· 109 139 110 140 return await createVitestExecutor({ 111 141 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 126 145 } 127 146 128 147 const result = await rpc().fetch(id, getTransformMode())