Skip to content

Commit ae34969

Browse files
committed
src: disable uncaught exception abortion for ESM syntax detection
1 parent 2f40652 commit ae34969

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/node_contextify.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,19 @@ void ContextifyContext::ContainsModuleSyntax(
14481448
code = args[0].As<String>();
14491449
}
14501450

1451+
// We need to temporarily disable aborting on uncaught exceptions because
1452+
// ScriptCompiler::CompileFunction will abort if it encounters an uncaught
1453+
// exception. This can be reproduced with `--abort-on-uncaught-exception`
1454+
// flag.
1455+
bool should_abort_on_uncaught_exception = env->abort_on_uncaught_exception();
1456+
// Validate that we're restoring the original value.
1457+
auto on_scope_leave =
1458+
OnScopeLeave([&env, &should_abort_on_uncaught_exception]() {
1459+
CHECK_EQ(env->abort_on_uncaught_exception(),
1460+
should_abort_on_uncaught_exception);
1461+
});
1462+
env->set_abort_on_uncaught_exception(false);
1463+
14511464
// TODO(geoffreybooth): Centralize this rather than matching the logic in
14521465
// cjs/loader.js and translators.js
14531466
Local<String> script_id = String::Concat(
@@ -1491,6 +1504,10 @@ void ContextifyContext::ContainsModuleSyntax(
14911504
}
14921505
}
14931506
}
1507+
1508+
// Revert the abort on uncaught exception setting.
1509+
env->set_abort_on_uncaught_exception(should_abort_on_uncaught_exception);
1510+
14941511
args.GetReturnValue().Set(found_error_message_caused_by_module_syntax);
14951512
}
14961513

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,23 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
235235
}
236236
});
237237
});
238+
239+
// Validate temporarily disabling `--abort-on-uncaught-exception`
240+
// while running `containsModuleSyntax`.
241+
// Ref: https://github.com/nodejs/node/issues/50878
242+
describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-exception`', () => {
243+
it('should work', async () => {
244+
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
245+
'--abort-on-uncaught-exception',
246+
'--eval',
247+
'assert.throws(() => require("./package-type-module/esm.js"), { code: "ERR_REQUIRE_ESM" })',
248+
], {
249+
cwd: fixtures.path('es-modules'),
250+
});
251+
252+
strictEqual(stderr, '');
253+
strictEqual(stdout, '');
254+
strictEqual(code, 0);
255+
strictEqual(signal, null);
256+
});
257+
});

0 commit comments

Comments
 (0)