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

getPackageType in web/packages/config-loader/src/getPackageType.js can loop forever #2873

Open
tiny-james opened this issue Jan 30, 2025 · 0 comments

Comments

@tiny-james
Copy link

If the working directory of Web Test Runner and its parent directories do not contain a package.json file the getPackageType function will loop forever.

This happens because path.resolve('/', '..') will return / without throwing any errors, hence the currentPath variable will keep the same value and the loop will never exit.

Suggested Fix

Add a check that the path has changed when the parent path is resolved.

async function getPackageType(basedir) {
  let currentPath = basedir;
  try {
    while (await fileExists(currentPath)) {
      const pkgJsonPath = path.join(currentPath, 'package.json');

      if (await fileExists(pkgJsonPath)) {
        const pkgJsonString = await fs.readFile(pkgJsonPath, { encoding: 'utf-8' });
        const pkgJson = JSON.parse(pkgJsonString);
        return pkgJson.type || 'commonjs';
      }
      const oldPath = currentPath;
      currentPath = path.resolve(oldPath, '..');
      if (currentPath === oldPath) {
         return 'commonjs';
     }
    }
  } catch (e) {
    // don't log any error
  }
  return 'commonjs';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant