Skip to content

Commit

Permalink
feat(MavenSupport): Improve the caching logic for empty remote artifacts
Browse files Browse the repository at this point in the history
Previously, failures to look up remote artifacts were unconditionally
cached as empty artifacts. While it is hard to reliably determine the
root cause of a failed lookup, in the case that all candidate URLs do
not end with a known extension it is very likely that ORT ran into a bug
in the context of #4694. Ease debugging such cases by not anymore
caching failed lookups due to missing file extensions, and generally
improve the logging when returning an empty remote artifact.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Feb 27, 2023
1 parent 632cdaf commit bd9f9b2
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions analyzer/src/main/kotlin/managers/utils/MavenSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import java.net.URI

import kotlin.time.Duration.Companion.hours

import org.apache.logging.log4j.Level
import org.apache.logging.log4j.kotlin.Logging
import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager
import org.apache.maven.bridge.MavenRepositorySystem
Expand Down Expand Up @@ -111,6 +110,14 @@ private val File?.safePath: String

class MavenSupport(private val workspaceReader: WorkspaceReader) {
companion object : Logging {
private val PACKAGING_TYPES = setOf(
// Core packaging types, see https://maven.apache.org/pom.html#packaging.
"pom", "jar", "maven-plugin", "ejb", "war", "ear", "rar",
// Custom packaging types, see "resources/META-INF/plexus/components.xml".
"aar", "apk", "bundle", "dll", "dylib", "eclipse-plugin", "gwt-app", "gwt-lib", "hk2-jar", "hpi",
"jenkins-module", "orbit", "so", "zip"
)

// See http://maven.apache.org/pom.html#SCM.
private val SCM_REGEX = Regex("scm:(?<type>[^:@]+):(?<url>.+)")
private val USER_HOST_REGEX = Regex("scm:(?<user>[^:@]+)@(?<host>[^:]+)[:/](?<path>.+)")
Expand Down Expand Up @@ -635,14 +642,20 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) {
}
}

val level = if (artifact.classifier == "sources") Level.DEBUG else Level.WARN
logger.log(level) {
"Could not find artifact $artifact in any of ${locationInfo.map { it.downloadUrl }.distinct()}."
}
return RemoteArtifact.EMPTY.also { remoteArtifact ->
val downloadUrls = locationInfo.map { it.downloadUrl }.distinct()

return RemoteArtifact.EMPTY.also {
logger.debug { "Writing empty remote artifact for '$artifact' to disk cache." }
remoteArtifactCache.write(cacheKey, yamlMapper.writeValueAsString(it))
// While for dependencies that have e.g. no sources artifact published it is completely valid to have an
// empty remote artifact and to cache that result, for cases where none of the tried URLs ends with an
// extension for a known packaging type probably some bug is the root cause for the artifact not being
// found, and thus such results should not be cached, but retried.
if (downloadUrls.any { url -> PACKAGING_TYPES.any { url.endsWith(".$it") } }) {
logger.debug { "Writing empty remote artifact for '$artifact' to disk cache." }

remoteArtifactCache.write(cacheKey, yamlMapper.writeValueAsString(remoteArtifact))
} else {
logger.warn { "Could not find artifact $artifact in any of $downloadUrls." }
}
}
}

Expand Down

0 comments on commit bd9f9b2

Please sign in to comment.