From af157ffa2bf2d9efd2ddaf7a0dd6f3ff5108b4bf Mon Sep 17 00:00:00 2001 From: Jerico Pingul Date: Mon, 12 Dec 2022 15:52:21 +0000 Subject: [PATCH] fix(core): handle undefined when package.json changes are not in node modules (#13681) --- .../affected/locators/npm-packages.spec.ts | 38 +++++++++++++++++++ .../affected/locators/npm-packages.ts | 14 +++++++ 2 files changed, 52 insertions(+) diff --git a/packages/nx/src/project-graph/affected/locators/npm-packages.spec.ts b/packages/nx/src/project-graph/affected/locators/npm-packages.spec.ts index 97dba48a8befd..58a9d12612226 100644 --- a/packages/nx/src/project-graph/affected/locators/npm-packages.spec.ts +++ b/packages/nx/src/project-graph/affected/locators/npm-packages.spec.ts @@ -1,6 +1,7 @@ import { NxJsonConfiguration } from '../../../config/nx-json'; import { ProjectGraph } from '../../../config/project-graph'; import { JsonDiffType } from '../../../utils/json-diff'; +import { logger } from '../../../utils/logger'; import { WholeFileChange } from '../../file-utils'; import { getTouchedNpmPackages } from './npm-packages'; @@ -265,4 +266,41 @@ describe('getTouchedNpmPackages', () => { 'npm:awesome-nrwl', ]); }); + it('should handle and log workspace package.json changes when the changes are not in `npmPackages` (projectGraph.externalNodes)', () => { + jest.spyOn(logger, 'warn'); + expect(() => { + getTouchedNpmPackages( + [ + { + file: 'package.json', + hash: 'some-hash', + getChanges: () => [ + { + type: 'JsonPropertyAdded', + path: ['devDependencies', 'changed-test-pkg-name-1'], + value: { rhs: 'workspace:*' }, + }, + { + type: 'JsonPropertyAdded', + path: ['devDependencies', 'changed-test-pkg-name-2'], + value: { rhs: 'workspace:*' }, + }, + ], + }, + ], + workspaceJson, + nxJson, + { + dependencies: { + 'happy-nrwl': '0.0.1', + 'awesome-nrwl': '0.0.1', + }, + }, + projectGraph + ); + }).not.toThrowError(); + expect(logger.warn).toHaveBeenCalledWith( + 'The affected projects might have not been identified properly. The package(s) changed-test-pkg-name-1, changed-test-pkg-name-2 were not found. Please open an issue in Github including the package.json file.' + ); + }); }); diff --git a/packages/nx/src/project-graph/affected/locators/npm-packages.ts b/packages/nx/src/project-graph/affected/locators/npm-packages.ts index cfec247231cca..3f795ee688793 100644 --- a/packages/nx/src/project-graph/affected/locators/npm-packages.ts +++ b/packages/nx/src/project-graph/affected/locators/npm-packages.ts @@ -4,6 +4,7 @@ import { isJsonChange, JsonChange, } from '../../../utils/json-diff'; +import { logger } from '../../../utils/logger'; import { TouchedProjectLocator } from '../affected-project-graph-models'; export const getTouchedNpmPackages: TouchedProjectLocator< @@ -17,6 +18,8 @@ export const getTouchedNpmPackages: TouchedProjectLocator< const npmPackages = Object.values(projectGraph.externalNodes); + const missingTouchedNpmPackages: string[] = []; + for (const c of changes) { if ( isJsonChange(c) && @@ -31,6 +34,10 @@ export const getTouchedNpmPackages: TouchedProjectLocator< const npmPackage = npmPackages.find( (pkg) => pkg.data.packageName === c.path[1] ); + if (!npmPackage) { + missingTouchedNpmPackages.push(c.path[1]); + continue; + } touched.push(npmPackage.name); // If it was a type declarations package then also mark its corresponding implementation package as affected if (npmPackage.name.startsWith('npm:@types/')) { @@ -49,5 +56,12 @@ export const getTouchedNpmPackages: TouchedProjectLocator< } } + if (missingTouchedNpmPackages.length) { + logger.warn( + `The affected projects might have not been identified properly. The package(s) ${missingTouchedNpmPackages.join( + ', ' + )} were not found. Please open an issue in Github including the package.json file.` + ); + } return touched; };