From 044ad84e6705d760e5d978b352c14e0710e9d981 Mon Sep 17 00:00:00 2001 From: Stefano Bennati Date: Wed, 9 Oct 2024 09:46:23 +0200 Subject: [PATCH] chores(analyzer): Do not throw exception if duplicate packages When duplicate packages or projects are found in the dependency tree, print a warning instead of throwing an exception and inteerrupting the scan. Duplicate packages may arise when the same package is imported twice, in these cases the dependency tree will be completed as the package is imported at least once. Signed-off-by: Stefano Bennati --- .../src/main/kotlin/AnalyzerResultBuilder.kt | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt b/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt index c50ff0a5a3508..5f34a518487cf 100644 --- a/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt +++ b/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt @@ -19,6 +19,8 @@ package org.ossreviewtoolkit.analyzer +import org.apache.logging.log4j.kotlin.logger + import org.ossreviewtoolkit.model.AnalyzerResult import org.ossreviewtoolkit.model.DependencyGraph import org.ossreviewtoolkit.model.DependencyGraphNavigator @@ -41,9 +43,23 @@ class AnalyzerResultBuilder { fun build(excludes: Excludes = Excludes.EMPTY): AnalyzerResult { val duplicates = (projects.map { it.toPackage() } + packages).getDuplicates { it.id } - require(duplicates.isEmpty()) { - "Unable to create the AnalyzerResult as it contains packages and projects with the same ids: " + - duplicates.values + if (duplicates.isNotEmpty()) { + logger.warn { + "AnalyzerResult contains packages and projects with the same ids: ${duplicates.values}" + } + // Log duplicates as issues + for ((key, items) in duplicates) { + val issue = createAndLogIssue( + source = "analyzer", + message = "AnalyzerResult contains packages and projects with the same ids: ${items}" + ) + + val existingIssues = issues.getOrDefault(key, emptyList()) + issues[key] = existingIssues + issue + } + // Remove duplicates from packages, to avoid side effects by having duplicate entries in the dependency tree + packages.removeAll { it.id in duplicates.keys.toSet() } + } return AnalyzerResult(projects, packages, issues, dependencyGraphs)