diff --git a/model/src/main/kotlin/DependencyGraphNavigator.kt b/model/src/main/kotlin/DependencyGraphNavigator.kt index 3ba365afd2349..16bb3f143501c 100644 --- a/model/src/main/kotlin/DependencyGraphNavigator.kt +++ b/model/src/main/kotlin/DependencyGraphNavigator.kt @@ -236,15 +236,15 @@ private fun collectDependencies( ids: MutableSet, visited: MutableSet = mutableSetOf() ) { - if (maxDepth != 0) { - nodes.forEach { node -> - val cursor = node as DependencyRefCursor - if (cursor.current !in visited) { - visited += cursor.current - if (matcher(node)) ids += node.id - - node.visitDependencies { collectDependencies(it, maxDepth - 1, matcher, ids, visited) } - } + if (maxDepth == 0) return + + nodes.forEach { node -> + val cursor = node as DependencyRefCursor + if (cursor.current !in visited) { + visited += cursor.current + if (matcher(node)) ids += node.id + + node.visitDependencies { collectDependencies(it, maxDepth - 1, matcher, ids, visited) } } } } 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 =