From 96fca453b424dd6727ff7ea8c4786dc71798e3df Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Mon, 1 May 2023 16:46:26 -0400 Subject: [PATCH] fix(core): improve touched projects locator performance (#16682) --- .../affected/locators/project-glob-changes.ts | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/nx/src/project-graph/affected/locators/project-glob-changes.ts b/packages/nx/src/project-graph/affected/locators/project-glob-changes.ts index 566ef31dd9b86..f5fd0955e9bbf 100644 --- a/packages/nx/src/project-graph/affected/locators/project-glob-changes.ts +++ b/packages/nx/src/project-graph/affected/locators/project-glob-changes.ts @@ -1,9 +1,3 @@ -import { - DeletedFileChange, - isDeletedFileChange, - WholeFileChange, -} from '../../file-utils'; -import { JsonChange } from '../../../utils/json-diff'; import { TouchedProjectLocator } from '../affected-project-graph-models'; import minimatch = require('minimatch'); import { @@ -12,40 +6,42 @@ import { } from '../../../config/workspaces'; import { workspaceRoot } from '../../../utils/workspace-root'; import { getNxRequirePaths } from '../../../utils/installation-directory'; +import { join } from 'path'; +import { existsSync } from 'fs'; -export const getTouchedProjectsFromProjectGlobChanges: TouchedProjectLocator< - WholeFileChange | JsonChange | DeletedFileChange -> = async (touchedFiles, projectGraphNodes, nxJson): Promise => { - const pluginGlobPatterns = await getGlobPatternsFromPluginsAsync( - nxJson, - getNxRequirePaths(), - workspaceRoot - ); - const workspacesGlobPatterns = - getGlobPatternsFromPackageManagerWorkspaces(workspaceRoot) || []; +export const getTouchedProjectsFromProjectGlobChanges: TouchedProjectLocator = + async (touchedFiles, projectGraphNodes, nxJson): Promise => { + const pluginGlobPatterns = await getGlobPatternsFromPluginsAsync( + nxJson, + getNxRequirePaths(), + workspaceRoot + ); + const workspacesGlobPatterns = + getGlobPatternsFromPackageManagerWorkspaces(workspaceRoot) || []; - const patterns = [ - '**/project.json', - ...pluginGlobPatterns, - ...workspacesGlobPatterns, - ]; - const combinedGlobPattern = - patterns.length === 1 ? '**/project.json' : '{' + patterns.join(',') + '}'; - const touchedProjects = new Set(); - for (const touchedFile of touchedFiles) { - const isProjectFile = minimatch(touchedFile.file, combinedGlobPattern); - if (isProjectFile) { - if ( - touchedFile.getChanges().some((change) => isDeletedFileChange(change)) - ) { - // If any project has been deleted, we must assume all projects were affected - return Object.keys(projectGraphNodes); - } + const patterns = [ + '**/project.json', + ...pluginGlobPatterns, + ...workspacesGlobPatterns, + ]; + const combinedGlobPattern = + patterns.length === 1 + ? '**/project.json' + : '{' + patterns.join(',') + '}'; + const touchedProjects = new Set(); + for (const touchedFile of touchedFiles) { + const isProjectFile = minimatch(touchedFile.file, combinedGlobPattern); + if (isProjectFile) { + // If the file no longer exists on disk, then it was deleted + if (!existsSync(join(workspaceRoot, touchedFile.file))) { + // If any project has been deleted, we must assume all projects were affected + return Object.keys(projectGraphNodes); + } - // Modified project config files are under a project's root, and implicitly - // mark it as affected. Thus, we don't need to handle it here. + // Modified project config files are under a project's root, and implicitly + // mark it as affected. Thus, we don't need to handle it here. + } } - } - return Array.from(touchedProjects); -}; + return Array.from(touchedProjects); + };