microsoft/rushstack

[eslint-patch] bulk suppressions patch fails to find eslintrc in git root

SpenserJ opened this issue · 0 comments

Summary

When trying to use @rushstack/eslint-patch/eslint-bulk-suppressions in a package that is not a monorepo, the .eslintrc.js may be at the root of the git repository. Enabling the patch for bulk suppression will throw an error of Cannot locate eslintrc

Repro steps

Reproduction: https://github.com/SpenserJ/rushstack/tree/repro-bulk-suppression-in-polyrepo

  1. Create a new repository with a new package (not a workspace/monorepo)
  2. Install ESLint and @rushstack/eslint-patch, and configure it with the bulk suppression patch and any ESLint config or preset you'd like
  3. Create a file that will result in a linting error with your config
  4. Run ESLint against that file

Expected result: The linter should run and report any linting errors

Actual result: Error: Cannot locate eslintrc

Details

function findEslintrcDirectory(fileAbsolutePath: string): string {
for (let currentDir = fileAbsolutePath; currentDir !== GitRootPath; currentDir = path.dirname(currentDir))
if (['.eslintrc.js', '.eslintrc.cjs'].some((eslintrc) => fs.existsSync(path.join(currentDir, eslintrc))))
return currentDir;
throw new Error('Cannot locate eslintrc');
}

In a polyrepo, the eslintrc file will usually be in the root directory of the git repository, however findEslintrcDirectory will stop as soon as it has reached the GitRootPath. Changing this to check if we're outside/above the root path resolves this issue:

- for (let currentDir = fileAbsolutePath; currentDir !== GitRootPath; currentDir = path.dirname(currentDir)) 
+ for (let currentDir = fileAbsolutePath; currentDir.startsWith(GitRootPath); currentDir = path.dirname(currentDir)) 

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@rushstack/eslint-config version? N/A
Operating system? Mac
Would you consider contributing a PR? Yes
TypeScript compiler version? N/A
Node.js version (node -v)? v20.10.0