Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

guybedford
Copy link
Contributor

@guybedford guybedford commented Oct 11, 2017

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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

esmodules

@nodejs-github-bot nodejs-github-bot added the module Issues and PRs related to the module subsystem. label Oct 11, 2017
@guybedford guybedford changed the title module: ESM main handling module: ESM main top-level load handling Oct 11, 2017
lib/module.js Outdated
}

var filename = Module._resolveFilename(request, parent, isMain);
Copy link
Member

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?

Copy link
Contributor Author

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.

@mscdex mscdex added the esm Issues and PRs related to the ECMAScript Modules implementation. label Oct 11, 2017
try {
if (!preserveSymlinks) {
let real;
real = realpathSync(internalURLModule.getPathFromURL(url), {

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.

catch (e) {
if (e && e.code === 'ENOENT') {
e = new Error(`Cannot find module '${specifier}'`);
e.code = 'MODULE_NOT_FOUND';

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?

Copy link
Contributor Author

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.

@guybedford guybedford force-pushed the esm-main-handling branch 2 times, most recently from 7b40f86 to 3c36790 Compare October 12, 2017 12:00
const assert = require('assert');
const child_process = require('child_process');
const { promisify } = require('util');
const execFile = promisify(child_process.execFile);
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.

@guybedford
Copy link
Contributor Author

@cjihrig thanks for review - that update is just pending merge of #16060 it seems. As soon as that is in I will update this PR.

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
@guybedford
Copy link
Contributor Author

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.

@targos
Copy link
Member

targos commented Oct 21, 2017

@targos
Copy link
Member

targos commented Oct 21, 2017

I'm fixing the linting errors and landing.

targos pushed a commit that referenced this pull request Oct 21, 2017
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
@targos
Copy link
Member

targos commented Oct 21, 2017

Landed in fe4675b

@targos targos closed this Oct 21, 2017
MylesBorins pushed a commit that referenced this pull request Oct 23, 2017
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Oct 26, 2017
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Dec 7, 2017
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants