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

loader: require.resolve should throw for unknown builtin modules #43336

Merged
merged 3 commits into from
Jun 17, 2022

Conversation

zhmushan
Copy link
Contributor

@zhmushan zhmushan commented Jun 7, 2022

Fixes: #43274

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/modules

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Jun 7, 2022
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for fixing this!

@ljharb ljharb added the module Issues and PRs related to the module subsystem. label Jun 7, 2022
@mscdex
Copy link
Contributor

mscdex commented Jun 7, 2022

s/unkonw/unknown/ in commit message

@zhmushan
Copy link
Contributor Author

zhmushan commented Jun 7, 2022

s/unkonw/unknown/ in commit message

Can you explain in detail what this means? I'm not very familiar with the nodejs contribution requirements.
I've added a link to Issue in the latest commit message

@DRoet
Copy link

DRoet commented Jun 7, 2022

@zhmushan

It just means there is a typo in your commit message "unkonw" that should be "unknown".
s/unkonw/unknown/ is just a way to describe a "search and replace".

@zhmushan
Copy link
Contributor Author

zhmushan commented Jun 7, 2022

It just means there is a typo in your commit message "unkonw" that should be "unknown". s/unkonw/unknown/ is just a way to describe a "search and replace".

Oh, thank you very much. I just realized it was a vim shortcut, forgive me for not knowing anything about it.
I've fixed it.

@JakobJingleheimer JakobJingleheimer changed the title loader: require.resolve should throw for unkonw builtin modules loader: require.resolve should throw for unknown builtin modules Jun 7, 2022
() => require.resolve('node:unknown'),
{
code: 'MODULE_NOT_FOUND',
message: /^Cannot find module 'node:unknown'/,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quote from the documentation:

If the module can not be found, a MODULE_NOT_FOUND error is thrown.

@@ -780,19 +780,19 @@ Module._load = function(request, parent, isMain) {
}
}

const filename = Module._resolveFilename(request, parent, isMain);
if (StringPrototypeStartsWith(filename, 'node:')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Loading a builtin module prefixed with "node:" expects "ERR_UNKNOWN_BUILTIN_MODULE" to be thrown.
If the "resolve" function is called here, "MODULE_NOT_FOUND" will be thrown.

Copy link
Contributor

Choose a reason for hiding this comment

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

ERR_UNKNOWN_BUILTIN_MODULE certainly makes more sense here than MODULE_NOT_FOUND, I agree (although I think both are fine).

lib/internal/modules/cjs/loader.js Outdated Show resolved Hide resolved
test/parallel/test-require-resolve.js Outdated Show resolved Hide resolved
zhmushan and others added 2 commits June 7, 2022 22:27
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@zhmushan zhmushan requested a review from ljharb June 8, 2022 02:43
@ljharb
Copy link
Member

ljharb commented Jun 8, 2022

LGTM

@ljharb ljharb removed their request for review June 8, 2022 06:06
@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jun 8, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 8, 2022
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@lpinca lpinca added the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 14, 2022
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Jun 14, 2022
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/43336
✔  Done loading data for nodejs/node/pull/43336
----------------------------------- PR info ------------------------------------
Title      loader: require.resolve should throw for unknown builtin modules (#43336)
Author     木杉  (@zhmushan)
Branch     zhmushan:require_resolve -> nodejs:master
Labels     module, author ready, needs-ci
Commits    3
 - loader: make require.resolve throw error for unknown builtin modules
 - Update lib/internal/modules/cjs/loader.js
 - Update test/parallel/test-require-resolve.js
Committers 2
 - 木杉 
 - GitHub 
PR-URL: https://github.com/nodejs/node/pull/43336
Fixes: https://github.com/nodejs/node/issues/43274
Reviewed-By: Antoine du Hamel 
Reviewed-By: Benjamin Gruenbaum 
Reviewed-By: James M Snell 
Reviewed-By: Luigi Pinca 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/43336
Fixes: https://github.com/nodejs/node/issues/43274
Reviewed-By: Antoine du Hamel 
Reviewed-By: Benjamin Gruenbaum 
Reviewed-By: James M Snell 
Reviewed-By: Luigi Pinca 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Tue, 07 Jun 2022 04:12:07 GMT
   ✔  Approvals: 4
   ✔  - Antoine du Hamel (@aduh95) (TSC): https://github.com/nodejs/node/pull/43336#pullrequestreview-999163419
   ✔  - Benjamin Gruenbaum (@benjamingr): https://github.com/nodejs/node/pull/43336#pullrequestreview-1000924532
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/43336#pullrequestreview-1003513209
   ✔  - Luigi Pinca (@lpinca): https://github.com/nodejs/node/pull/43336#pullrequestreview-1006496775
   ✖  Last GitHub CI failed
   ℹ  Last Full PR CI on 2022-06-13T16:59:06Z: https://ci.nodejs.org/job/node-test-pull-request/44498/
- Querying data for job/node-test-pull-request/44498/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/2497846390

@aduh95 aduh95 merged commit 7fd4cf4 into nodejs:main Jun 17, 2022
@aduh95
Copy link
Contributor

aduh95 commented Jun 17, 2022

Landed in 7fd4cf4

@ljharb
Copy link
Member

ljharb commented Jun 17, 2022

Yay! What’s the best way to quickly backport this as far as possible?

@zhmushan zhmushan deleted the require_resolve branch June 30, 2022 16:20
targos pushed a commit that referenced this pull request Jul 12, 2022
Fixes: #43274

PR-URL: #43336
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
targos pushed a commit that referenced this pull request Jul 31, 2022
Fixes: #43274

PR-URL: #43336
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
guangwong pushed a commit to noslate-project/node that referenced this pull request Oct 10, 2022
Fixes: nodejs/node#43274

PR-URL: nodejs/node#43336
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this pull request May 5, 2024
Fixes: nodejs/node#43274

PR-URL: nodejs/node#43336
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@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. 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.

require throws, but require.resolve does not