Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous exceptions in ES modules are always 'caught' #50430

Open
connor4312 opened this issue Oct 27, 2023 · 9 comments
Open

Synchronous exceptions in ES modules are always 'caught' #50430

connor4312 opened this issue Oct 27, 2023 · 9 comments
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. inspector Issues and PRs related to the V8 inspector protocol v8 engine Issues and PRs related to the V8 dependency.

Comments

@connor4312
Copy link
Contributor

connor4312 commented Oct 27, 2023

Version

20.3.1

Platform

Darwin mbp.peet.io 22.6.0 Darwin Kernel Version 22.6.0: Fri Sep 15 13:39:52 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_X86_64 x86_64

Subsystem

inspector

What steps will reproduce the bug?

  1. Start a program containing throw new Error('foo'); in an ES module
  2. Attach a debugger, like devtools or VS Code's debugger, and enable "pause on uncaught exceptions"
  3. Run the program

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior? Why is that the expected behavior?

The runtime should pause on the throw line

What do you see instead?

The runtime does not pause and the program exits

Additional information

First reported in microsoft/vscode-js-debug#1861

Guessing this is because module evaluation is in a try/catch. Wonder if there's any machinery that can be used to mark containing exceptions as uncaught

try {
await this.module.evaluate(timeout, breakOnSigint);
} catch (e) {

@connor4312 connor4312 changed the title Exceptions in ES modules are always caught Synchronous exceptions in ES modules are always 'caught' Oct 27, 2023
@starball5
Copy link

starball5 commented Oct 27, 2023

Related on Stack Overflow: Why doesn't the VS Code JS Debugger pause on uncaught exceptions in ES modules in NodeJS?.

Is this problem limited to NodeJS v20? Or does it go back to older versions as well?

@bnoordhuis bnoordhuis added inspector Issues and PRs related to the V8 inspector protocol esm Issues and PRs related to the ECMAScript Modules implementation. labels Oct 28, 2023
@targos
Copy link
Member

targos commented Oct 28, 2023

@nodejs/loaders

@GeoffreyBooth
Copy link
Member

GeoffreyBooth commented Oct 30, 2023

The initial version of #50096 involved updating v8 compileFunction to add a parameter that meant “in case of exception, return the exception rather than throwing it.” Maybe we could revive that code and use it here, so that the referenced module_job.js wouldn’t need a try/catch at all; if something is returned, it’s an exception, and then we decorate it with the extra “common mistakes people make” stuff you see just below these cited lines; and then throw it.

I’m not sure that would actually fix the issue, though. In particular, why isn’t the debugger breaking on the line where the exception is re-thrown:

Is it because it’s in the ignorelist?

@Megabyteceer
Copy link

Megabyteceer commented Dec 12, 2023

I faced the same problem in node+ts project. I solved it by set "target": "ES6", tsconfig.json. (at least in debugging config). There is few restrictions (as no root await) raised, But for me it is less problematic.

@twilson90
Copy link

twilson90 commented Jun 14, 2024

Is there any update on this?
I'm mystified why this doesn't appear to be an issue for more people.
I use node mostly as a private utility, I don't want to use typescript or a compiler on my script before execution, I just want to run some esm js and debug it.

But when I try to debug an esm script with vs code debugger it still doesn't pause on errors.
I'm still having to use:

process.on("uncaughtException", (e)=>{
  console.trace(e);
})

And it's just a pain.

@Megabyteceer
Copy link

Megabyteceer commented Jun 14, 2024

There are similar problems in other places too. As example some time ago preact had no try catch wrappers in event handlers and it was easy to debug your app. But later they added try catch blocks and development some time become to hell. I think problems like that should be handled as it was in flash. Debug player for development , where all errors crash as early as possible, and release player for productoin where errors handling are enabled.

@connor4312
Copy link
Contributor Author

connor4312 commented Aug 30, 2024

This also affects rejections from top-level await in ES modules and continues to reproduce on Node 22.7.0.

@btj
Copy link

btj commented Oct 31, 2024

This has cost me a lot of wasted time as well. A workaround seems to be to run your top-level code inside process.nextTick(async () => { ... });.

@legendecas
Copy link
Member

legendecas commented Feb 16, 2025

All module evaluations are asynchronous even if they could be evaluated synchronously. There are shortcuts in require(esm) but it is still achieved with promises.

This is rather a V8/inspector issue that V8 inspector only supports promiseRejection debugger pause reason in an async function.

This can also be reproduced in Chrome where a uncaught module top level exception can not be paused in the Chrome DevTools.

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"></head>
<body></body>

<script type="module">
  // This does not pause even with "Pause on uncaught exception" in Chrome DevTools
  throw new Error('foobar');
</script>

</html>

If V8 supports promiseRejection debugger pause reason in a module body, this issue on both platforms could be fixed.

Upstream issue: https://issues.chromium.org/issues/397130621

@legendecas legendecas added v8 engine Issues and PRs related to the V8 dependency. confirmed-bug Issues with confirmed bugs. labels Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. inspector Issues and PRs related to the V8 inspector protocol v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

No branches or pull requests

9 participants