Skip to content

Commit

Permalink
fix(core): ensure stale dependencies are pruned in graph (#16533)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Apr 28, 2023
1 parent a7c4009 commit f138a34
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/build-project-graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ describe('project graph', () => {
expect(graph.dependencies).toEqual({
api: [{ source: 'api', target: 'npm:express', type: 'static' }],
demo: [
{ source: 'demo', target: 'api', type: 'implicit' },
{
source: 'demo',
target: 'ui',
Expand All @@ -243,7 +244,6 @@ describe('project graph', () => {
target: 'lazy-lib',
type: 'dynamic',
},
{ source: 'demo', target: 'api', type: 'implicit' },
],
'demo-e2e': [],
'lazy-lib': [],
Expand Down
110 changes: 110 additions & 0 deletions packages/nx/src/project-graph/project-graph-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ProjectGraphBuilder } from './project-graph-builder';

describe('ProjectGraphBuilder', () => {
let builder: ProjectGraphBuilder;

beforeEach(() => {
builder = new ProjectGraphBuilder();
builder.addNode({
Expand Down Expand Up @@ -209,4 +210,113 @@ describe('ProjectGraphBuilder', () => {
target2: [],
});
});

it('should prune dependencies when removing explicit dependencies from the files', () => {
builder.addImplicitDependency('source', 'target');
builder.addExternalNode({
name: 'npm:external',
type: 'npm',
data: {
version: '1.0.0',
packageName: 'external',
},
});
builder.addExternalNode({
name: 'npm:external2',
type: 'npm',
data: {
version: '1.0.0',
packageName: 'external2',
},
});
builder.addStaticDependency('npm:external', 'npm:external2');
builder.addStaticDependency('source', 'npm:external', 'source/index.ts');
builder.addDynamicDependency('source', 'npm:external2', 'source/second.ts');
const graph = builder.getUpdatedProjectGraph();

expect(graph.dependencies).toMatchInlineSnapshot(`
{
"npm:external": [
{
"source": "npm:external",
"target": "npm:external2",
"type": "static",
},
],
"source": [
{
"source": "source",
"target": "target",
"type": "implicit",
},
{
"source": "source",
"target": "npm:external",
"type": "static",
},
{
"source": "source",
"target": "npm:external2",
"type": "dynamic",
},
],
"target": [],
}
`);
expect(graph.nodes['source'].data.files).toMatchInlineSnapshot(`
[
{
"dependencies": [
{
"source": "source",
"target": "npm:external",
"type": "static",
},
],
"file": "source/index.ts",
},
{
"dependencies": [
{
"source": "source",
"target": "npm:external2",
"type": "dynamic",
},
],
"file": "source/second.ts",
},
]
`);

const newBuilder = new ProjectGraphBuilder(graph);
// remove static dependency from the file
delete newBuilder.graph.nodes['source'].data.files[0].dependencies;

const updatedGraph = newBuilder.getUpdatedProjectGraph();

expect(updatedGraph.dependencies).toMatchInlineSnapshot(`
{
"npm:external": [
{
"source": "npm:external",
"target": "npm:external2",
"type": "static",
},
],
"source": [
{
"source": "source",
"target": "target",
"type": "implicit",
},
{
"source": "source",
"target": "npm:external2",
"type": "dynamic",
},
],
"target": [],
}
`);
});
});
28 changes: 16 additions & 12 deletions packages/nx/src/project-graph/project-graph-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export class ProjectGraphBuilder {
targetProjectName: string,
sourceProjectFile?: string
): void {
// internal nodes must provide sourceProjectFile when creating static dependency
// externalNodes do not have sourceProjectFile
if (this.graph.nodes[sourceProjectName] && !sourceProjectFile) {
throw new Error(`Source project file is required`);
}
Expand All @@ -110,6 +112,7 @@ export class ProjectGraphBuilder {
if (this.graph.externalNodes[sourceProjectName]) {
throw new Error(`External projects can't have "dynamic" dependencies`);
}
// dynamic dependency is always bound to a file
if (!sourceProjectFile) {
throw new Error(`Source project file is required`);
}
Expand Down Expand Up @@ -281,10 +284,6 @@ export class ProjectGraphBuilder {
const isDuplicate = !!this.graph.dependencies[sourceProjectName].find(
(d) => d.target === targetProjectName && d.type === type
);
// do not add duplicate to project
if (isDuplicate && !sourceProjectFile) {
return;
}

const dependency = {
source: sourceProjectName,
Expand All @@ -311,16 +310,18 @@ export class ProjectGraphBuilder {
if (!fileData.dependencies) {
fileData.dependencies = [];
}
if (!fileData.dependencies.find((t) => t.target === targetProjectName)) {
if (
!fileData.dependencies.find(
(t) => t.target === targetProjectName && t.type === type
)
) {
fileData.dependencies.push(dependency);
}
} else if (!isDuplicate) {
// only add to dependencies section if the source file is not specified
// and not already added
this.graph.dependencies[sourceProjectName].push(dependency);
}

if (isDuplicate) {
return;
}

this.graph.dependencies[sourceProjectName].push(dependency);
}

private removeDependenciesWithNode(name: string) {
Expand Down Expand Up @@ -376,7 +377,10 @@ export class ProjectGraphBuilder {
if (this.graph.dependencies[sourceProject]) {
const removed = this.removedEdges[sourceProject];
for (const d of this.graph.dependencies[sourceProject]) {
if (!removed || !removed.has(d.target)) {
// static and dynamic dependencies of internal projects
// will be rebuilt based on the file dependencies
// we only need to keep the implicit dependencies
if (d.type === DependencyType.implicit && !removed?.has(d.target)) {
if (!alreadySetTargetProjects.has(d.target)) {
alreadySetTargetProjects.set(d.target, new Map([[d.type, d]]));
} else {
Expand Down

1 comment on commit f138a34

@vercel
Copy link

@vercel vercel bot commented on f138a34 Apr 28, 2023

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
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.