diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index 50c058328a5c..c351b4ecffff 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -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 */ @@ -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 `:/` on windows: see nodejs/node#31710 const url = pathToFileURL(file).href const meta = { url } diff --git a/test/core/test/imports.test.ts b/test/core/test/imports.test.ts index 389e0d869093..f803eb01a7a0 100644 --- a/test/core/test/imports.test.ts +++ b/test/core/test/imports.test.ts @@ -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/) +})