-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
module: Fix for #17130 CJS dependency shared with loader #17131
Conversation
import dep from './loader-dep.js'; | ||
import assert from 'assert'; | ||
|
||
export function resolve (specifier, base, defaultResolve) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: no space before (
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed up.
18037ac
to
e480ba7
Compare
8acb1e2
to
6462cbe
Compare
6462cbe
to
10a0650
Compare
This failed the snapshot test unfortunately... I've adjusted the fix to deal with the snapshotting edge cases. The snapshot test case is basically there to ensure the CJS module is its module.exports value at exactly the time of execution, and not after any further Lines 538 to 552 in 9c6f6b0
require() value in the loader itself
The fix here is to specifically catch the case of any modules loaded before the loader itself was initialized, based on using the module from the require cache if it is loaded there. |
@@ -2,6 +2,7 @@ | |||
|
|||
const fs = require('fs'); | |||
const internalCJSModule = require('internal/module'); | |||
const CJSModule = require('module'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also hoisted up this require by fixing the circular reference in module.js.
const ctx = createDynamicModule(['default'], url, undefined); | ||
ctx.reflect.exports.default.set(module.exports); | ||
return ctx; | ||
} | ||
return createDynamicModule(['default'], url, (reflect) => { | ||
debug(`Loading CJSModule ${url}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come reflect
isn't used inside the createDynamicModule
callback for 'cjs'
...
reflect.exports.default.set(exports);
...like it is for others and the cached case above 👆
(I'm not familiar with how things work with the loader)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using _load calls around a bunch and eventually ends up here
Lines 577 to 581 in 317f596
} else { | |
const job = ESMLoader.moduleMap.get(urlString); | |
if (job.reflect) | |
job.reflect.exports.default.set(this.exports); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devsnek Ah, any way that could be tidied up so that all the reflect.exports.default.set
is done near each other or at least in the same file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is already as tidy as it will get, but i think a comment above line 49 would go a long way towards explaining what is going on. personally i had no idea how line 49 worked until when you asked because i decided it was time to figure it out. maybe something like we don't assign using reflect here Module.prototype.load handles setting it.
Thanks @targos. Hmm, CI seems to be failing on |
|
Ah right, so it's as simple as a Windows failure. Thanks, will get the local test going here. |
I've added the windows fix - was pretty much as to be expected. |
@jdalton the function at https://github.com/nodejs/node/blob/master/lib/internal/url.js#L1317 could possibly be updated to convert to windows slashes. |
Ya, it should totally be updated. Node has methods to handle this without the need for one-off hacks. |
@jasnell do you think it would make sense to have internalURLModule.getPathFromURL to do |
//cc @bmeck if you have a moment to review. |
This looks fine to me. I need to think on things a bit, but see no blockers. I am wondering if we should move the module map to be based upon It might be more consistent overall to use a separate Module Map during loader initialization but... well that can be done separately to this. |
Thanks @bmeck. I'll aim to merge this tomorrow then. I'm sure these edge cases will change over time, but it would be good to get the bug fix in sooner rather than later. |
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Landed in 1f5ee33. |
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
PR-URL: #17131 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
This fixes the issue of a CJS dependency loaded by the loader then not being defined when imported.
The reason for this is that CJS modules define themselves early into the loader on execution, instead of as a result of being imported by the loader. But this defining only happens the first time they are loaded. Because we use a different loader for the loader itself, the modules weren't being redefined, which has been fixed here.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
esmodules