From 035b40620b6badc50fa60fc20f44a0f21d71d219 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 6 Sep 2024 18:07:56 -0400 Subject: [PATCH] fix(linter): fix plugin race condition (#27810) ## Current Behavior There is a race condition in the `@nx/eslint/plugin` causing projects to be missing lint. ## Expected Behavior 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) Fixes # --- packages/eslint/src/plugins/plugin.ts | 28 ++++++++++----------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/packages/eslint/src/plugins/plugin.ts b/packages/eslint/src/plugins/plugin.ts index 83db17d74d0ee..6c61cab70d3fb 100644 --- a/packages/eslint/src/plugins/plugin.ts +++ b/packages/eslint/src/plugins/plugin.ts @@ -169,13 +169,11 @@ const internalCreateNodes = async ( }; }; -let collectingLintableFilesPromise: Promise; const internalCreateNodesV2 = async ( configFilePath: string, options: EslintPluginOptions, context: CreateNodesContextV2, eslintConfigFiles: string[], - allProjectRoots: string[], projectRootsByEslintRoots: Map, lintableFilesPerProjectRoot: Map, projectsCache: Record @@ -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), }); @@ -273,8 +260,11 @@ export const createNodesV2: CreateNodesV2 = [ const { eslintConfigFiles, projectRoots, projectRootsByEslintRoots } = splitConfigFiles(configFiles); - const lintableFilesPerProjectRoot = new Map(); - + const lintableFilesPerProjectRoot = await collectLintableFilesByProjectRoot( + projectRoots, + options, + context + ); try { return await createNodesFromFiles( (configFile, options, context) => @@ -283,7 +273,6 @@ export const createNodesV2: CreateNodesV2 = [ options, context, eslintConfigFiles, - projectRoots, projectRootsByEslintRoots, lintableFilesPerProjectRoot, targetsCache @@ -360,11 +349,12 @@ function groupProjectRootsByEslintRoots( } async function collectLintableFilesByProjectRoot( - lintableFilesPerProjectRoot: Map, projectRoots: string[], options: EslintPluginOptions, context: CreateNodesContext | CreateNodesContextV2 -): Promise { +): Promise> { + const lintableFilesPerProjectRoot = new Map(); + const lintableFiles = await globWithWorkspaceContext(context.workspaceRoot, [ `**/*.{${options.extensions.join(',')}}`, ]); @@ -382,6 +372,8 @@ async function collectLintableFilesByProjectRoot( lintableFilesPerProjectRoot.get(projectRoot).push(file); } } + + return lintableFilesPerProjectRoot; } function getRootForDirectory(