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
  • Loading branch information
meeroslav committed Dec 9, 2022
1 parent dbc673d commit 00507db
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Expand Up @@ -3,6 +3,7 @@ import { ProjectGraph } from '../../../config/project-graph';
import { JsonDiffType } from '../../../utils/json-diff';
import { WholeFileChange } from '../../file-utils';
import { getTouchedNpmPackages } from './npm-packages';
import { logger } from '../../../utils/logger';

describe('getTouchedNpmPackages', () => {
let workspaceJson;
Expand Down Expand Up @@ -265,4 +266,43 @@ describe('getTouchedNpmPackages', () => {
'npm:awesome-nrwl',
]);
});

it('should handle and workspace packages when defined in dependencies', () => {
jest.spyOn(logger, 'warn');
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']);
});
});
14 changes: 11 additions & 3 deletions packages/nx/src/project-graph/affected/locators/npm-packages.ts
Expand Up @@ -5,6 +5,10 @@ import {
JsonChange,
} from '../../../utils/json-diff';
import { TouchedProjectLocator } from '../affected-project-graph-models';
import {
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from 'nx/src/config/project-graph';

export const getTouchedNpmPackages: TouchedProjectLocator<
WholeFileChange | JsonChange
Expand All @@ -28,9 +32,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]);
}
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 Down

0 comments on commit 00507db

Please sign in to comment.