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: improve check for ESM syntax #50127

Merged
merged 5 commits into from
Oct 18, 2023
Merged

Conversation

GeoffreyBooth
Copy link
Member

@GeoffreyBooth GeoffreyBooth commented Oct 10, 2023

This PR speeds up and simplifies the error path for when users require ES modules. In particular, the previous hasEsmSyntax helper used Acorn to parse the required module’s source code to find ESM syntax. In this refactor, the check moves into C++ and relies on V8 throwing a SyntaxError with one of the error messages that we know are caused by ESM syntax in a module compiled as CommonJS. This allows us to eliminate one use of Acorn from the codebase.

This also creates the helper function that #50096 needs.

@nodejs/loaders @nodejs/cpp-reviewers

@GeoffreyBooth GeoffreyBooth added module Issues and PRs related to the module subsystem. esm Issues and PRs related to the ECMAScript Modules implementation. labels Oct 10, 2023
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Oct 10, 2023
lib/internal/modules/helpers.js Outdated Show resolved Hide resolved
@GeoffreyBooth
Copy link
Member Author

@anonrig showed me how this could be improved further by eliminating the try/catch, adapting the work from 6c1c2f5 (#50096). I’m going to take another pass at this, incorporating the C++ changes and doing the comparisons of the error messages in C++ rather than returning the error object across the boundary.

@GeoffreyBooth GeoffreyBooth added the lib / src Issues and PRs related to general changes in the lib or src directory. label Oct 14, 2023
@GeoffreyBooth
Copy link
Member Author

GeoffreyBooth commented Oct 14, 2023

I pushed a completely new implementation for this, building off of @joyeecheung’s #50137. Until that merges in, you can see what this PR adds at https://github.com/GeoffreyBooth/node/compare/vm-flag-callback-2…GeoffreyBooth:node:better-esm-check

lib/internal/process/execution.js Outdated Show resolved Hide resolved
lib/internal/vm.js Outdated Show resolved Hide resolved
@GeoffreyBooth GeoffreyBooth force-pushed the better-esm-check branch 2 times, most recently from 651bfde to cb86df8 Compare October 14, 2023 22:28
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Great to see, this should actually be an optimal ESM check.

It would be great to start to remove our internal dependence on Acorn as well.

src/node_contextify.cc Outdated Show resolved Hide resolved
lib/internal/modules/esm/translators.js Outdated Show resolved Hide resolved
@GeoffreyBooth GeoffreyBooth force-pushed the better-esm-check branch 2 times, most recently from 9b456ba to 1836cb2 Compare October 17, 2023 02:48
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@GeoffreyBooth GeoffreyBooth added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 17, 2023
@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Oct 17, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@GeoffreyBooth GeoffreyBooth added the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 18, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 18, 2023
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/50127
✔  Done loading data for nodejs/node/pull/50127
----------------------------------- PR info ------------------------------------
Title      esm: improve check for ESM syntax (#50127)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     GeoffreyBooth:better-esm-check -> nodejs:main
Labels     module, lib / src, esm, author ready, needs-ci, commit-queue-squash
Commits    5
 - esm: improve check for ESM syntax
 - add ContainsModuleSyntax function
 - Replace old helper with new
 - code review notes
 - Fix compilation for arm-debug
Committers 1
 - Geoffrey Booth 
PR-URL: https://github.com/nodejs/node/pull/50127
Reviewed-By: Guy Bedford 
Reviewed-By: Yagiz Nizipli 
Reviewed-By: Jacob Smith 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/50127
Reviewed-By: Guy Bedford 
Reviewed-By: Yagiz Nizipli 
Reviewed-By: Jacob Smith 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Tue, 10 Oct 2023 18:35:12 GMT
   ✔  Approvals: 3
   ✔  - Guy Bedford (@guybedford): https://github.com/nodejs/node/pull/50127#pullrequestreview-1678506390
   ✔  - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/50127#pullrequestreview-1683777828
   ✔  - Jacob Smith (@JakobJingleheimer): https://github.com/nodejs/node/pull/50127#pullrequestreview-1682930116
   ✘  Last GitHub CI failed
   ℹ  Last Full PR CI on 2023-10-18T05:18:03Z: https://ci.nodejs.org/job/node-test-pull-request/54923/
- Querying data for job/node-test-pull-request/54923/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/6560512702

@nodejs-github-bot nodejs-github-bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Oct 18, 2023
@aduh95 aduh95 merged commit a9b2535 into nodejs:main Oct 18, 2023
25 checks passed
@aduh95
Copy link
Contributor

aduh95 commented Oct 18, 2023

Landed in a9b2535

aduh95 pushed a commit to aduh95/node that referenced this pull request Oct 18, 2023
PR-URL: nodejs#50127
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@GeoffreyBooth GeoffreyBooth deleted the better-esm-check branch October 18, 2023 21:19
targos pushed a commit that referenced this pull request Oct 23, 2023
PR-URL: #50127
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
alexfernandez pushed a commit to alexfernandez/node that referenced this pull request Nov 1, 2023
PR-URL: nodejs#50127
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
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: #50127
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
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>
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. commit-queue-failed An error occurred while landing this 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. esm Issues and PRs related to the ECMAScript Modules implementation. lib / src Issues and PRs related to general changes in the lib or src directory. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants