Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.3k
module: resolve format for all situations with auto module detection on#53044
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: resolve format for all situations with auto module detection on #53044
Uh oh!
There was an error while loading. Please reload this page.
Conversation
dygabo commented May 18, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
nodejs-github-bot commented May 18, 2024
Review requested:
|
dygabo commented May 18, 2024
if the solution is feasible and finds approval, the |
avivkeller commented May 18, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
avivkeller commented May 18, 2024
Also, |
Uh oh!
There was an error while loading. Please reload this page.
avivkeller left a comment • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
+1
Tip
While my review shows my support, I am not a core collaborator, and this review has no power / place in the approval process
GeoffreyBooth left a comment
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.
Great work! Thanks for getting to the bottom of this.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
dygabo commented May 18, 2024
Will check your comments and split it into two PRs, will happen maybe beginning of next week. |
avivkeller commented May 18, 2024
Lovely! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
1d7b4de to b7ec307Comparedygabo commented May 20, 2024
split done, this fixes the |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
afb82c8 to b2f221fCompare33d3bed to f4d6382Compare
mcollina left a comment
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.
still LGTM
nodejs-github-bot commented Jun 24, 2024
266c499 to 6445e1aCompareCo-authored-by: Gabriel Bota <[email protected]>
6445e1a to 6132129Comparenodejs-github-bot commented Jun 25, 2024
6132129 to 5753afcCompareCo-authored-by: Geoffrey Booth <[email protected]>
5753afc to bd945b6Comparenodejs-github-bot commented Jun 25, 2024
src/node_contextify.cc Outdated
| Local<String> code; | ||
| if (args[0]->IsUndefined()){ | ||
| CHECK(!filename.IsEmpty()); | ||
| constchar* filename_str = Utf8Value(isolate, filename).out(); |
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.
The Utf8Value here can’t be temporary, it needs to outlive the const char* or otherwise it is use-after-free.
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.
fixed
| CHECK(!filename.IsEmpty()); | ||
| constchar* filename_str = Utf8Value(isolate, filename).out(); | ||
| std::string contents; | ||
| int result = ReadFileSync(&contents, filename_str); |
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.
Reading from disk from the native layer here doesn’t seem right - it’s not guaranteed that this is on disk and at a location pointed to by filename (the filename can be some artificial ID). The JS land should be refactored to only call this after source code is confirmed instead.
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.
this comes as part of the trials to cover the module type resolution for the resolve. I would expect if any of that happens (correct me if I'm wrong) that ReadFileSync will return != 0here and this would cause the early exit with an exception.
Additionally we call this from managed code here. I am guessing that at this stage the fileUrl was already validated but that is at this stage just an assumption. This test made me think that.
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.
The exception would be surprising for a hook - resolve shouldn't be poking into the file system, that should be load's job.
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.
that is changed now, the comment above is obsolete. The function would return undefined on file access errors. But I am still looking for a way to remove this file access without losing the test.
Uh oh!
There was an error while loading. Please reload this page.
joyeecheung commented Jun 25, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
IMO it doesn’t make sense to eagerly detect the format at resolve, the format should only be confirmed during loading, when the source becomes available. Until then it should be allowed to be undefined. For one it’s not guaranteed that the filename is actually readable from the fs at resolve time. Also for libraries like tsx that uses the defaultResolve to probe possible default entry points, doing the detection at resolve incurs unnecessary overhead on formats that are not recognizable by Node.js. That should be left to default load when the final source code is available. |
dygabo commented Jun 25, 2024
With current version it still is allowed and it might happen that it is undefined (if one of the cases occur where it cannot be determined during resolve, like the typescript case, or file not accessible). The more confusing part is IMO where like with the last test being added, it can be determined but the |
dygabo commented Jun 26, 2024
@joyeecheung I tried today to modfiy this part according to your suggestion to have With current implementation the hint quality is better than it was before because of the One example: tests like this one are not passing because they are based on loaders like this that expect the format to be determined by resolve. I did not find a quick solution for this. Do you or @GeoffreyBooth, @aduh95 have an idea on how to solve it? |
joyeecheung commented Jun 26, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Doesn't this PR "improves" the format detection in resolve by trying harder (even though reading from the file system at resolve phase might be the wrong way to try it), and hence make the breaking change necessary for correcting this design flaw even more breaking than it already is? Format detection in resolve should be a best effort (tentative based on file extensions and package.json info) IMO, and we shouldn't go out of our ways to detect the format by reading the file and even parsing it at resolve phase. That leads to unnecessary overhead to the overall design especially if user hooks point to a non-existent URL or a file with unrecognizable format at resolve phase (for better error stack traces) and only override the format detection at load. |
dygabo commented Jun 26, 2024
I agree with this and also that the file read has an impact on the running time. But I am slightly out of ideas on how to solve the one failing test that I mentioned. I will give it one more try, see what solution I can find. Any suggestions on how to solve it are appreciated. |
joyeecheung commented Jun 26, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Is the failing test something like https://github.com/dygabo/node/blob/fix-for-unflagging-module-format-detection/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs#L31 ? In that case I think it's the test that's making wrong assumptions - the loader hook should not count on the |
joyeecheung commented Jun 26, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
In terms of format, I think there are actually two concepts:
e.g. if it's a |
GeoffreyBooth commented Jun 26, 2024
I’ve been referring to “a Because it does feel like the right approach here is to update the test so that it no longer expects |
dygabo commented Jun 27, 2024
Thinking about this a bit more over the past hours, I understand the concerns and am looking into alternative solution to propose.
I hope this makes sense. Having said that, I will continue to look for alternatives but this might take some time. |
GeoffreyBooth commented Jun 28, 2024
Talking to @dygabo and @joyeecheung, I think we need to explore a solution that doesn’t involve reading from disk during the It could be that we simply change the hinted I think the next step is to open a PR that unflags detection and updates all the tests accordingly, including making this |
dygabo commented Aug 1, 2024
superseded and solved by #53619. |

triggered by #53015
solves: #53016
this should be a consistent fix to always resolve the module format correctly.
Enabling module detection by default made a few other tests need some adjustments because in this case they don't generate errors anymore. e.g.
test-esm-cjs-exports.jsinstead of error becasue a .mjs imports a .js with ESM syntax it now successfully imports it and generates the warning that this should be fixed to avoid the performance penalty.Kindly please review and let me know what you think (if changes are necessary).
make test && make lint=> greenCo-authored-by: @GeoffreyBooth
@nodejs/loaders