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

Is it possible to catch a build-time error during a dynamic import? #472

Closed
4 of 6 tasks
thelinuxlich opened this issue Feb 6, 2024 · 4 comments
Closed
4 of 6 tasks
Labels
bug Something isn't working pr welcome

Comments

@thelinuxlich
Copy link

Precheck

  • I searched existing issues before opening this one to avoid duplicates
  • I understand tsx aims for TypeScript parity and will not support arbitrary Node.js enhancements
  • This request cannot be made to Node.js directly, and is specific to tsx
  • I understand this form is not for addressing a bug or seeking help

Feature request

I have a codegen task watching a directory for changes and whenever there is a malformed TS file, TSX will throw a esbuild error and exit the watcher. I wish I could try-catch this error so the watcher restarts the task.

Motivations

I don't know if it's expected but I think it's a bit unintuitive that I can't trust this:

try {
   await import('./something')
} catch (e) {
   console.log(e)
}

Alternatives

No response

Additional context

No response

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@thelinuxlich thelinuxlich added enhancement New feature or request pending triage labels Feb 6, 2024
@privatenumber
Copy link
Owner

Not really sure what you're asking/reporting

@wesbos
Copy link

wesbos commented Mar 13, 2024

Also have this issue. I'm dynamically importing a path, and if that file has a syntax error in it, the watcher will crash and I need to restart the whole process.

In Node, you can catch the error like so:

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);
}

But with tsx, esbuild throws and error and exits the process:

Screenshot 2024-03-13 at 3 04 16 PM

I supposed I could run nodemon or something to get around this, but was wondering if I could somehow catch the error on a dynamic require/import.

@privatenumber
Copy link
Owner

privatenumber commented Mar 13, 2024

Ooh gotcha. The original issue didn't explain that dynamic import errors can't be caught. Thanks for explaining.

This would be a bug, and possibly related to #493

Feel free to submit a PR

@privatenumber privatenumber added bug Something isn't working pr welcome and removed enhancement New feature or request pending triage labels Mar 13, 2024
@wesbos
Copy link

wesbos commented Mar 13, 2024

Okay thank you - that is also the issue i have. I think we can close this as a dupe.

@privatenumber privatenumber closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
Repository owner locked and limited conversation to collaborators Mar 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working pr welcome
Projects
None yet
Development

No branches or pull requests

3 participants