Skip to content

Commit

Permalink
fix(linter): fix plugin race condition (#27810)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

There is a race condition in the `@nx/eslint/plugin` causing projects to
be missing lint.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The `@nx/eslint/plugin` creates a map of lintable files up front before
analyzing any particular project and there is no race condition.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored Sep 6, 2024
1 parent ac9da41 commit 035b406
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions packages/eslint/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,11 @@ const internalCreateNodes = async (
};
};

let collectingLintableFilesPromise: Promise<void>;
const internalCreateNodesV2 = async (
configFilePath: string,
options: EslintPluginOptions,
context: CreateNodesContextV2,
eslintConfigFiles: string[],
allProjectRoots: string[],
projectRootsByEslintRoots: Map<string, string[]>,
lintableFilesPerProjectRoot: Map<string, string[]>,
projectsCache: Record<string, CreateNodesResult['projects']>
Expand Down Expand Up @@ -208,17 +206,6 @@ const internalCreateNodesV2 = async (
return;
}

if (!lintableFilesPerProjectRoot.size) {
collectingLintableFilesPromise ??= collectLintableFilesByProjectRoot(
lintableFilesPerProjectRoot,
allProjectRoots,
options,
context
);
await collectingLintableFilesPromise;
collectingLintableFilesPromise = null;
}

const eslint = new ESLint({
cwd: join(context.workspaceRoot, projectRoot),
});
Expand Down Expand Up @@ -273,8 +260,11 @@ export const createNodesV2: CreateNodesV2<EslintPluginOptions> = [

const { eslintConfigFiles, projectRoots, projectRootsByEslintRoots } =
splitConfigFiles(configFiles);
const lintableFilesPerProjectRoot = new Map<string, string[]>();

const lintableFilesPerProjectRoot = await collectLintableFilesByProjectRoot(
projectRoots,
options,
context
);
try {
return await createNodesFromFiles(
(configFile, options, context) =>
Expand All @@ -283,7 +273,6 @@ export const createNodesV2: CreateNodesV2<EslintPluginOptions> = [
options,
context,
eslintConfigFiles,
projectRoots,
projectRootsByEslintRoots,
lintableFilesPerProjectRoot,
targetsCache
Expand Down Expand Up @@ -360,11 +349,12 @@ function groupProjectRootsByEslintRoots(
}

async function collectLintableFilesByProjectRoot(
lintableFilesPerProjectRoot: Map<string, string[]>,
projectRoots: string[],
options: EslintPluginOptions,
context: CreateNodesContext | CreateNodesContextV2
): Promise<void> {
): Promise<Map<string, string[]>> {
const lintableFilesPerProjectRoot = new Map<string, string[]>();

const lintableFiles = await globWithWorkspaceContext(context.workspaceRoot, [
`**/*.{${options.extensions.join(',')}}`,
]);
Expand All @@ -382,6 +372,8 @@ async function collectLintableFilesByProjectRoot(
lintableFilesPerProjectRoot.get(projectRoot).push(file);
}
}

return lintableFilesPerProjectRoot;
}

function getRootForDirectory(
Expand Down

0 comments on commit 035b406

Please sign in to comment.