Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle undefined when package.json changes are not in node modules #13681

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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;
};