-
Notifications
You must be signed in to change notification settings - Fork 30.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: ESM main top-level load handling #16147
Conversation
8a243eb
to
4e3bae0
Compare
lib/module.js
Outdated
} | ||
|
||
var filename = Module._resolveFilename(request, parent, isMain); |
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.
Why do you use var
?
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.
To match the code beneath it goes with - this is just moving the existing line from https://github.com/nodejs/node/pull/16147/files#diff-d1234a869b3d648ebfcdce5a76747d71L424. Although it could be refactored in the process certainly.
lib/internal/loader/ModuleRequest.js
Outdated
try { | ||
if (!preserveSymlinks) { | ||
let real; | ||
real = realpathSync(internalURLModule.getPathFromURL(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.
Why did this change to let real
instead of staying const real
? It's still only assigned once during its lifetime.
lib/internal/loader/ModuleRequest.js
Outdated
catch (e) { | ||
if (e && e.code === 'ENOENT') { | ||
e = new Error(`Cannot find module '${specifier}'`); | ||
e.code = 'MODULE_NOT_FOUND'; |
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.
Is Error
the correct class, or is there a more precise subclass with a code
?
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 adjusted this to amend the code to the error thrown from C++. If there is a better way to throw the error with the right code in C++ let me know, but I wasn't able to find existing examples for this pattern.
7b40f86
to
3c36790
Compare
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const { promisify } = require('util'); | ||
const execFile = promisify(child_process.execFile); |
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.
Can you just use execFileSync()
if it gets the job done. I think it would simplify the test.
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const { promisify } = require('util'); | ||
const execFile = promisify(child_process.execFile); |
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.
Same comment here.
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from nodejs#15736
3c36790
to
23381c7
Compare
I've updated this PR to use the sync tests, while working around the issue in #16060. Would be nice to get these fixes into the current release cycle if possible, but I understand if the timeline for that has passed already. |
I'm fixing the linting errors and landing. |
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from #15736. Fixes: #15732 PR-URL: #16147 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Landed in fe4675b |
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from #15736. Fixes: #15732 PR-URL: #16147 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from nodejs/node#15736. Fixes: nodejs/node#15732 PR-URL: nodejs/node#16147 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from nodejs/node#15736. Fixes: nodejs/node#15732 PR-URL: nodejs/node#16147 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This is an update to the top-level loading for ES module loading based on the latest master, including the fix and test (working async version) from #15736 by @targos.
Firstly we ensure that all resolver errors of code ENOENT are reported as MODULE_NOT_FOUND errors, and secondly, we can now delegate entirely to the ES module loader for resolution as it will internally delegate to the CommonJS loader based on extension rules so we can simplify the top-level logic quite nicely.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
esmodules