Skip to content

Commit 6d56d2b

Browse files
committed
Don't make self refential referrer as indirect
1 parent 0de0af1 commit 6d56d2b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

componentDetection.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ describe("ComponentDetection.addPackagesToManifests", () => {
122122
expect(manifests[0].countDependencies()).toBe(1);
123123
});
124124

125+
// Component detection reports some packages as top level referrers of themselves
126+
// We need to mark as direct as causes Dependency Graph to mark the package as transitive without any Direct
125127
test("adds package as indirect dependency when top level referrer is itself", () => {
126128
const manifests: any[] = [];
127129

128130
const testPackage = {
129131
id: "test-package",
130132
packageUrl: "pkg:npm/test-package@1.0.0",
131133
isDevelopmentDependency: false,
132-
topLevelReferrers: [{ packageUrl: "pkg:npm/test-package@1.0.0" }], // Self-reference case
134+
topLevelReferrers: [{ packageUrl: "pkg:npm/test-package@1.0.0" }],
133135
locationsFoundAt: ["package.json"],
134136
containerDetailIds: [],
135137
containerLayerIds: [],
@@ -142,9 +144,8 @@ describe("ComponentDetection.addPackagesToManifests", () => {
142144
expect(manifests).toHaveLength(1);
143145
expect(manifests[0].name).toBe("package.json");
144146

145-
// Self-referencing packages are currently treated as indirect - this might be a bug to investigate
146-
expect(manifests[0].directDependencies()).toHaveLength(0);
147-
expect(manifests[0].indirectDependencies()).toHaveLength(1);
147+
expect(manifests[0].directDependencies()).toHaveLength(1);
148+
expect(manifests[0].indirectDependencies()).toHaveLength(0);
148149
expect(manifests[0].countDependencies()).toBe(1);
149150
});
150151

componentDetection.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ export default class ComponentDetection {
147147
const manifest = new Manifest(location, location);
148148
manifests.push(manifest);
149149
}
150-
if (pkg.topLevelReferrers.length == 0) {
150+
151+
// Filter out self-references from topLevelReferrers
152+
const nonSelfReferrers = pkg.topLevelReferrers.filter((referrer: any) => {
153+
if (!referrer.packageUrl) return false;
154+
const referrerUrl = ComponentDetection.makePackageUrl(referrer.packageUrl);
155+
return referrerUrl !== pkg.packageUrl;
156+
});
157+
158+
if (nonSelfReferrers.length == 0) {
151159
manifests.find((manifest: Manifest) => manifest.name == location)?.addDirectDependency(pkg, ComponentDetection.getDependencyScope(pkg));
152160
} else {
153161
manifests.find((manifest: Manifest) => manifest.name == location)?.addIndirectDependency(pkg, ComponentDetection.getDependencyScope(pkg));

0 commit comments

Comments
 (0)