Skip to content

Commit

Permalink
fix(core): handle local workspace projects in package.json for affect…
Browse files Browse the repository at this point in the history
…ed locator (#13728)

(cherry picked from commit 65ecd9c)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Dec 19, 2022
1 parent 4309873 commit 9c9328d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Expand Up @@ -266,6 +266,45 @@ describe('getTouchedNpmPackages', () => {
'npm:awesome-nrwl',
]);
});

it('should handle and workspace packages when defined in dependencies', () => {
const result = getTouchedNpmPackages(
[
{
file: 'package.json',
hash: 'some-hash',
getChanges: () => [
{
type: 'JsonPropertyAdded',
path: ['devDependencies', 'changed-test-pkg-name-1'],
value: { rhs: 'workspace:*' },
},
],
},
],
workspaceJson,
nxJson,
{
dependencies: {
'happy-nrwl': '0.0.1',
'awesome-nrwl': '0.0.1',
},
},
{
...projectGraph,
nodes: {
...projectGraph.nodes,
'any-random-name': {
name: 'changed-test-pkg-name-1',
type: 'lib',
data: {},
},
},
}
);
expect(result).toEqual(['changed-test-pkg-name-1']);
});

it('should handle and log workspace package.json changes when the changes are not in `npmPackages` (projectGraph.externalNodes)', () => {
jest.spyOn(logger, 'warn');
expect(() => {
Expand Down
14 changes: 11 additions & 3 deletions packages/nx/src/project-graph/affected/locators/npm-packages.ts
Expand Up @@ -6,6 +6,10 @@ import {
} from '../../../utils/json-diff';
import { logger } from '../../../utils/logger';
import { TouchedProjectLocator } from '../affected-project-graph-models';
import {
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from 'nx/src/config/project-graph';

export const getTouchedNpmPackages: TouchedProjectLocator<
WholeFileChange | JsonChange
Expand All @@ -31,9 +35,13 @@ export const getTouchedNpmPackages: TouchedProjectLocator<
touched = Object.keys(projectGraph.nodes);
break;
} else {
const npmPackage = npmPackages.find(
(pkg) => pkg.data.packageName === c.path[1]
);
let npmPackage: ProjectGraphProjectNode | ProjectGraphExternalNode =
npmPackages.find((pkg) => pkg.data.packageName === c.path[1]);
if (!npmPackage) {
// dependency can also point to a workspace project
const nodes = Object.values(projectGraph.nodes);
npmPackage = nodes.find((n) => n.name === c.path[1]);
}
if (!npmPackage) {
missingTouchedNpmPackages.push(c.path[1]);
continue;
Expand Down

0 comments on commit 9c9328d

Please sign in to comment.