standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Dynamic Import Crash With Electron #817

Open dsanders11 opened 5 years ago

dsanders11 commented 5 years ago

At first I thought this was a regression of #692, but it seems like it's a different issue.

I don't have a tight reproduce case, but I can describe the general setup and what I think is happening.

  1. Install a module which has a native component (for example, usb) but don't run electron-rebuild. The module will have an error about being the wrong Node version when you try to run Electron, if using require.
  2. Use esm and do a dynamic import for the module (import('usb')). Run Electron, dev tools disconnect and the process hangs.
  3. Ue electron-rebuild, run Electron again, all works.

So, it appears dynamic import is not gracefully handling the error about wrong Node version for the native module. Even if I try to catch any error on dynamic import, it still fails, which means the promise isn't throwing: import('usb').then(module => console.log(module)).catch('error')

I'm not positive it's only related to native modules, but that's how I ran into it, and I unfortunately don't have time to fully run it down to a small reproduce case.

I did try simply making a module with a single line (throw new Error('fail')) and using dynamic import on that module. It doesn't crash, but it does log an uncaught exception rather than using the catch block. Not sure if that's working as expected, my intuition would have been that it should trigger the catch block.

jdalton commented 5 years ago

Hi @dsanders11!

Could you create a repro repo to help me narrow things down?

dsanders11 commented 5 years ago

@jdalton, here you go: https://github.com/dsanders11/electron-quick-start/tree/dynamic-import-native-module-crash

You can run npm run rebuild to see the go-right case when the native module is built for Electron.

stagefright5 commented 3 years ago

Any workarounds / fixes for this?