-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix(resolve): check nested directories for package.json #5665
Changes from all commits
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 |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
isBuiltin, | ||
bareImportRE, | ||
createDebugger, | ||
deepImportRE, | ||
injectQuery, | ||
isExternalUrl, | ||
isObject, | ||
|
@@ -505,13 +504,35 @@ export function tryNodeResolve( | |
const nestedRoot = id.substring(0, lastArrowIndex).trim() | ||
const nestedPath = id.substring(lastArrowIndex + 1).trim() | ||
|
||
// check for deep import, e.g. "my-lib/foo" | ||
const deepMatch = nestedPath.match(deepImportRE) | ||
const possiblePkgIds: string[] = [] | ||
for (let prevSlashIndex = -1; ; ) { | ||
let slashIndex = nestedPath.indexOf('/', prevSlashIndex + 1) | ||
if (slashIndex < 0) { | ||
slashIndex = nestedPath.length | ||
} | ||
|
||
const part = nestedPath.slice( | ||
prevSlashIndex + 1, | ||
(prevSlashIndex = slashIndex) | ||
) | ||
if (!part) { | ||
break | ||
} | ||
|
||
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : nestedPath | ||
// Assume path parts with an extension are not package roots, except for the | ||
// first path part (since periods are sadly allowed in package names). | ||
// At the same time, skip the first path part if it begins with "@" | ||
// (since "@foo/bar" should be treated as the top-level path). | ||
if (possiblePkgIds.length ? path.extname(part) : part[0] === '@') { | ||
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. Sadly this causes bugs in the case of Do you have a specific test case for the problem that this PR was trying to fix? 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. Would you open an issue for it? 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. 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.
Did you test that theory? If I remember right, the 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. Also 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.
Yeah, I've manually confirmed. 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. For Unfortunately, So 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.
That would only be possible if 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. Oh sorry, then I'm mistaken on that one. But the issue does get fixed after reverting this commit… 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 don't think I have fully understood this issue. diff --git a/node_modules/vite/dist/node/chunks/dep-fcec4469.js b/node_modules/vite/dist/node/chunks/dep-fcec4469.js
index d16f7d4..828ef47 100644
--- a/node_modules/vite/dist/node/chunks/dep-fcec4469.js
+++ b/node_modules/vite/dist/node/chunks/dep-fcec4469.js
@@ -30385,28 +30385,12 @@ function tryNodeResolve(id, importer, options, targetWeb, server, ssr) {
const lastArrowIndex = id.lastIndexOf('>');
const nestedRoot = id.substring(0, lastArrowIndex).trim();
const nestedPath = id.substring(lastArrowIndex + 1).trim();
- const possiblePkgIds = [];
- for (let prevSlashIndex = -1;;) {
- let slashIndex = nestedPath.indexOf('/', prevSlashIndex + 1);
- if (slashIndex < 0) {
- slashIndex = nestedPath.length;
- }
- const part = nestedPath.slice(prevSlashIndex + 1, (prevSlashIndex = slashIndex));
- if (!part) {
- break;
- }
- // Assume path parts with an extension are not package roots, except for the
- // first path part (since periods are sadly allowed in package names).
- // At the same time, skip the first path part if it begins with "@"
- // (since "@foo/bar" should be treated as the top-level path).
- if (possiblePkgIds.length ? path__default.extname(part) : part[0] === '@') {
- continue;
- }
- const possiblePkgId = nestedPath.slice(0, slashIndex);
- possiblePkgIds.push(possiblePkgId);
- }
+ // check for deep import, e.g. "my-lib/foo"
+ const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//
+ const deepMatch = nestedPath.match(deepImportRE)
+ const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : nestedPath
let basedir;
- if (dedupe === null || dedupe === void 0 ? void 0 : dedupe.some((id) => possiblePkgIds.includes(id))) {
+ if (dedupe && dedupe.includes(pkgId)) {
basedir = root;
}
else if (importer &&
@@ -30421,19 +30405,16 @@ function tryNodeResolve(id, importer, options, targetWeb, server, ssr) {
if (nestedRoot) {
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks);
}
- let pkg;
- const pkgId = possiblePkgIds.reverse().find((pkgId) => {
- pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache);
- return pkg;
- });
+ const pkg = resolvePackageData(pkgId, basedir, options.preserveSymlinks)
+
if (!pkg) {
return;
}
let resolveId = resolvePackageEntry;
- let unresolvedId = pkgId;
- if (unresolvedId !== nestedPath) {
+ let unresolvedId = id;
+ if (deepMatch) {
resolveId = resolveDeepImport;
- unresolvedId = '.' + nestedPath.slice(pkgId.length);
+ unresolvedId = '.' + id.slice(pkgId.length);
}
let resolved;
try { |
||
continue | ||
} | ||
|
||
const possiblePkgId = nestedPath.slice(0, slashIndex) | ||
possiblePkgIds.push(possiblePkgId) | ||
} | ||
|
||
let basedir: string | ||
if (dedupe && dedupe.includes(pkgId)) { | ||
if (dedupe?.some((id) => possiblePkgIds.includes(id))) { | ||
basedir = root | ||
} else if ( | ||
importer && | ||
|
@@ -528,21 +549,33 @@ export function tryNodeResolve( | |
basedir = nestedResolveFrom(nestedRoot, basedir, options.preserveSymlinks) | ||
} | ||
|
||
const pkg = resolvePackageData(pkgId, basedir, options.preserveSymlinks) | ||
let pkg: PackageData | undefined | ||
const pkgId = possiblePkgIds.reverse().find((pkgId) => { | ||
pkg = resolvePackageData(pkgId, basedir, options.preserveSymlinks) | ||
return pkg | ||
patak-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
})! | ||
|
||
if (!pkg) { | ||
return | ||
} | ||
|
||
let resolved = deepMatch | ||
? resolveDeepImport( | ||
'.' + id.slice(pkgId.length), | ||
pkg, | ||
options, | ||
targetWeb, | ||
options.preserveSymlinks | ||
) | ||
: resolvePackageEntry(id, pkg, options, targetWeb, options.preserveSymlinks) | ||
let resolved = | ||
nestedPath !== pkgId | ||
? resolveDeepImport( | ||
'.' + nestedPath.slice(pkgId.length), | ||
pkg, | ||
options, | ||
targetWeb, | ||
options.preserveSymlinks | ||
) | ||
: resolvePackageEntry( | ||
nestedPath, | ||
pkg, | ||
options, | ||
targetWeb, | ||
options.preserveSymlinks | ||
) | ||
|
||
if (!resolved) { | ||
return | ||
} | ||
|
@@ -572,7 +605,7 @@ export function tryNodeResolve( | |
!isJsType || | ||
importer?.includes('node_modules') || | ||
exclude?.includes(pkgId) || | ||
exclude?.includes(id) || | ||
exclude?.includes(nestedPath) || | ||
SPECIAL_QUERY_RE.test(resolved) || | ||
ssr | ||
) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this no longer needs to be exported from
utils
. Not sure if it's worth changing that there or not