Skip to content

Commit 9e875aa

Browse files
authored
Add cycle safety for transitive dependencies
1 parent 17ef676 commit 9e875aa

File tree

4 files changed

+26687
-5
lines changed

4 files changed

+26687
-5
lines changed

src/depgraph.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ describe('depgraph', () => {
116116
});
117117
});
118118

119+
describe('hadoop-tree', () => {
120+
let depGraph;
121+
beforeAll(() => {
122+
depGraph = parseDependencyJson(getTestDataFile("hadoop-tree"));
123+
});
124+
125+
it('should parse out the top level dependencies', () => {
126+
const mavenDependencies = new MavenDependencyGraph(depGraph);
127+
expect(mavenDependencies.getPackageCount()).to.equal(678);
128+
});
129+
130+
it('should be able to generate a manifest despite having a cycle', () => {
131+
const mavenDependencies = new MavenDependencyGraph(depGraph);
132+
const manifest = mavenDependencies.createManifest();
133+
134+
expect(manifest.name).to.equal('hadoop-main');
135+
expect(manifest.countDependencies()).to.equal(653);
136+
})
137+
});
119138

120139
describe('bs-parent-dep-tree', () => {
121140

src/depgraph.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,18 @@ export class MavenDependencyGraph {
8585
let scope = getDependencyScopeForMavenScope(artifact.scopes);
8686
manifest.addDirectDependency(depPackage, scope);
8787

88-
function addTransitiveDeps(dependencies) {
88+
function addTransitiveDeps(dependencies, seen: Set<string> = new Set()) {
8989
if (dependencies) {
9090
dependencies.forEach(transitiveDep => {
91+
if (seen.has(transitiveDep.packageURL.toString())) {
92+
// we're in a cycle! skip this one.
93+
return;
94+
}
95+
seen.add(transitiveDep.packageURL.toString());
9196
const transitiveDepArtifact = packageUrlToArtifact[transitiveDep.packageURL.toString()];
9297
const transitiveDepScope = getDependencyScopeForMavenScope(transitiveDepArtifact.scopes);
9398
manifest.addIndirectDependency(transitiveDep, transitiveDepScope);
94-
addTransitiveDeps(transitiveDep.dependencies);
99+
addTransitiveDeps(transitiveDep.dependencies, seen);
95100
});
96101
}
97102
}

src/snapshot-generator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ export async function generateSnapshot(directory: string, mvnConfig?: MavenConfi
5858
? snapshotConfig.correlator
5959
: snapshot.job?.correlator;
6060

61-
const specifiedRef = getNonEmtptyValue(snapshotConfig?.ref);
61+
const specifiedRef = getNonEmptyValue(snapshotConfig?.ref);
6262
if (specifiedRef) {
6363
snapshot.ref = specifiedRef;
6464
}
6565

66-
const specifiedSha = getNonEmtptyValue(snapshot?.sha);
66+
const specifiedSha = getNonEmptyValue(snapshot?.sha);
6767
if (specifiedSha) {
6868
snapshot.sha = specifiedSha;
6969
}
@@ -173,7 +173,7 @@ function getRepositoryRelativePath(file) {
173173
return result;
174174
}
175175

176-
function getNonEmtptyValue(str?: string) {
176+
function getNonEmptyValue(str?: string) {
177177
if (str) {
178178
const trimmed = str.trim();
179179
if (trimmed.length > 0) {

0 commit comments

Comments
 (0)