Skip to content

Commit

Permalink
fix(core): handle undefined when package.json changes are not in node…
Browse files Browse the repository at this point in the history
… modules (#13681)
  • Loading branch information
jericopingul committed Dec 12, 2022
1 parent 7a70d52 commit af157ff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
@@ -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';

Expand Down Expand Up @@ -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.'
);
});
});
14 changes: 14 additions & 0 deletions packages/nx/src/project-graph/affected/locators/npm-packages.ts
Expand Up @@ -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<
Expand All @@ -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) &&
Expand All @@ -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/')) {
Expand All @@ -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;
};

1 comment on commit af157ff

@vercel
Copy link

@vercel vercel bot commented on af157ff Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.