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

require() of malformed script uncatchably crashes the process #493

Closed
4 of 6 tasks
broofa opened this issue Mar 12, 2024 · 6 comments
Closed
4 of 6 tasks

require() of malformed script uncatchably crashes the process #493

broofa opened this issue Mar 12, 2024 · 6 comments
Labels
bug Something isn't working pr welcome

Comments

@broofa
Copy link

broofa commented Mar 12, 2024

Acknowledgements

  • I searched existing issues before opening this one to avoid duplicates
  • I understand this is not a place for seek help, but to report a bug
  • I understand that the bug must be proven first with a minimal reproduction
  • I will be polite, respectful, and considerate of people's time and effort

Minimal reproduction URL

https://stackblitz.com/edit/stackblitz-starters-xedxss?file=load.mjs

Version

4.7.1

Node.js version

18.18.0

Package manager

npm

Operating system

Linux

Problem & Expected behavior

  • What happened?

See the attached stackblitz MRE.

Basically I'm trying to require() user-supplied code that may be malformed. This causes the tsx process to die immediately. There appears to be no way of preventing this. (try-catch, uncaughtException event, and unhandledPromise event are all ineffective.)

You can see this by running npm start:

❯ npm start

> start
> tsx load.mjs

STARTING ...

[esbuild Error]: Expected "}" but found end of file
    at /home/projects/stackblitz-starters-xedxss/foo.js:1:67

~/projects/stackblitz-starters-xedxss
❯ 
  • What did you expect instead?

require()'ing a module should behave similar to how running this directly in node works. E.g. node load.mjs produces the following...

❯ node load.mjs
STARTING ...

CATCH Unexpected token 'export'
FINALLY

... AND SETTLED

~/projects/stackblitz-starters-xedxss
❯ 

In other words ...

  1. Process should not die.
  2. Control should always return to the routine calling require().
  3. Any/all errors (e.g. from esbuild) should be catch-able by the calling routine
  • Do you have ... ?

Note: Possibly related to issue #472, but as you noted the description there is a bit vague. Maybe this issue will help?

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@wesbos

This comment was marked as off-topic.

@thelinuxlich

This comment was marked as off-topic.

@broofa

This comment was marked as off-topic.

@wesbos
Copy link

wesbos commented Mar 13, 2024

Yep, node.js dynamic import or require can be caught, but tsx cannot.

try {
  const x = await import(`./file-with-syntax-error.js`);
  console.log(x);
} catch (e) {
  console.log(`caught error in try/catch block`);
  console.error(e);
}

Change the .js file with a .ts and you'll see the issue.

Right now my whole dev experience breaks with a syntax error and I have to a non-ignored file and save it to restart the process.

@privatenumber
Copy link
Owner

Fixed in https://github.com/pvtnbr/tsx/pull/7

@privatenumber
Copy link
Owner

privatenumber commented May 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pr welcome
Projects
None yet
Development

No branches or pull requests

4 participants