Skip to content

Commit

Permalink
chores(analyzer): Do not throw exception if duplicate packages
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bennati committed Oct 10, 2024
1 parent 546550e commit 344d6cb
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions analyzer/src/main/kotlin/AnalyzerResultBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
}

Check warning

Code scanning / detekt

Reports code blocks that are not followed by an empty line Warning

Missing empty line after block.
// 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}"

Check notice on line 54 in analyzer/src/main/kotlin/AnalyzerResultBuilder.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Redundant curly braces in string template

Redundant curly braces in string template

Check warning

Code scanning / detekt

Detects simplifications in template strings Warning

Redundant curly braces

Check notice

Code scanning / QDJVMC

Redundant curly braces in string template Note

Redundant curly braces in string template
)

val existingIssues = issues.getOrDefault(key, emptyList())
issues[key] = existingIssues + issue
}

Check warning

Code scanning / detekt

Reports code blocks that are not followed by an empty line Warning

Missing empty line after block.
// Remove duplicates from packages, to avoid side effects by having duplicate entries in the dependency tree
packages.removeAll { it.id in duplicates.keys.toSet() }

}

Check warning

Code scanning / detekt

Detects blank lines before rbraces Warning

Unexpected blank line(s) before "}"

return AnalyzerResult(projects, packages, issues, dependencyGraphs)
Expand Down

0 comments on commit 344d6cb

Please sign in to comment.