From 709ac479eba30d1e218fbd07e2081b041fd63435 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 3 Dec 2023 18:45:57 -0500 Subject: [PATCH] src: disable uncaught exception abortion for ESM syntax detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/50987 Reviewed-By: Geoffrey Booth Reviewed-By: Antoine du Hamel Reviewed-By: Michaƫl Zasso Reviewed-By: Jacob Smith --- src/node_contextify.cc | 1 + test/es-module/test-esm-detect-ambiguous.mjs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ec0f68671301d0..85cdd4b3e74163 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1468,6 +1468,7 @@ void ContextifyContext::ContainsModuleSyntax( String::NewFromUtf8(isolate, "__dirname").ToLocalChecked()}; TryCatchScope try_catch(env); + ShouldNotAbortOnUncaughtScope no_abort_scope(env); ContextifyContext::CompileFunctionAndCacheResult(env, context, diff --git a/test/es-module/test-esm-detect-ambiguous.mjs b/test/es-module/test-esm-detect-ambiguous.mjs index 34c5f17f007c8f..43537f26f5304a 100644 --- a/test/es-module/test-esm-detect-ambiguous.mjs +++ b/test/es-module/test-esm-detect-ambiguous.mjs @@ -235,3 +235,23 @@ describe('--experimental-detect-module', { concurrency: true }, () => { } }); }); + +// Validate temporarily disabling `--abort-on-uncaught-exception` +// while running `containsModuleSyntax`. +// Ref: https://github.com/nodejs/node/issues/50878 +describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-exception`', () => { + it('should work', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--abort-on-uncaught-exception', + '--eval', + 'assert.throws(() => require("./package-type-module/esm.js"), { code: "ERR_REQUIRE_ESM" })', + ], { + cwd: fixtures.path('es-modules'), + }); + + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 0); + strictEqual(signal, null); + }); +});