Skip to content

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

Open
@tiny-james

Description

@tiny-james

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';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions