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

fix(node-resolve): prevent infinite findPackageJson loop #1648

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/node-resolve/src/package/utils.ts
Expand Up @@ -11,14 +11,19 @@ function isModuleDir(current: string, moduleDirs: readonly string[]) {
export async function findPackageJson(base: string, moduleDirs: readonly string[]) {
const { root } = path.parse(base);
let current = base;
let prevCurrent = null;

while (current !== root && !isModuleDir(current, moduleDirs)) {
const pkgJsonPath = path.join(current, 'package.json');
if (await fileExists(pkgJsonPath)) {
const pkgJsonString = fs.readFileSync(pkgJsonPath, 'utf-8');
return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
}
prevCurrent = current;
current = path.resolve(current, '..');

// issue #1647 prevent infinite loop for virtual modules
if (current === '/' && prevCurrent === '/') break;
}
return null;
}
Expand Down