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

esm: bypass CommonJS loader under --default-type=module #49986

Merged
merged 11 commits into from Oct 5, 2023

Conversation

GeoffreyBooth
Copy link
Member

@GeoffreyBooth GeoffreyBooth commented Oct 1, 2023

This PR implements the part of #49432 where we avoid using the CommonJS loader for ESM entry points in the new --default-type=module mode. This effectively completes some TODOs in the code that note that particular code paths should be deprecated.

The only user-observable effects of this are that, under --experimental-default-type=module:

  • Extension searching is no longer provided for the main entry point. Users need to type node entry.js, not node entry.
  • Libraries that were monkey-patching the CommonJS loader via code loaded by --require will no longer achieve anything. They weren’t achieving much before, as monkey-patching the CommonJS loader only affects CommonJS and not ES modules, so it’s not like the previous behavior was really working very well (if at all) for applications with an ESM entry point.

This inches us closer to actually deprecating the CommonJS loader and having the ESM loader handle all modules, which is a long term goal that will aid in maintainability and future functionality for the modules subsystem.

@GeoffreyBooth GeoffreyBooth added module Issues and PRs related to the module subsystem. cli Issues and PRs related to the Node.js command line interface. esm Issues and PRs related to the ECMAScript Modules implementation. labels Oct 1, 2023
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. process Issues and PRs related to the process subsystem. labels Oct 1, 2023
@GeoffreyBooth
Copy link
Member Author

GeoffreyBooth commented Oct 1, 2023

So there are really two places where we resolve the input into a path: the resolveMainPath function, and this code within runMainESM:

    const main = path.isAbsolute(mainPath) ?
      pathToFileURL(mainPath).href : mainPath;

If we bypass or immediately return from resolveMainPath, then the mainPath input to runMainESM is the raw input from the CLI, like entry.js. And it usually won’t be absolute, so pathToFileURL isn’t getting called for it. So then we’re passing that input into esmLoader.import, which might work when the relative file path is parseable as a relative URL, which is most of the time—but not all of the time.

So I think either we undo the commit I just pushed, and always resolve the input into a path within resolveMainPath for both flows; or do something else. The more I think about this, the more it feels like runMainESM should always expect either an absolute file path or an absolute file URL, and code upstream of it is responsible for that conversion. We shouldn’t be resolving the input multiple times in multiple places. Thoughts @aduh95?

@GeoffreyBooth
Copy link
Member Author

@aduh95 I pushed some more commits, that undo the “streamline” commit, and add some tests and comments and improve an error message.

I’m open to being convinced otherwise, but it feels like a mistake to completely bypass resolveMainPath in ESM mode. Even if the esmLoader.import call within runMainESM somehow correctly parses a relative path as an URL (and please educate me on if that’s the case) we still should preserve the part of resolveMainPath that follows symlinks if --preserve-symlinks-main is not passed, right? We should presumably still support that in the new mode, right?

Assuming the above is all correct so far, I wanted a better error message for the “left off the file extension” case than just ENOENT: no such file or directory, lstat '/Users/geoffrey/Sites/node/test/fixtures/es-modules/package-without-type/index'. So I abstracted the “see if the resolution would’ve worked according to CommonJS rules” logic from esm/resolve.js and import it into the error path in run_main.js, so that node index prints an error message including Did you mean to import /Users/geoffrey/Sites/node/test/fixtures/es-modules/package-without-type/index.js? like we show for import specifiers that lack an extension. We were getting this message for free by passing through invalid input into esmLoader.import, but if we follow through with the toRealPath call in resolveMainPath then we need to handle it there.

Copy link
Contributor

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

Huzzah 🙂

