Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 020d2e1

Browse files
vsavkinFrozenPandaz
authored andcommittedAug 13, 2020
fix(core): with-deps should handle circular dependencies
1 parent 12ac1bc commit 020d2e1

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed
 

‎e2e/workspace/src/workspace.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -498,24 +498,24 @@ forEachCli((cliName) => {
498498
);
499499
expect(resWithDeps.tasks).toEqual([
500500
{
501-
id: `${mypublishablelib}:build`,
501+
id: `${myapp}:build`,
502502
overrides: {},
503503
target: {
504-
project: mypublishablelib,
504+
project: myapp,
505505
target: 'build',
506506
},
507-
command: `npm run ${cliCommand} -- build ${mypublishablelib}`,
508-
outputs: [`dist/libs/${mypublishablelib}`],
507+
command: `npm run ${cliCommand} -- build ${myapp}`,
508+
outputs: [`dist/apps/${myapp}`],
509509
},
510510
{
511-
id: `${myapp}:build`,
511+
id: `${mypublishablelib}:build`,
512512
overrides: {},
513513
target: {
514-
project: myapp,
514+
project: mypublishablelib,
515515
target: 'build',
516516
},
517-
command: `npm run ${cliCommand} -- build ${myapp}`,
518-
outputs: [`dist/apps/${myapp}`],
517+
command: `npm run ${cliCommand} -- build ${mypublishablelib}`,
518+
outputs: [`dist/libs/${mypublishablelib}`],
519519
},
520520
]);
521521
compareTwoArrays(resWithDeps.projects, [

‎packages/workspace/src/core/project-graph/operators.spec.ts

+59
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,65 @@ describe('withDeps', () => {
173173
},
174174
});
175175
});
176+
177+
it('should handle circular deps', () => {
178+
const graph: ProjectGraph = {
179+
nodes: {
180+
lib1: { name: 'lib1', type: 'lib', data: null },
181+
lib2: { name: 'lib2', type: 'lib', data: null },
182+
},
183+
dependencies: {
184+
lib1: [
185+
{
186+
type: DependencyType.static,
187+
source: 'lib1',
188+
target: 'lib2',
189+
},
190+
],
191+
lib2: [
192+
{
193+
type: DependencyType.static,
194+
source: 'lib2',
195+
target: 'lib1',
196+
},
197+
],
198+
},
199+
};
200+
201+
const affectedNodes = [{ name: 'lib1', type: 'lib', data: null }];
202+
203+
const result = withDeps(graph, affectedNodes);
204+
expect(result).toEqual({
205+
nodes: {
206+
lib1: {
207+
name: 'lib1',
208+
type: 'lib',
209+
data: null,
210+
},
211+
lib2: {
212+
name: 'lib2',
213+
type: 'lib',
214+
data: null,
215+
},
216+
},
217+
dependencies: {
218+
lib2: [
219+
{
220+
type: 'static',
221+
source: 'lib2',
222+
target: 'lib1',
223+
},
224+
],
225+
lib1: [
226+
{
227+
type: 'static',
228+
source: 'lib1',
229+
target: 'lib2',
230+
},
231+
],
232+
},
233+
});
234+
});
176235
});
177236

178237
describe('filterNodes', () => {

‎packages/workspace/src/core/project-graph/operators.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,35 @@ export function withDeps(
8080
subsetNodes: ProjectGraphNode[]
8181
): ProjectGraph {
8282
const builder = new ProjectGraphBuilder();
83-
Object.values(subsetNodes).forEach(recur);
83+
const visitedNodes = [];
84+
const visitedEdges = [];
85+
Object.values(subsetNodes).forEach(recurNodes);
86+
Object.values(subsetNodes).forEach(recurEdges);
8487
return builder.build();
8588

8689
// ---------------------------------------------------------------------------
8790

88-
function recur(node) {
91+
function recurNodes(node) {
92+
if (visitedNodes.indexOf(node.name) > -1) return;
93+
builder.addNode(node);
94+
visitedNodes.push(node.name);
95+
96+
original.dependencies[node.name].forEach((n) => {
97+
recurNodes(original.nodes[n.target]);
98+
});
99+
}
100+
101+
function recurEdges(node) {
102+
if (visitedEdges.indexOf(node.name) > -1) return;
103+
visitedEdges.push(node.name);
104+
89105
const ds = original.dependencies[node.name];
90-
// 1. Recursively add all source nodes
91106
ds.forEach((n) => {
92-
recur(original.nodes[n.target]);
107+
builder.addDependency(n.type, n.source, n.target);
93108
});
94-
// 2. Add current node
95-
builder.addNode(node);
96-
// 3. Add all source dependencies
109+
97110
ds.forEach((n) => {
98-
builder.addDependency(n.type, n.source, n.target);
111+
recurEdges(original.nodes[n.target]);
99112
});
100113
}
101114
}

0 commit comments

Comments
 (0)
Please sign in to comment.