From 404c5daf4b38f0772929141cf9121c4756ffea54 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 12 Dec 2022 12:49:20 +0100 Subject: [PATCH 1/2] fix: don't throw unhandled error, if import was caught --- packages/vite-node/src/client.ts | 11 ++++++----- test/core/test/imports.test.ts | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) 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..23d0df0664ee 100644 --- a/test/core/test/imports.test.ts +++ b/test/core/test/imports.test.ts @@ -53,3 +53,9 @@ test('dynamic import has null prototype', async () => { expect(Object.getPrototypeOf(stringTimeoutMod)).toBe(null) }) + +test('dynamic imports throws an error', async () => { + const path = './some-unknown-path' + const imported = import(path) + await expect(imported).rejects.toThrowError(/Failed to load/) +}) From 409f63eb12af93893b95a273b72a86c54a082c06 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 12 Dec 2022 12:52:53 +0100 Subject: [PATCH 2/2] chore: add more tests --- test/core/test/imports.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/core/test/imports.test.ts b/test/core/test/imports.test.ts index 23d0df0664ee..f803eb01a7a0 100644 --- a/test/core/test/imports.test.ts +++ b/test/core/test/imports.test.ts @@ -54,8 +54,10 @@ test('dynamic import has null prototype', async () => { expect(Object.getPrototypeOf(stringTimeoutMod)).toBe(null) }) -test('dynamic imports throws an error', async () => { +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/) })