A small nit: somewhere (I can't find the line now—on my mobile), it declares a typedef for default-type as a string: it's not any old string, it's an enum of specific strings. Ideally, those are numerated in the typedef.

Comment on lines +1161 to +1164
error.stack =
ArrayPrototypeShift(lines) + '\n' +
hint + '\n' +
ArrayPrototypeJoin(lines, '\n');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
error.stack =
ArrayPrototypeShift(lines) + '\n' +
hint + '\n' +
ArrayPrototypeJoin(lines, '\n');
error.stack = `${ArrayPrototypeShift(lines)}\n${hint}\n${ArrayPrototypeJoin(lines, '\n')}`;

@GeoffreyBooth
Copy link
Member Author

I reverted the optimization for the legacy flow. I don’t want to make changes to the legacy flow in this PR, I’m too nervous about such changes introducing bugs since the changes don’t break any tests when I’d expect that they should. I want to just land the new feature intended by this PR, bypassing the CommonJS loader under --default-type=module, and we can refactor the legacy flow in a separate PR (and add more tests for it).

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@GeoffreyBooth GeoffreyBooth added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Oct 5, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 5, 2023
@nodejs-github-bot nodejs-github-bot merged commit 19b470f into nodejs:main Oct 5, 2023
60 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 19b470f

@GeoffreyBooth GeoffreyBooth deleted the bypass-cjs-loader branch October 5, 2023 18:02
alexfernandez pushed a commit to alexfernandez/node that referenced this pull request Nov 1, 2023
PR-URL: nodejs#49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit to targos/node that referenced this pull request Nov 11, 2023
PR-URL: nodejs#49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit to targos/node that referenced this pull request Nov 11, 2023
PR-URL: nodejs#49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit that referenced this pull request Nov 11, 2023
PR-URL: #49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit to targos/node that referenced this pull request Nov 11, 2023
PR-URL: nodejs#49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@targos targos added the backported-to-v18.x PRs backported to the v18.x-staging branch. label Nov 23, 2023
targos pushed a commit that referenced this pull request Nov 23, 2023
PR-URL: #49986
Backport-PR-URL: #50669
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
ckerr added a commit to electron/electron that referenced this pull request Dec 7, 2023
no code changes; patch just needed an update due to nearby upstream changes

Xref: nodejs/node#49986
ckerr added a commit to electron/electron that referenced this pull request Dec 7, 2023
…patch

Xref: nodejs/node#49986

minor manual changes needed to sync with upstream change
codebytere pushed a commit to electron/electron that referenced this pull request Dec 8, 2023
no code changes; patch just needed an update due to nearby upstream changes

Xref: nodejs/node#49986
codebytere pushed a commit to electron/electron that referenced this pull request Dec 8, 2023
…patch

Xref: nodejs/node#49986

minor manual changes needed to sync with upstream change
codebytere added a commit to electron/electron that referenced this pull request Dec 8, 2023
codebytere added a commit to electron/electron that referenced this pull request Dec 11, 2023
* chore: bump node in DEPS to v20.10.0

* chore: update feat_initialize_asar_support.patch

no code changes; patch just needed an update due to nearby upstream changes

Xref: nodejs/node#49986

* chore: update pass_all_globals_through_require.patch

no manual changes; patch applied with fuzz

Xref: nodejs/node#49657

* chore: update refactor_allow_embedder_overriding_of_internal_fs_calls

Xref: nodejs/node#49912

no code changes; patch just needed an update due to nearby upstream changes

* chore: update chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch

Xref: nodejs/node#49986

minor manual changes needed to sync with upstream change

* update fix_expose_the_built-in_electron_module_via_the_esm_loader.patch

Xref: nodejs/node#50096
Xref: nodejs/node#50314
in lib/internal/modules/esm/load.js, update the code that checks for
`format === 'electron'`. I'd like 👀 on this

Xref: nodejs/node#49657
add braces in lib/internal/modules/esm/translators.js to sync with upstream

* fix: lazyload fs in esm loaders to apply asar patches

* nodejs/node#50127
* nodejs/node#50096

* esm: jsdoc for modules code

nodejs/node#49523

* test: set test-cli-node-options as flaky

nodejs/node#50296

* deps: update c-ares to 1.20.1

nodejs/node#50082

* esm: bypass CommonJS loader under --default-type=module

nodejs/node#49986

* deps: update uvwasi to 0.0.19

nodejs/node#49908

* lib,test: do not hardcode Buffer.kMaxLength

nodejs/node#49876

* crypto: account for disabled SharedArrayBuffer

nodejs/node#50034

* test: fix edge snapshot stack traces

nodejs/node#49659

* src: generate snapshot with --predictable

nodejs/node#48749

* chore: fixup patch indices

* fs: throw errors from sync branches instead of separate implementations

nodejs/node#49913

* crypto: ensure valid point on elliptic curve in SubtleCrypto.importKey

nodejs/node#50234

* esm: detect ESM syntax in ambiguous JavaScrip

nodejs/node#50096

* fixup! test: fix edge snapshot stack traces

* esm: unflag extensionless ES module JavaScript and Wasm in module scope

nodejs/node#49974

* [tagged-ptr] Arrowify objects

https://chromium-review.googlesource.com/c/v8/v8/+/4705331

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
debadree25 pushed a commit to debadree25/node that referenced this pull request Apr 15, 2024
PR-URL: nodejs#49986
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#49986
Backport-PR-URL: nodejs/node#50669
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#49986
Backport-PR-URL: nodejs/node#50669
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. backported-to-v18.x PRs backported to the v18.x-staging branch. cli Issues and PRs related to the Node.js command line interface. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. process Issues and PRs related to the process subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants