export function getBazelWorkspaceFolder()

in src/bazel/bazel_utils.ts [94:124]


export function getBazelWorkspaceFolder(fsPath: string): string | undefined {
  if (shouldIgnorePath(fsPath)) {
    return undefined;
  }
  let dirname = fsPath;
  let iteration = 0;
  // Fail safe in case other file systems have a base dirname that doesn't
  // match the checks below. Having this failsafe guarantees that we don't
  // hang in an infinite loop.
  const maxIterations = 100;
  if (fs.statSync(fsPath).isFile()) {
    dirname = path.dirname(dirname);
  }
  do {
    const WORKSPACE_FILES = ["WORKSPACE.bazel", "WORKSPACE"];
    for (const workspaceFileName of WORKSPACE_FILES) {
      const workspace = path.join(dirname, workspaceFileName);
      try {
        fs.accessSync(workspace, fs.constants.F_OK);
        // workspace file is accessible. We have found the Bazel workspace
        // directory.
        return dirname;
      } catch (err) {
        // Intentionally do nothing; just try the next parent directory.
      }
    }
    dirname = path.dirname(dirname);
  } while (++iteration < maxIterations && dirname !== "" && dirname !== "/");

  return undefined;
}