-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
Disallow ESM-CJS-ESM-ESM cycles when using require(esm)
#52145
Comments
Oh, I just noticed that, when building Node.js in debug mode, V8 crashes when running 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 |
require(esm)
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. |
I'm not sure I understand what you are proposing here. The cycle never fully evaluated when |
Hmm, I guess I was too jet-legged when I posted that, and was thinking about the other cycle we talked about ( re. a.mjs, it seems in this case, re. the DCHECK, we should add a status check before instantiation. I'll dig into it. |
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. |
Opened #52264 |
On a side note, #52145 (comment) is still there (i.e. if this is |
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 |
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 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; |
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). |
This patch disallows CJS <-> ESM edges when they come from require(esm) requested in ESM evalaution. Drive-by: don't reuse the cache for imported CJS modules to stash source code of required ESM because the former is also used for cycle detection. PR-URL: #52264 Fixes: #52145 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
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 |
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. |
This patch disallows CJS <-> ESM edges when they come from require(esm) requested in ESM evalaution. Drive-by: don't reuse the cache for imported CJS modules to stash source code of required ESM because the former is also used for cycle detection. PR-URL: nodejs#52264 Fixes: nodejs#52145 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This patch disallows CJS <-> ESM edges when they come from require(esm) requested in ESM evalaution. Drive-by: don't reuse the cache for imported CJS modules to stash source code of required ESM because the former is also used for cycle detection. PR-URL: nodejs#52264 Fixes: nodejs#52145 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This patch disallows CJS <-> ESM edges when they come from require(esm) requested in ESM evalaution. Drive-by: don't reuse the cache for imported CJS modules to stash source code of required ESM because the former is also used for cycle detection. PR-URL: #52264 Backport-PR-URL: #53500 Fixes: #52145 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
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.mjs
contains some code and then athrow
statement at the top level, soimport("./d.mjs")
must always reject (either because of the "some code", of because of thethrow
)c.mjs
importsd.mjs
, soimport("./c.mjs")
must also always throwb.mjs
importsc.mjs
, soimport("./b.mjs")
must also always rejectIndeed,
out/Release/node --experimental-require-module ./d.mjs
throws(which is a wrong error, but at least it is an error 😛) and
out/Release/node --experimental-require-module ./c.mjs
andout/Release/node --experimental-require-module ./c.mjs
throwWhat do you see instead?
However, if you
import()
firstb.mjs
and thend.mjs
, the secondimport()
does not reject!out/Release/node --experimental-require-module ./d.mjs
outputsAdditional 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)
The text was updated successfully, but these errors were encountered: