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: don't throw unhandled error, if import was caught #2494

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/vite-node/src/client.ts
Expand Up @@ -180,11 +180,12 @@ export class ViteNodeRunner {
const promise = this.directRequest(id, fsPath, callstack)
Object.assign(mod, { promise, evaluated: false })

promise.finally(() => {
try {
return await promise
}
finally {
mod.evaluated = true
})

return await promise
}
}

/** @internal */
Expand Down Expand Up @@ -272,7 +273,7 @@ export class ViteNodeRunner {
throw new Error(`[vite-node] Failed to load ${id}`)

const file = cleanUrl(resolvedId || fsPath)
// console.log('file', file)

// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const url = pathToFileURL(file).href
const meta = { url }
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/imports.test.ts
Expand Up @@ -53,3 +53,11 @@ test('dynamic import has null prototype', async () => {

expect(Object.getPrototypeOf(stringTimeoutMod)).toBe(null)
})

test('dynamic import throws an error', async () => {
const path = './some-unknown-path'
const imported = import(path)
await expect(imported).rejects.toThrowError(/Failed to load/)
// @ts-expect-error path does not exist
await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/)
})