[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(vm): fix external module resolve error with deps optimizer query (#10024)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

authored by

Hiroshi Ogawa
Claude Sonnet 4.6
and committed by
GitHub
(Mar 30, 2026, 10:24 AM +0200) 9dbf4778 9d504ce8

+48 -33
+8
packages/utils/src/helpers.ts
··· 65 65 return url.replace(postfixRE, '') 66 66 } 67 67 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 + 68 76 const externalRE = /^(?:[a-z]+:)?\/\// 69 77 export const isExternalUrl = (url: string): boolean => externalRE.test(url) 70 78
+11 -27
test/cli/test/optimize-deps.test.ts
··· 1 1 import { expect, test } from 'vitest' 2 2 import { runVitest } from '../../test-utils' 3 3 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 + }) 28 11 29 - expect(stderr).toBe('') 30 - expect(errorTree()).toMatchInlineSnapshot(` 12 + expect(stderr).toBe('') 13 + expect(errorTree()).toMatchInlineSnapshot(` 31 14 { 32 15 "ssr.test.ts": { 33 16 "import.meta.url": "passed", ··· 37 20 }, 38 21 } 39 22 `) 40 - }) 23 + }, 24 + )
+5 -6
packages/vitest/src/runtime/external-executor.ts
··· 5 5 import fs from 'node:fs' 6 6 import { isBuiltin } from 'node:module' 7 7 import { fileURLToPath, pathToFileURL } from 'node:url' 8 - import { isBareImport } from '@vitest/utils/helpers' 8 + import { isBareImport, splitFileAndPostfix } from '@vitest/utils/helpers' 9 9 import { findNearestPackageData } from '@vitest/utils/resolver' 10 10 import { extname, normalize } from 'pathe' 11 11 import { CommonjsExecutor } from './vm/commonjs-executor' ··· 125 125 return { type: 'data', url: identifier, path: identifier } 126 126 } 127 127 128 - const extension = extname(identifier) 128 + const { file, postfix } = splitFileAndPostfix(identifier) 129 + const extension = extname(file) 129 130 if (extension === '.node' || isBuiltin(identifier)) { 130 131 return { type: 'builtin', url: identifier, path: identifier } 131 132 } ··· 138 139 } 139 140 140 141 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}` 145 144 146 145 let type: 'module' | 'commonjs' | 'vite' | 'wasm' 147 146 if (this.vite.canResolve(fileUrl)) {
+24
test/cli/fixtures/optimize-deps/vitest.config.ts
··· 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 + });