-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
loader: fix package resolution for edge case #41218
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
Changes from 10 commits
a149086
db7a12a
6f7d871
435dc9b
f9fe067
0bb04f2
a72ad73
05d7f87
16f422f
a4c3456
9a45c2b
8b741f0
01a3abc
fdf0d4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -464,22 +464,6 @@ const patternRegEx = /\*/g; | |
| function resolvePackageTargetString( | ||
| target, subpath, match, packageJSONUrl, base, pattern, internal, conditions) { | ||
|
|
||
| const composeResult = (resolved) => { | ||
| let format; | ||
| try { | ||
| format = getPackageType(resolved); | ||
| } catch (err) { | ||
| if (err.code === 'ERR_INVALID_FILE_URL_PATH') { | ||
| const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER( | ||
| resolved, 'must not include encoded "/" or "\\" characters', base); | ||
| invalidModuleErr.cause = err; | ||
| throw invalidModuleErr; | ||
| } | ||
| throw err; | ||
| } | ||
| return { resolved, ...(format !== 'none') && { format } }; | ||
| }; | ||
|
|
||
| if (subpath !== '' && !pattern && target[target.length - 1] !== '/') | ||
| throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); | ||
|
|
||
|
|
@@ -512,7 +496,7 @@ function resolvePackageTargetString( | |
| if (!StringPrototypeStartsWith(resolvedPath, packagePath)) | ||
| throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); | ||
|
|
||
| if (subpath === '') return composeResult(resolved); | ||
| if (subpath === '') return resolved; | ||
|
|
||
| if (RegExpPrototypeTest(invalidSegmentRegEx, subpath)) { | ||
| const request = pattern ? | ||
|
|
@@ -521,12 +505,16 @@ function resolvePackageTargetString( | |
| } | ||
|
|
||
| if (pattern) { | ||
| return composeResult(new URL(RegExpPrototypeSymbolReplace(patternRegEx, | ||
| resolved.href, | ||
| () => subpath))); | ||
| return new URL( | ||
| RegExpPrototypeSymbolReplace( | ||
| patternRegEx, | ||
| resolved.href, | ||
| () => subpath | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return composeResult(new URL(subpath, resolved)); | ||
| return new URL(subpath, resolved); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -753,7 +741,7 @@ function packageImportsResolve(name, base, conditions) { | |
| packageJSONUrl, imports[name], '', name, base, false, true, conditions | ||
| ); | ||
| if (resolveResult != null) { | ||
| return resolveResult.resolved; | ||
| return resolveResult; | ||
| } | ||
| } else { | ||
| let bestMatch = ''; | ||
|
|
@@ -785,7 +773,7 @@ function packageImportsResolve(name, base, conditions) { | |
| bestMatch, base, true, | ||
| true, conditions); | ||
| if (resolveResult != null) { | ||
| return resolveResult.resolved; | ||
| return resolveResult; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -849,7 +837,7 @@ function parsePackageName(specifier, base) { | |
| */ | ||
| function packageResolve(specifier, base, conditions) { | ||
| if (NativeModule.canBeRequiredByUsers(specifier)) | ||
| return { resolved: new URL('node:' + specifier) }; | ||
| return new URL('node:' + specifier); | ||
|
|
||
| const { packageName, packageSubpath, isScoped } = | ||
| parsePackageName(specifier, base); | ||
|
|
@@ -888,19 +876,14 @@ function packageResolve(specifier, base, conditions) { | |
| packageJSONUrl, packageSubpath, packageConfig, base, conditions); | ||
| } | ||
| if (packageSubpath === '.') { | ||
| return { | ||
| resolved: legacyMainResolve( | ||
| packageJSONUrl, | ||
| packageConfig, | ||
| base), | ||
| ...(packageConfig.type !== 'none') && { format: packageConfig.type } | ||
| }; | ||
| return legacyMainResolve( | ||
| packageJSONUrl, | ||
| packageConfig, | ||
| base | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| resolved: new URL(packageSubpath, packageJSONUrl), | ||
| ...(packageConfig.type !== 'none') && { format: packageConfig.type } | ||
| }; | ||
| return new URL(packageSubpath, packageJSONUrl); | ||
| // Cross-platform root check. | ||
| } while (packageJSONPath.length !== lastPath.length); | ||
|
|
||
|
|
@@ -933,6 +916,12 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |
| return isRelativeSpecifier(specifier); | ||
| } | ||
|
|
||
| function amendFormatToUrl(resolved) { | ||
| const format = getModuleFileFormat(resolved, false); | ||
|
|
||
| return { resolved, ...(format !== null) && { format } }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add this code snippet at the top of Reflect.defineProperty(Object.prototype, 'format', {
get: common.mustNotCall('get Object.prototype.format'),
})And wrap all
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot commit this because the tests would fail. For example here. |
||
| } | ||
|
|
||
| /** | ||
| * @param {string} specifier | ||
| * @param {string | URL | undefined} base | ||
|
|
@@ -945,6 +934,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |
| // Ok since relative URLs cannot parse as URLs. | ||
| let resolved; | ||
| let format; | ||
|
|
||
| if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | ||
| resolved = new URL(specifier, base); | ||
| } else if (specifier[0] === '#') { | ||
|
|
@@ -953,7 +943,13 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |
| try { | ||
| resolved = new URL(specifier); | ||
| } catch { | ||
| ({ resolved, format } = packageResolve(specifier, base, conditions)); | ||
| ({ resolved, format } = | ||
| amendFormatToUrl( | ||
|
guybedford marked this conversation as resolved.
Outdated
|
||
| packageResolve( | ||
| specifier, | ||
| base, | ||
| conditions | ||
| ))); | ||
| } | ||
| } | ||
| if (resolved.protocol !== 'file:') { | ||
|
|
@@ -1107,4 +1103,7 @@ module.exports = { | |
| }; | ||
|
|
||
| // cycle | ||
| const { defaultGetFormat } = require('internal/modules/esm/get_format'); | ||
| const { | ||
| defaultGetFormat, | ||
| getModuleFileFormat, | ||
| } = require('internal/modules/esm/get_format'); | ||
Uh oh!
There was an error while loading. Please reload this page.