Skip to content

Commit

Permalink
fix(vitest): throw an error if Vite wasn't able to resolve aliased pa…
Browse files Browse the repository at this point in the history
…th (#4503)
  • Loading branch information
sheremet-va authored Nov 15, 2023
1 parent fb84885 commit 503331d
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 365 deletions.
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

0 comments on commit 503331d

Please sign in to comment.