Skip to content

Commit 9dbf477

Browse files
hi-ogawaclaude
andauthored
fix(vm): fix external module resolve error with deps optimizer query (#10024)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9d504ce commit 9dbf477

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

packages/utils/src/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export function cleanUrl(url: string): string {
6565
return url.replace(postfixRE, '')
6666
}
6767

68+
export function splitFileAndPostfix(path: string): {
69+
file: string
70+
postfix: string
71+
} {
72+
const file = cleanUrl(path)
73+
return { file, postfix: path.slice(file.length) }
74+
}
75+
6876
const externalRE = /^(?:[a-z]+:)?\/\//
6977
export const isExternalUrl = (url: string): boolean => externalRE.test(url)
7078

packages/vitest/src/runtime/external-executor.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { VMModule } from './vm/types'
55
import fs from 'node:fs'
66
import { isBuiltin } from 'node:module'
77
import { fileURLToPath, pathToFileURL } from 'node:url'
8-
import { isBareImport } from '@vitest/utils/helpers'
8+
import { isBareImport, splitFileAndPostfix } from '@vitest/utils/helpers'
99
import { findNearestPackageData } from '@vitest/utils/resolver'
1010
import { extname, normalize } from 'pathe'
1111
import { CommonjsExecutor } from './vm/commonjs-executor'
@@ -125,7 +125,8 @@ export class ExternalModulesExecutor {
125125
return { type: 'data', url: identifier, path: identifier }
126126
}
127127

128-
const extension = extname(identifier)
128+
const { file, postfix } = splitFileAndPostfix(identifier)
129+
const extension = extname(file)
129130
if (extension === '.node' || isBuiltin(identifier)) {
130131
return { type: 'builtin', url: identifier, path: identifier }
131132
}
@@ -138,10 +139,8 @@ export class ExternalModulesExecutor {
138139
}
139140

140141
const isFileUrl = identifier.startsWith('file://')
141-
const pathUrl = isFileUrl
142-
? fileURLToPath(identifier.split('?')[0])
143-
: identifier
144-
const fileUrl = isFileUrl ? identifier : pathToFileURL(pathUrl).toString()
142+
const pathUrl = isFileUrl ? fileURLToPath(file) : file
143+
const fileUrl = isFileUrl ? identifier : `${pathToFileURL(file)}${postfix}`
145144

146145
let type: 'module' | 'commonjs' | 'vite' | 'wasm'
147146
if (this.vite.canResolve(fileUrl)) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
optimizeDeps: {
5+
include: ["@test/test-dep-url"],
6+
},
7+
ssr: {
8+
optimizeDeps: {
9+
include: ["@test/test-dep-url"],
10+
},
11+
},
12+
test: {
13+
deps: {
14+
optimizer: {
15+
client: {
16+
enabled: true,
17+
},
18+
ssr: {
19+
enabled: true,
20+
},
21+
},
22+
},
23+
},
24+
});
Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
import { expect, test } from 'vitest'
22
import { runVitest } from '../../test-utils'
33

4-
test('optimize deps optimizes them into node_modules/.vite', async () => {
5-
const { errorTree, stderr } = await runVitest({
6-
root: './fixtures/optimize-deps',
7-
deps: {
8-
optimizer: {
9-
client: {
10-
enabled: true,
11-
},
12-
ssr: {
13-
enabled: true,
14-
},
15-
},
16-
},
17-
$viteConfig: {
18-
optimizeDeps: {
19-
include: ['@test/test-dep-url'],
20-
},
21-
ssr: {
22-
optimizeDeps: {
23-
include: ['@test/test-dep-url'],
24-
},
25-
},
26-
},
27-
})
4+
test.for(['forks', 'threads', 'vmThreads', 'vmForks'])(
5+
'optimize deps optimizes them into node_modules/.vite - %s',
6+
async (pool) => {
7+
const { errorTree, stderr } = await runVitest({
8+
root: './fixtures/optimize-deps',
9+
pool,
10+
})
2811

29-
expect(stderr).toBe('')
30-
expect(errorTree()).toMatchInlineSnapshot(`
12+
expect(stderr).toBe('')
13+
expect(errorTree()).toMatchInlineSnapshot(`
3114
{
3215
"ssr.test.ts": {
3316
"import.meta.url": "passed",
@@ -37,4 +20,5 @@ test('optimize deps optimizes them into node_modules/.vite', async () => {
3720
},
3821
}
3922
`)
40-
})
23+
},
24+
)

0 commit comments

Comments
 (0)