Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw an error if Vite wasn't able to resolve aliased path #4503

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test:unit": "vitest",
"test": "vitest",
"lint": "prettier --plugin-search-dir . --check .",
"format": "prettier --plugin-search-dir . --write ."
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"simple-git-hooks": "^2.9.0",
"tsx": "^4.1.1",
"typescript": "^5.2.2",
"vite": "^5.0.0-beta.15",
"vite": "^5.0.0-beta.19",
"vitest": "workspace:*"
},
"pnpm": {
Expand Down
29 changes: 12 additions & 17 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
import vm from 'node:vm'
import { resolve } from 'pathe'
import createDebug from 'debug'
import { VALID_ID_PREFIX, cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import { cleanUrl, createImportMetaEnvProxy, isInternalRequest, isNodeBuiltin, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
import { extractSourceMap } from './source-map'

Expand Down Expand Up @@ -221,29 +221,24 @@ export class ViteNodeRunner {
}

private async _resolveUrl(id: string, importer?: string): Promise<[url: string, fsPath: string]> {
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
// should be checked before normalization, because it removes this prefix
// TODO: this is a hack, we should find a better way to handle this
if (importer && id.startsWith(VALID_ID_PREFIX))
importer = undefined
const dep = normalizeRequestId(id, this.options.base)
if (!this.shouldResolveId(dep))
return [dep, dep]
const { path, exists } = toFilePath(dep, this.root)
if (!this.options.resolveId || exists)
return [dep, path]
const resolved = await this.options.resolveId(dep, importer)
// TODO: we need to better handle module resolution when different urls point to the same module
// if (!resolved) {
// const error = new Error(
// `Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
// + '\n\n- If you rely on tsconfig.json\'s "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
// + '\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',
// )
// Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
// Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id: dep, importer }, enumerable: false })
// throw error
// }
// supported since Vite 5-beta.19
if (resolved?.meta?.['vite:alias']?.noResolved) {
const error = new Error(
`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
+ '\n\n- If you rely on tsconfig.json\'s "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
+ '\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',
)
Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id: dep, importer }, enumerable: false })
throw error
}
const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep
return [resolvedId, resolvedId]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"strip-literal": "^1.3.0",
"tinybench": "^2.5.1",
"tinypool": "^0.8.1",
"vite": "^5.0.0-beta.15 || ^5.0.0",
"vite": "^5.0.0-beta.19 || ^5.0.0",
"vite-node": "workspace:*",
"why-is-node-running": "^2.2.2"
},
Expand Down
Loading
Loading