Skip to content

Commit b426c19

Browse files
authored
fix: prevent false Vitest import resolution (#11196)
1 parent 99fc525 commit b426c19

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎packages/vitest/src/runtime/moduleRunner/cachedResolver.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const normalizedDistDir = normalize(distDir)
99
const relativeIds: Record<string, string> = {}
1010
const externalizeMap = new Map<string, string>()
1111

12+
function getRelativeDistDir(root: string): string {
13+
const normalizedRoot = normalize(root).replace(/\/+$/, '') || '/'
14+
const rootPrefix = normalizedRoot === '/' ? normalizedRoot : `${normalizedRoot}/`
15+
return normalizedDistDir.startsWith(rootPrefix)
16+
? normalizedDistDir.slice(normalizedRoot.length)
17+
: ''
18+
}
19+
1220
// all Vitest imports always need to be externalized
1321
export function getCachedVitestImport(
1422
id: string,
@@ -24,7 +32,7 @@ export function getCachedVitestImport(
2432
// always externalize Vitest because we import from there before running tests
2533
// so we already have it cached by Node.js
2634
const root = state().config.root
27-
const relativeRoot = relativeIds[root] ?? (relativeIds[root] = normalizedDistDir.slice(root.length))
35+
const relativeRoot = relativeIds[root] ?? (relativeIds[root] = getRelativeDistDir(root))
2836
if (id.includes(distDir) || id.includes(normalizedDistDir)) {
2937
const { file, postfix } = splitFileAndPostfix(id)
3038
const externalize = id.startsWith('file://')
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { WorkerGlobalState } from '../../../packages/vitest/src/types/worker'
2+
import { pathToFileURL } from 'node:url'
3+
import { dirname, normalize } from 'pathe'
4+
import { describe, expect, test } from 'vitest'
5+
import { distDir } from '../../../packages/vitest/src/paths'
6+
import { getCachedVitestImport } from '../../../packages/vitest/src/runtime/moduleRunner/cachedResolver'
7+
8+
const normalizedDistDir = normalize(distDir)
9+
10+
function stateWithRoot(root: string): () => WorkerGlobalState {
11+
return () => ({ config: { root } }) as WorkerGlobalState
12+
}
13+
14+
function unrelatedRootForSuffix(suffix: string, fill: string): string {
15+
return `/${fill.repeat(normalizedDistDir.length - suffix.length - 1)}`
16+
}
17+
18+
describe('getCachedVitestImport', () => {
19+
test('does not resolve a bare builtin relative to an unrelated root', () => {
20+
const root = unrelatedRootForSuffix('t', 'a')
21+
expect(normalizedDistDir.slice(root.length)).toBe('t')
22+
23+
expect(getCachedVitestImport('timers', stateWithRoot(root))).toBeNull()
24+
})
25+
26+
test('does not resolve a builtin subpath relative to an unrelated root', () => {
27+
const root = unrelatedRootForSuffix('st', 'b')
28+
expect(normalizedDistDir.slice(root.length)).toBe('st')
29+
30+
expect(getCachedVitestImport('stream/promises', stateWithRoot(root))).toBeNull()
31+
})
32+
33+
test('does not derive a relative path from a sibling root prefix', () => {
34+
const root = normalizedDistDir.slice(0, -'st/dist'.length)
35+
expect(normalizedDistDir.slice(root.length)).toBe('st/dist')
36+
37+
expect(getCachedVitestImport('st/dist/index.js', stateWithRoot(root))).toBeNull()
38+
})
39+
40+
test('resolves a dist path relative to an ancestor root', () => {
41+
const root = dirname(normalizedDistDir)
42+
const id = `${normalizedDistDir.slice(root.length)}/index.js?relative`
43+
44+
expect(getCachedVitestImport(id, stateWithRoot(root))).toEqual({
45+
externalize: `${pathToFileURL(`${normalizedDistDir}/index.js`)}?relative`,
46+
type: 'module',
47+
})
48+
})
49+
50+
test('still resolves absolute and bare Vitest imports', () => {
51+
const root = unrelatedRootForSuffix('test', 'c')
52+
const id = `${normalizedDistDir}/index.js?absolute`
53+
54+
expect(getCachedVitestImport(id, stateWithRoot(root))).toEqual({
55+
externalize: `${pathToFileURL(`${normalizedDistDir}/index.js`)}?absolute`,
56+
type: 'module',
57+
})
58+
expect(getCachedVitestImport('vitest/config', stateWithRoot(root))).toEqual({
59+
externalize: 'vitest/config',
60+
type: 'module',
61+
})
62+
})
63+
})

0 commit comments

Comments
 (0)