jkcfg / jk

Configuration as Code with ECMAScript
https://jkcfg.github.io
Apache License 2.0
406 stars 30 forks source link

Segfault when having a cycle in imports #185

Open dlespiau opened 5 years ago

dlespiau commented 5 years ago

With:

$ cat foo.js 
import * as bar from 'bar.js';
$ cat bar.js 
import * as foo from 'foo.js';

jk segfaults trying to resolve an infinite import loop. --debug-imports can be used to trace what happens, continuously resolving the two imports:

$ jk run foo.js --debug-imports
debug:   File: ✔ import foo.js from /home/damien/go/src/github.com/jkcfg/jk/bar.js (base=/home/damien/go/src/github.com/jkcfg/jk) -> /home/damien/go/src/github.com/jkcfg/jk/foo.js
debug:   File: ✔ import bar.js from /home/damien/go/src/github.com/jkcfg/jk/foo.js (base=/home/damien/go/src/github.com/jkcfg/jk) -> /home/damien/go/src/github.com/jkcfg/jk/bar.js
debug:   File: ✔ import foo.js from /home/damien/go/src/github.com/jkcfg/jk/bar.js (base=/home/damien/go/src/github.com/jkcfg/jk) -> /home/damien/go/src/github.com/jkcfg/jk/foo.js
debug:   File: ✔ import bar.js from /home/damien/go/src/github.com/jkcfg/jk/foo.js (base=/home/damien/go/src/github.com/jkcfg/jk) -> /home/damien/go/src/github.com/jkcfg/jk/bar.js
...

We should detect import cycles and stop them before they explode, displaying the cycle for users to debug.

dlespiau commented 5 years ago

es6 is supposed to be able to load modules with cyclic dependencies. The cycle is entirely in our import code, not the v8 one, so maybe, once we break the cycle instead of segfaulting, v8 will be able to load the script.

https://2ality.com/2014/09/es6-modules-final.html#support-for-cyclic-dependencies-between-modules

dlespiau commented 5 years ago

The thing we want to do here is not to segfault. Supporting cyclic import may start to "just work" once we break cycles.