Skip to content

Commit 43020e5

Browse files
committed
src: disable abort exceptions for module loading
1 parent 2f40652 commit 43020e5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/node_contextify.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,20 @@ 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 =
1456+
env->options()->abort_on_uncaught_exception;
1457+
// Validate that we're restoring the original value.
1458+
auto on_scope_leave =
1459+
OnScopeLeave([&env, &should_abort_on_uncaught_exception]() {
1460+
CHECK_EQ(env->options()->abort_on_uncaught_exception,
1461+
should_abort_on_uncaught_exception);
1462+
});
1463+
env->options()->abort_on_uncaught_exception = false;
1464+
14511465
// TODO(geoffreybooth): Centralize this rather than matching the logic in
14521466
// cjs/loader.js and translators.js
14531467
Local<String> script_id = String::Concat(
@@ -1491,6 +1505,10 @@ void ContextifyContext::ContainsModuleSyntax(
14911505
}
14921506
}
14931507
}
1508+
1509+
env->options()->abort_on_uncaught_exception =
1510+
should_abort_on_uncaught_exception;
1511+
14941512
args.GetReturnValue().Set(found_error_message_caused_by_module_syntax);
14951513
}
14961514

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+
'try { require("./package-type-module/esm.js") } catch (error) { console.log(error.code) }',
248+
], {
249+
cwd: fixtures.path('es-modules'),
250+
});
251+
252+
strictEqual(stderr, '');
253+
strictEqual(stdout.trim(), 'ERR_REQUIRE_ESM');
254+
strictEqual(code, 0);
255+
strictEqual(signal, null);
256+
});
257+
});

0 commit comments

Comments
 (0)