Repository navigation
Conversation
Package imports such as `"#path": "path"` resolve to a `node:` URL.
The CJS loader passed that URL to `fileURLToPath()`, which only accepts
`file:` URLs, so `require('#path')` threw `ERR_INVALID_URL_SCHEME`
while `import('#path')` worked.
Return the normalized builtin id when the resolved URL uses the `node:`
protocol. This mirrors the ESM resolver, which returns non-file URLs
without finalizing them as file paths.
Add a regression test covering both `require()` and `import()` of a
package import that targets a builtin.
Fixes: nodejs#49257
Signed-off-by: neoLsH <906344049@qq.com>
Collaborator
|
Review requested:
|
Contributor
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #49257.
require()of a package import that resolves to a builtin module (for example"#path": "path") currently throws:packageImportsResolve()returns anode:pathURL.finalizeEsmResolution()then passes it tofileURLToPath(), which only acceptsfile:URLs. The ESM resolver already returns non-file URLs frompackageImportsResolve()without callingfinalizeResolution().This change makes the CJS loader return the normalized builtin id when the resolved URL uses the
node:protocol, so the existing builtin loading path handles it.Test: added
#builtinto thepkgimportsfixture and covered bothrequire('#builtin')andimport('#builtin')intest/es-module/test-esm-imports.mjs.Verification (local build of v26.14.0 source with this patch; the patched function is unchanged on current main):
node assert.cjs->ERR_INVALID_URL_SCHEME;node assert.mjs-> OK.out/Release/node test/es-module/test-esm-imports.mjs-> pass.python3 tools/test.py test/es-module/test-esm-imports.mjs->+1 -0.git diff --check-> clean.AI disclosure: I used an AI coding assistant to help investigate and draft this change. I reviewed the diff and personally verified the failing and passing test results above.