Skip to content

Commit

Permalink
src: disable abort exceptions for module loading
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Dec 1, 2023
1 parent 2f40652 commit 7e81ca2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,20 @@ void ContextifyContext::ContainsModuleSyntax(
code = args[0].As<String>();
}

// We need to temporarily disable aborting on uncaught exceptions because
// ScriptCompiler::CompileFunction will abort if it encounters an uncaught
// exception. This can be reproduced with `--abort-on-uncaught-exception`
// flag.
bool should_abort_on_uncaught_exception =
env->options()->abort_on_uncaught_exception;
// Validate that we're restoring the original value.
auto on_scope_leave =
OnScopeLeave([&env, &should_abort_on_uncaught_exception]() {
DCHECK_EQ(env->options()->abort_on_uncaught_exception,
should_abort_on_uncaught_exception);
});
env->options()->abort_on_uncaught_exception = false;

// TODO(geoffreybooth): Centralize this rather than matching the logic in
// cjs/loader.js and translators.js
Local<String> script_id = String::Concat(
Expand Down Expand Up @@ -1491,6 +1505,10 @@ void ContextifyContext::ContainsModuleSyntax(
}
}
}

env->options()->abort_on_uncaught_exception =
should_abort_on_uncaught_exception;

args.GetReturnValue().Set(found_error_message_caused_by_module_syntax);
}

Expand Down
18 changes: 18 additions & 0 deletions test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,21 @@ 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('ESM: importing ESM with --abort-on-uncaught-exception', () => {
it('should work', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--abort-on-uncaught-exception',
'--eval',
`try { require('${fixtures.path('es-modules/package-type-module/esm.js')}') } catch (error) { console.log(error.code) }`,
]);

strictEqual(stderr, '');
strictEqual(stdout.trim(), 'ERR_REQUIRE_ESM');
strictEqual(code, 0);
strictEqual(signal, null);
});
});

0 comments on commit 7e81ca2

Please sign in to comment.