Repository navigation
Disallow ESM-CJS-ESM-ESM cycles when using require(esm) #52145
Description
Activity
Oh, I just noticed that, when building Node.js in debug mode, V8 crashes when running
c.mjs:out/Debug/node --experimental-require-module ./c.mjs (node:930777) 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 # # Fatal error in ../../deps/v8/src/objects/module.cc, line 205 # Debug check failed: module->status() != kEvaluating (4 vs. 4). # # # #FailureMessage Object: 0x7ffef98cbe30 ----- Native stack trace ----- 1: 0x55cfdded5ad8 node::DumpNativeBacktrace(_IO_FILE*) [out/Debug/node] 2: 0x55cfde1252f9 [out/Debug/node] 3: 0x55cfde12531d [out/Debug/node] 4: 0x55cfe0660f4b V8_Fatal(char const*, int, char const*, ...) [out/Debug/node] 5: 0x55cfe0660f7b [out/Debug/node] 6: 0x55cfded97342 v8::internal::Module::PrepareInstantiate(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Module>, v8::Local<v8::Context>, v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::FixedArray>, v8::Local<v8::Module>), v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>)) [out/Debug/node] 7: 0x55cfdee31a78 v8::internal::SourceTextModule::PrepareInstantiate(v8::internal::Isolate*, v8::internal::Handle<v8::internal::SourceTextModule>, v8::Local<v8::Context>, v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::FixedArray>, v8::Local<v8::Module>), v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>)) [out/Debug/node] 8: 0x55cfded9824b v8::internal::Module::Instantiate(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Module>, v8::Local<v8::Context>, v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::FixedArray>, v8::Local<v8::Module>), v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::Module>)) [out/Debug/node] 9: 0x55cfde446021 v8::Module::InstantiateModule(v8::Local<v8::Context>, v8::MaybeLocal<v8::Module> (*)(v8::Local<v8::Context>, v8::Local<v8::String>, v8::Local<v8::FixedArray>, v8::Local<v8::Module>)) [out/Debug/node] 10: 0x55cfddf77132 node::loader::ModuleWrap::InstantiateSync(v8::FunctionCallbackInfo<v8::Value> const&) [out/Debug/node] 11: 0x55cf7f8d199d [1] 930777 trace trap (core dumped) out/Debug/node --experimental-require-module
- changed the title
[-]Disallow ESM-CJS-ESM-ESM cycles[/-][+]Disallow ESM-CJS-ESM-ESM cycles when using `require(esm)`[/+]on Mar 18, 2024 I think we have two options:
- Only return the module if it's already evaluated. But ESM-CJS-ESM-ESM can still be allowed if, by the time CJS -> ESM happens, the cycle is already fully evaluated (unless that graph involves TLA)
- Distinguish between "modules (directly or indirectly) required() by CJS" and "modules (directly or indirectly) imported by ESM". And disallow cycles when the last ESM-ESM edge is the latter.
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.
Only return the module if it's already evaluated. But ESM-CJS-ESM-ESM can still be allowed if, by the time CJS -> ESM happens, the cycle is already fully evaluated (unless that graph involves TLA)
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.- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on 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.
Reacted by ExE BossWorking 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.
Reacted by Nicolò Ribaudo, Toni Villena and ExE BossOpened #52264
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...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:
globalThis.mod = new vm.SourceTextModule(` function requireImpl(specifier) { // specifier gets resolved to the currently to the currently evaluating module const mod = globalThis.mod; mod.evaluate(); return mod.namespace; } console.log("Before require"); const getMod = requireImpl("specifier-that-results-in-evaluate-of-the-current-module"); console.log("After require"); `);
my suggested fix is just to check if evaluation is already happening:
globalThis.mod = new vm.SourceTextModule(` function requireImpl(specifier) { // specifier gets resolved to the currently to the currently evaluating module const mod = globalThis.mod; // In a cycle so just return the namespace if (mod.status === "evaluating") { return mod.namespace; } mod.evaluate(); return mod.namespace; } console.log("Before require"); const getMod = requireImpl("specifier-that-results-in-evaluate-of-the-current-module"); console.log("After require"); `);
What if the
require()d module itself is not evaluating, but one of its dependencies is?What if the
require()d module itself is not evaluating, but one of its dependencies is?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:const mod = new SourceTextModule(...); mod.linkSync(); // Whatever the internal method here is for linking sync // Accessing any member of the namespace triggers EvaluateSync mod.namespace.ANY_PROP_TO_TRIGGER_EVAL;
Reacted by ExE BossThis 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).
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).
Reacted by ExE Boss- added a commit that references this issue
on 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).
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...).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.
Reacted by ExE Boss- added a commit that references this issue
on Jul 30, 2024 - added a commit that references this issue
on Aug 8, 2024
Version
Unreleased (5f7fad2)
Platform
n/a
Subsystem
esm
What steps will reproduce the bug?
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 rejectIndeed,
out/Release/node --experimental-require-module ./d.mjsthrows(which is a wrong error, but at least it is an error 😛) and
out/Release/node --experimental-require-module ./c.mjsandout/Release/node --experimental-require-module ./c.mjsthrowWhat do you see instead?
However, if you
import()firstb.mjsand thend.mjs, the secondimport()does not reject!out/Release/node --experimental-require-module ./d.mjsoutputsAdditional 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)