From 3bb2ef463bc1d8a40d1b1b66f32a0d7f07e1a6c4 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 13 Dec 2024 19:28:32 +0100 Subject: [PATCH] fix(aosd): Lookup node linkage breadth-first Change finding nodes from depth- to breadth-first for a more compact implementation. This also fixes a bug of the original implementation which "swallowed" the return value of `traverse()` calls which resulted in the `linking` of transitive dependencies to be wrong. Signed-off-by: Sebastian Schuberth --- .../aosd/src/main/kotlin/Aosd21Reporter.kt | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/plugins/reporters/aosd/src/main/kotlin/Aosd21Reporter.kt b/plugins/reporters/aosd/src/main/kotlin/Aosd21Reporter.kt index fa5c59fccbc22..d9ec2cfc3985e 100644 --- a/plugins/reporters/aosd/src/main/kotlin/Aosd21Reporter.kt +++ b/plugins/reporters/aosd/src/main/kotlin/Aosd21Reporter.kt @@ -80,7 +80,7 @@ private fun Map>.toComponents( ): Set = values.mapTo(mutableSetOf()) { (index, pkg) -> val dependencies = input.ortResult.getDependencies(pkg.metadata.id, maxLevel = 1, omitExcluded = true) - val node = input.ortResult.dependencyNavigator.findNode(project, pkg.metadata.id) + val node = input.ortResult.dependencyNavigator.findBreadthFirst(project, pkg.metadata.id) val nonExcludedLicenseInfo = input.licenseInfoResolver.resolveLicenseInfo(pkg.metadata.id).filterExcluded() val relevantLicenseInfo = nonExcludedLicenseInfo.filter(LicenseView.CONCLUDED_OR_DECLARED_AND_DETECTED) @@ -124,24 +124,21 @@ private fun Map>.toComponents( } } -private fun DependencyNavigator.findNode(project: Project, id: Identifier): DependencyNode? { - fun traverse(node: DependencyNode): DependencyNode? { - if (node.id == id) return node +private fun DependencyNavigator.findBreadthFirst(project: Project, nodeId: Identifier): DependencyNode? { + fun Sequence.findBreadthFirst(id: Identifier): DependencyNode? { + // This also turns the sequence into a list so it can be consumed twice, see below. + val directDependencies = mapTo(mutableListOf()) { it.getStableReference() } - node.visitDependencies { dependencies -> - dependencies.forEach(::traverse) - } - - return null - } + directDependencies.find { node -> node.id == id }?.also { return it } - scopeNames(project).forEach { scopeName -> - directDependencies(project, scopeName).forEach { node -> - traverse(node)?.also { return it } + return directDependencies.firstNotNullOfOrNull { node -> + node.visitDependencies { it.findBreadthFirst(id) } } } - return null + return scopeNames(project).asSequence().mapNotNull { scopeName -> + directDependencies(project, scopeName).findBreadthFirst(nodeId) + }.firstOrNull() } private fun ResolvedLicenseInfo.toSimplifiedCompoundExpression(): SpdxExpression =