Skip to content

Commit 503331d

Browse files
authored
fix(vitest): throw an error if Vite wasn't able to resolve aliased path (#4503)
1 parent fb84885 commit 503331d

File tree

8 files changed

+222
-365
lines changed

8 files changed

+222
-365
lines changed

examples/sveltekit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"preview": "vite preview",
1010
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
1111
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12-
"test:unit": "vitest",
12+
"test": "vitest",
1313
"lint": "prettier --plugin-search-dir . --check .",
1414
"format": "prettier --plugin-search-dir . --write ."
1515
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"simple-git-hooks": "^2.9.0",
6666
"tsx": "^4.1.1",
6767
"typescript": "^5.2.2",
68-
"vite": "^5.0.0-beta.15",
68+
"vite": "^5.0.0-beta.19",
6969
"vitest": "workspace:*"
7070
},
7171
"pnpm": {

packages/vite-node/src/client.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
66
import vm from 'node:vm'
77
import { resolve } from 'pathe'
88
import createDebug from 'debug'
9-
import { VALID_ID_PREFIX, cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
9+
import { cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
1010
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
1111
import { extractSourceMap } from './source-map'
1212

@@ -221,29 +221,24 @@ export class ViteNodeRunner {
221221
}
222222

223223
private async _resolveUrl(id: string, importer?: string): Promise<[url: string, fsPath: string]> {
224-
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
225-
// should be checked before normalization, because it removes this prefix
226-
// TODO: this is a hack, we should find a better way to handle this
227-
if (importer && id.startsWith(VALID_ID_PREFIX))
228-
importer = undefined
229224
const dep = normalizeRequestId(id, this.options.base)
230225
if (!this.shouldResolveId(dep))
231226
return [dep, dep]
232227
const { path, exists } = toFilePath(dep, this.root)
233228
if (!this.options.resolveId || exists)
234229
return [dep, path]
235230
const resolved = await this.options.resolveId(dep, importer)
236-
// TODO: we need to better handle module resolution when different urls point to the same module
237-
// if (!resolved) {
238-
// const error = new Error(
239-
// `Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
240-
// + '\n\n- If you rely on tsconfig.json\'s "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
241-
// + '\n- Make sure you don\'t have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors',
242-
// )
243-
// Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
244-
// Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id: dep, importer }, enumerable: false })
245-
// throw error
246-
// }
231+
// supported since Vite 5-beta.19
232+
if (resolved?.meta?.['vite:alias']?.noResolved) {
233+
const error = new Error(
234+
`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
235+
+ '\n\n- If you rely on tsconfig.json\'s "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
236+
+ '\n- Make sure you don\'t have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors',
237+
)
238+
Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
239+
Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id: dep, importer }, enumerable: false })
240+
throw error
241+
}
247242
const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep
248243
return [resolvedId, resolvedId]
249244
}

packages/vitest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
"strip-literal": "^1.3.0",
153153
"tinybench": "^2.5.1",
154154
"tinypool": "^0.8.1",
155-
"vite": "^5.0.0-beta.15 || ^5.0.0",
155+
"vite": "^5.0.0-beta.19 || ^5.0.0",
156156
"vite-node": "workspace:*",
157157
"why-is-node-running": "^2.2.2"
158158
},

0 commit comments

Comments
 (0)