From 7b46b20947e5a818d1caf2cfc80738be089bdc7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 2 Nov 2019 12:10:53 +0100 Subject: [PATCH] esm: exit the process with an error if loader has an issue Previously, this would trigger an unhandled rejection that the user cannot handle. Fixes: https://github.com/nodejs/node/issues/30205 PR-URL: https://github.com/nodejs/node/pull/30219 Reviewed-By: Guy Bedford Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Yongsheng Zhang --- lib/internal/bootstrap/pre_execution.js | 20 ++++++++++---------- test/message/esm_loader_not_found.mjs | 3 +++ test/message/esm_loader_not_found.out | 18 ++++++++++++++++++ test/message/esm_loader_syntax_error.mjs | 3 +++ test/message/esm_loader_syntax_error.out | 9 +++++++++ 5 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 test/message/esm_loader_not_found.mjs create mode 100644 test/message/esm_loader_not_found.out create mode 100644 test/message/esm_loader_syntax_error.mjs create mode 100644 test/message/esm_loader_syntax_error.out diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 1c7fedb0b273dd..5e7095a75f63f5 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -480,16 +480,16 @@ function runMainESM(mainPath) { return esmLoader.initializeLoader().then(() => { const main = path.isAbsolute(mainPath) ? pathToFileURL(mainPath).href : mainPath; - return esmLoader.ESMLoader.import(main).catch((e) => { - if (hasUncaughtExceptionCaptureCallback()) { - process._fatalException(e); - return; - } - internalBinding('errors').triggerUncaughtException( - e, - true /* fromPromise */ - ); - }); + return esmLoader.ESMLoader.import(main); + }).catch((e) => { + if (hasUncaughtExceptionCaptureCallback()) { + process._fatalException(e); + return; + } + internalBinding('errors').triggerUncaughtException( + e, + true /* fromPromise */ + ); }); } diff --git a/test/message/esm_loader_not_found.mjs b/test/message/esm_loader_not_found.mjs new file mode 100644 index 00000000000000..fd6f29ac531507 --- /dev/null +++ b/test/message/esm_loader_not_found.mjs @@ -0,0 +1,3 @@ +// Flags: --experimental-modules --experimental-loader i-dont-exist +import '../common/index.mjs'; +console.log('This should not be printed'); diff --git a/test/message/esm_loader_not_found.out b/test/message/esm_loader_not_found.out new file mode 100644 index 00000000000000..87161cdff5627c --- /dev/null +++ b/test/message/esm_loader_not_found.out @@ -0,0 +1,18 @@ +(node:*) ExperimentalWarning: The ESM module loader is experimental. +(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time +internal/modules/esm/default_resolve.js:* + let url = moduleWrapResolve(specifier, parentURL); + ^ + +Error: Cannot find package 'i-dont-exist' imported from * + at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:*:*) + at Loader.resolve (internal/modules/esm/loader.js:*:*) + at Loader.getModuleJob (internal/modules/esm/loader.js:*:*) + at Loader.import (internal/modules/esm/loader.js:*:*) + at internal/process/esm_loader.js:*:* + at Object.initializeLoader (internal/process/esm_loader.js:*:*) + at runMainESM (internal/bootstrap/pre_execution.js:*:*) + at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) + at internal/main/run_main_module.js:*:* { + code: 'ERR_MODULE_NOT_FOUND' +} diff --git a/test/message/esm_loader_syntax_error.mjs b/test/message/esm_loader_syntax_error.mjs new file mode 100644 index 00000000000000..7caad4056e08d2 --- /dev/null +++ b/test/message/esm_loader_syntax_error.mjs @@ -0,0 +1,3 @@ +// Flags: --experimental-modules --experimental-loader ./test/fixtures/es-module-loaders/syntax-error.mjs +import '../common/index.mjs'; +console.log('This should not be printed'); diff --git a/test/message/esm_loader_syntax_error.out b/test/message/esm_loader_syntax_error.out new file mode 100644 index 00000000000000..9767a9c86c6bc2 --- /dev/null +++ b/test/message/esm_loader_syntax_error.out @@ -0,0 +1,9 @@ +(node:*) ExperimentalWarning: The ESM module loader is experimental. +(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time +file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2 +await async () => 0; +^^^^^ + +SyntaxError: Unexpected reserved word + at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*) + at async link (internal/modules/esm/module_job.js:*:*)