-
-
Notifications
You must be signed in to change notification settings - Fork 32k
esm: fix hint on invalid module specifier #51223
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 all commits
1726a56
7324ce9
88e72c5
226a945
67439d1
0523d28
38f4580
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,11 +3,10 @@ | |||||
const { | ||||||
ArrayIsArray, | ||||||
ArrayPrototypeJoin, | ||||||
ArrayPrototypeShift, | ||||||
ArrayPrototypeMap, | ||||||
JSONStringify, | ||||||
ObjectGetOwnPropertyNames, | ||||||
ObjectPrototypeHasOwnProperty, | ||||||
RegExp, | ||||||
RegExpPrototypeExec, | ||||||
RegExpPrototypeSymbolReplace, | ||||||
SafeMap, | ||||||
|
@@ -21,6 +20,7 @@ const { | |||||
StringPrototypeSlice, | ||||||
StringPrototypeSplit, | ||||||
StringPrototypeStartsWith, | ||||||
encodeURIComponent, | ||||||
} = primordials; | ||||||
const internalFS = require('internal/fs/utils'); | ||||||
const { BuiltinModule } = require('internal/bootstrap/realm'); | ||||||
|
@@ -30,7 +30,7 @@ const { getOptionValue } = require('internal/options'); | |||||
const policy = getOptionValue('--experimental-policy') ? | ||||||
require('internal/process/policy') : | ||||||
null; | ||||||
const { sep, relative, toNamespacedPath, resolve } = require('path'); | ||||||
const { sep, posix: { relative: relativePosixPath }, toNamespacedPath, resolve } = require('path'); | ||||||
const preserveSymlinks = getOptionValue('--preserve-symlinks'); | ||||||
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | ||||||
const experimentalNetworkImports = | ||||||
|
@@ -912,6 +912,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |||||
* Try to resolve an import as a CommonJS module. | ||||||
* @param {string} specifier - The specifier to resolve. | ||||||
* @param {string} parentURL - The base URL. | ||||||
* @returns {string | Buffer | false} | ||||||
*/ | ||||||
function resolveAsCommonJS(specifier, parentURL) { | ||||||
try { | ||||||
|
@@ -924,29 +925,38 @@ function resolveAsCommonJS(specifier, parentURL) { | |||||
// If it is a relative specifier return the relative path | ||||||
// to the parent | ||||||
if (isRelativeSpecifier(specifier)) { | ||||||
found = relative(parent, found); | ||||||
// Add '.separator if the path does not start with '..separator' | ||||||
const foundURL = pathToFileURL(found).pathname; | ||||||
found = relativePosixPath( | ||||||
StringPrototypeSlice(parentURL, 'file://'.length, StringPrototypeLastIndexOf(parentURL, '/')), | ||||||
foundURL); | ||||||
|
||||||
// Add './' if the path does not start with '../' | ||||||
// This should be a safe assumption because when loading | ||||||
// esm modules there should be always a file specified so | ||||||
// there should not be a specifier like '..' or '.' | ||||||
if (!StringPrototypeStartsWith(found, `..${sep}`)) { | ||||||
found = `.${sep}${found}`; | ||||||
if (!StringPrototypeStartsWith(found, '../')) { | ||||||
found = `./${found}`; | ||||||
} | ||||||
} else if (isBareSpecifier(specifier)) { | ||||||
// If it is a bare specifier return the relative path within the | ||||||
// module | ||||||
const pkg = StringPrototypeSplit(specifier, '/')[0]; | ||||||
const index = StringPrototypeIndexOf(found, pkg); | ||||||
const i = StringPrototypeIndexOf(specifier, '/'); | ||||||
const pkg = i === -1 ? specifier : StringPrototypeSlice(specifier, 0, i); | ||||||
guybedford marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const needle = `${sep}node_modules${sep}${pkg}${sep}`; | ||||||
const index = StringPrototypeLastIndexOf(found, needle); | ||||||
if (index !== -1) { | ||||||
found = StringPrototypeSlice(found, index); | ||||||
found = pkg + '/' + ArrayPrototypeJoin( | ||||||
ArrayPrototypeMap( | ||||||
StringPrototypeSplit(StringPrototypeSlice(found, index + needle.length), sep), | ||||||
// Escape URL-special characters to avoid generating a incorrect suggestion | ||||||
encodeURIComponent, | ||||||
), | ||||||
'/', | ||||||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
); | ||||||
} else { | ||||||
found = `${pathToFileURL(found)}`; | ||||||
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. Nit: This might be a little bit more readable.
Suggested change
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 think we try to always "coerce to string" in core rather than calling the |
||||||
} | ||||||
} | ||||||
// Normalize the path separator to give a valid suggestion | ||||||
// on Windows | ||||||
if (process.platform === 'win32') { | ||||||
found = RegExpPrototypeSymbolReplace(new RegExp(`\\${sep}`, 'g'), | ||||||
found, '/'); | ||||||
} | ||||||
return found; | ||||||
} catch { | ||||||
return false; | ||||||
|
@@ -1154,14 +1164,14 @@ function defaultResolve(specifier, context = {}) { | |||||
*/ | ||||||
function decorateErrorWithCommonJSHints(error, specifier, parentURL) { | ||||||
const found = resolveAsCommonJS(specifier, parentURL); | ||||||
if (found) { | ||||||
if (found && found !== specifier) { // Don't suggest the same input the user provided. | ||||||
// Modify the stack and message string to include the hint | ||||||
const lines = StringPrototypeSplit(error.stack, '\n'); | ||||||
const hint = `Did you mean to import ${found}?`; | ||||||
const endOfFirstLine = StringPrototypeIndexOf(error.stack, '\n'); | ||||||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const hint = `Did you mean to import ${JSONStringify(found)}?`; | ||||||
error.stack = | ||||||
ArrayPrototypeShift(lines) + '\n' + | ||||||
hint + '\n' + | ||||||
ArrayPrototypeJoin(lines, '\n'); | ||||||
StringPrototypeSlice(error.stack, 0, endOfFirstLine) + '\n' + | ||||||
hint + | ||||||
StringPrototypeSlice(error.stack, endOfFirstLine); | ||||||
error.message += `\n${hint}`; | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use strict'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.