rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 568 forks source link

Infinite loop in combination with virtual module and missing package.json #1647

Open dasdeo opened 6 months ago

dasdeo commented 6 months ago

Expected Behavior

Bundling should work the same regardless from where the bundler was invoked.

Actual Behavior

If the plugin fails to find a corresponding package.json it will hang forever.

Note that this is not remedied by setting rootDir.

Additional Information

I already tracked down the offending function, it is:

async function findPackageJson(base, moduleDirs) {
    const { root } = path.parse(base);
    let current = base;
    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 };
        }
        current = path.resolve(current, '..');
    }
    return null;
}

Variable current over time:

findPackageJson my-virtual-module
findPackageJson /Users/dasdeo/Desktop/rollup-bug-repo
findPackageJson /Users/dasdeo/Desktop
findPackageJson /Users/dasdeo
findPackageJson /Users
findPackageJson /
findPackageJson /
findPackageJson /
...