Repository navigation
Disallow ESM-CJS-ESM-ESM cycles when using require(esm) #52145
Description
Version
Unreleased (5f7fad2)
Platform
n/a
Subsystem
esm
What steps will reproduce the bug?
// c.mjs
import { createRequire } from "module";
console.log("Start c");
createRequire(import.meta.url)("./d.mjs");
throw new Error("Error from c");// d.mjs
import "./c.mjs";
console.log("Execute d");// b.mjs
import "./c.mjs";
console.log("Execute b");// a.mjs
try {
await import("./b.mjs");
console.log("Dynamic 1 didn't fail")
} catch (err) {
console.log(err);
}
try {
await import("./d.mjs");
console.log("Dynamic 2 didn't fail")
} catch (err) {
console.log(err);
}How often does it reproduce? Is there a required condition?
Whenever there is an ES modules that triggers a require() of a separate ES module that imports the original ES module.
What is the expected behavior? Why is that the expected behavior?
d.mjscontains some code and then athrowstatement at the top level, soimport("./d.mjs")must always reject (either because of the "some code", of because of thethrow)c.mjsimportsd.mjs, soimport("./c.mjs")must also always throwb.mjsimportsc.mjs, soimport("./b.mjs")must also always reject
Indeed, out/Release/node --experimental-require-module ./d.mjs throws
Start c
node:internal/modules/esm/loader:274
return job.module.getNamespaceSync();
^
Error: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.(which is a wrong error, but at least it is an error 😛) and out/Release/node --experimental-require-module ./c.mjs and out/Release/node --experimental-require-module ./c.mjs throw
Start c
Execute d
file:///.../c.mjs:5
throw new Error("Error from c");
^
Error: Error from cWhat do you see instead?
However, if you import() first b.mjs and then d.mjs, the second import() does not reject! out/Release/node --experimental-require-module ./d.mjs outputs
(node:928766) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Start c
Execute d
Error: Error from c
at file://.../c.mjs:5:7
at ModuleJob.run (node:internal/modules/esm/module_job:235:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:436:24)
at async file://.../a.mjs:3:3
Dynamic 2 didn't fail
Additional information
I think the reason the second import() does not properly reject is because running two module evaluation algorithms at the same time messes up with the way V8 tracks cycles. Specifically, it violates the assumption made at step 11.c.ii of https://tc39.es/ecma262/#sec-innermoduleevaluation, causing them problems at step 16.
A solution to this would be, when doing require(esm), to check if the module or any of its transitive dependencies are currently being evaluated. Right now Node.js just checks if the module itself is being evaluated (and throws that top-level await error).
cc @joyeecheung (who is already aware of this, I'm just opening the issue so that it doesn't get forgotten)
Activity
nicolo-ribaudo commentedon Mar 18, 2024
Oh, I just noticed that, when building Node.js in debug mode, V8 crashes when running
c.mjs:[-]Disallow ESM-CJS-ESM-ESM cycles[/-][+]Disallow ESM-CJS-ESM-ESM cycles when using `require(esm)`[/+]joyeecheung commentedon Mar 19, 2024
I think we have two options:
I think the first is probably easier to implement - just throw when the status is not evaluated. In practice, that's probably suits the "best effort" approach of CJS/ESM interop better - we give users whatever we can when it's safe, otherwise we throw. The latter would be a bit trickier to implement - we need to keep track of where the modules come from and search for cycles. Not impossible but I wonder if that's really worth the effort. We could just implement 1 first, and, if it turns out to be problematic, we do 2.
nicolo-ribaudo commentedon Mar 19, 2024
I'm not sure I understand what you are proposing here. The cycle never fully evaluated when
require()returns, because the module that containsrequire()is part of the cycle and thus it will not be finished evaluating yet.joyeecheung commentedon Mar 19, 2024
Hmm, I guess I was too jet-legged when I posted that, and was thinking about the other cycle we talked about (
import cjs->require(esm)cycle).re. a.mjs, it seems in this case,
d.mjsgets cached in the first case, so you wouldn't see an error later. I am not sure what really should be the expected behavior. Funny enough if you switchcreateRequire(import.meta.url)("./d.mjs");toawait import("./d.mjs")inc.mjsit gets even more bizzare (only logsStart c. On main with the recent changes I added in #51999 it will show you thatawait import("./b.mjs")ina.mjswas never resolved). I suspect we need to fix the caching strategy for this, not just forrequire(esm).re. the DCHECK, we should add a status check before instantiation. I'll dig into it.
joyeecheung commentedon Mar 22, 2024
Working on a branch to just ban ESM -> CJS and CJS -> ESM edges when they participate in a cycle. For the example in the OP, b-d.mjs fails, the two cases in a.mjs fail individually but not together, still need to properly fix up the cache when the evaluation fails.
joyeecheung commentedon Mar 31, 2024
Opened #52264
joyeecheung commentedon Mar 31, 2024
On a side note, #52145 (comment) is still there (i.e. if this is
await import(), the code below TLA never runs), though I am not sure what the spec says about that. Looks like a footgun to have silently never-executed code belowawait import()that trigger a cycle...Jamesernator commentedon Apr 4, 2024
Wouldn't it be better here to just check if the module is evaluating and simply return the namespace if it's already evaluating? This would more easily allow people to migrate file-at-a-time between CJS and ESM (and it mirrors how CJS already works so wouldn't be a big divergence).
i.e. The problem here basically reduces down to this:
my suggested fix is just to check if evaluation is already happening:
nicolo-ribaudo commentedon Apr 4, 2024
What if the
require()d module itself is not evaluating, but one of its dependencies is?Jamesernator commentedon Apr 4, 2024
True, it might be worth seeing if we can get a spec change here as this behaviour is very similar to the change proposed for deferred module evaluation.
In fact I wonder if, with that proposal and the fix, implementing
require("esm")like this would just work:joyeecheung commentedon Apr 5, 2024
Technically with CJS, it's not simply returning circular exports that are being loaded, but a proxy (pretty slow) that emits a warning if you are accessing a property before it's added to the exports object (which is still more of a guess - one can still create a race condition by modifying the exports later, so in the cycle then you can't be certain what version of the export you are getting from a consumer point of view).
module: disallow CJS <-> ESM edges in a cycle from require(esm)
joyeecheung commentedon Apr 8, 2024
Should we keep it open in case we want to discuss further about the behavior? I guess for now at least we can just throw, as this is probably more of an edge case that most wouldn't care about. If somehow that proves to be inconvenient enough we can revisit to return the not-yet-populated namespaces for the cycle (probably requires a V8 & sepc change to get rid of the assertions in subgraphs though).
joyeecheung commentedon Apr 8, 2024
It might also be nice to print a require + import stack when the cycle happens. Though I guess that kind of traces have been long missing on the ESM front (for
require()there is a stack that we maintain to enhance the errors, for ESM I guess so far one has to increase--stack-trace-limitand fish out relevant lines from the error stack trace when the ESM loader throws...).joyeecheung commentedon Apr 10, 2024
It seems tc39/proposal-defer-import-eval#39 is what we'll need to not throw for cyclic edges. So until that ends up in the ECMA262 proper, we can just throw for now.
module: disallow CJS <-> ESM edges in a cycle from require(esm)
module: disallow CJS <-> ESM edges in a cycle from require(esm)