From f2063d7ea39778eb7469a8b3c28ffeee959f3872 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Wed, 23 Oct 2024 23:36:37 +0200 Subject: [PATCH] Refactor OCI variants WIP Improve resolution failure handling --- .../internal/dsl/OciImageDependenciesImpl.kt | 13 ++++++++--- .../oci/internal/gradle/VariantArtifact.kt | 8 ++++++- .../resolution/OciImageSpecResolution.kt | 22 +++++++++---------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt index 72f43fa2..2ed6bffb 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt @@ -55,10 +55,15 @@ internal abstract class OciImageDependenciesImpl @Inject constructor( private val allDependencies = lazy { indexConfiguration.allDependencies.filterIsInstance() } final override fun resolve(platformSelectorProvider: Provider): Provider> { - return indexConfiguration.incoming.resolutionResult.rootComponent.flatMap { rootComponent -> - val graph = resolveOciVariantGraph(rootComponent) // val lazy = lazy { // val graph = resolveOciVariantGraph(indexConfiguration.incoming.resolutionResult.root) + return indexConfiguration.incoming.resolutionResult.rootComponent.flatMap { rootComponent -> + val graph = try { + resolveOciVariantGraph(rootComponent) + } catch (e: ResolutionException) { + indexConfiguration.incoming.artifacts.failures // throws the failures + throw e + } val platformSelector = platformSelectorProvider.orNull val graphRootAndPlatformsList = selectPlatforms(graph, platformSelector) val platformToGraphRoots = HashMap>() @@ -98,7 +103,9 @@ internal abstract class OciImageDependenciesImpl @Inject constructor( for (imageSpec in imageSpecs) { val imageInput = OciImagesTask.ImageInput( platform, - imageSpec.variants.mapNotNull { variant -> variantDescriptorToInput[variant.toId()] }, + imageSpec.variants.map { variant -> + variantDescriptorToInput[variant.toId()] ?: throw IllegalStateException() // TODO message + }, imageSpec.selectors.collectReferenceSpecs(), ) variantSelectorsToImageInput[Pair(platform, imageSpec.selectors)] = imageInput diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/gradle/VariantArtifact.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/gradle/VariantArtifact.kt index 0412be55..f864d036 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/gradle/VariantArtifact.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/gradle/VariantArtifact.kt @@ -15,6 +15,7 @@ internal data class VariantArtifact(val variantId: VariantId, val file: File) internal val ArtifactCollection.variantArtifacts: Set get() { val variantArtifacts = LinkedHashSet() + var hasFailure = false // we need to use internal APIs to workaround the issue https://github.com/gradle/gradle/issues/29977 // ArtifactCollection.getResolvedArtifacts() wrongly deduplicates ResolvedArtifactResults of different variants for the same file (this as ArtifactCollectionInternal).visitArtifacts(object : ArtifactVisitor { @@ -48,9 +49,14 @@ internal val ArtifactCollection.variantArtifacts: Set ) } - override fun visitFailure(failure: Throwable) = Unit + override fun visitFailure(failure: Throwable) { + hasFailure = true + } override fun requireArtifactFiles() = true }) + if (hasFailure && !isLenient) { + failures // throws the failures + } return variantArtifacts } diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt index 4d86f052..3cd0c238 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt @@ -22,9 +22,8 @@ internal fun resolveOciVariantGraph(rootComponent: ResolvedComponentResult): Lis // rootNodesToDependencySelectors is linked to preserve the dependency order val rootNodesToSelectors = LinkedHashMap>() for (dependency in rootComponent.getDependenciesForVariant(rootVariant)) { - if ((dependency !is ResolvedDependencyResult) || dependency.isConstraint) { - continue // TODO fail - } + if (dependency.isConstraint) continue + if (dependency !is ResolvedDependencyResult) throw ResolutionException() val node = resolveOciVariantNode(dependency.selected, dependency.resolvedVariant, variantToNode) rootNodesToSelectors.getOrPut(node) { HashSet() } += dependency.requested.toVariantSelector() } @@ -49,9 +48,8 @@ private fun resolveOciVariantNode( platformToDependenciesAndSupportedPlatforms[platform] = Pair(ArrayList(), supportedPlatforms) } for (dependency in component.getDependenciesForVariant(variant)) { - if ((dependency !is ResolvedDependencyResult) || dependency.isConstraint) { - continue // TODO fail - } + if (dependency.isConstraint) continue + if (dependency !is ResolvedDependencyResult) throw ResolutionException() val dependencyPlatforms = dependency.requested.attributes.getAttribute(OCI_IMAGE_INDEX_PLATFORM_ATTRIBUTE)?.decodePlatforms() ?: platforms @@ -112,9 +110,8 @@ internal fun collectOciImageSpecs(rootComponent: ResolvedComponentResult): List< val firstLevelComponentAndVariantToSelectors = LinkedHashMap, HashSet>() for (dependency in rootComponent.getDependenciesForVariant(rootVariant)) { - if ((dependency !is ResolvedDependencyResult) || dependency.isConstraint) { - continue // TODO fail - } + if (dependency.isConstraint) continue + if (dependency !is ResolvedDependencyResult) throw ResolutionException() val componentAndVariant = Pair(dependency.selected, dependency.resolvedVariant) firstLevelComponentAndVariantToSelectors.getOrPut(componentAndVariant) { HashSet() } += dependency.requested.toVariantSelector() } @@ -133,9 +130,8 @@ private fun collectOciVariants( ) { if (variant !in variants) { for (dependency in component.getDependenciesForVariant(variant)) { - if ((dependency !is ResolvedDependencyResult) || dependency.isConstraint) { - continue // TODO fail - } + if (dependency.isConstraint) continue + if (dependency !is ResolvedDependencyResult) throw ResolutionException() collectOciVariants(dependency.selected, dependency.resolvedVariant, variants) } variants += variant @@ -149,3 +145,5 @@ internal fun Set.collectReferenceSpecs() = flatMapTo(LinkedHash private fun Set.normalize(): Set = if ((size == 1) && contains(DEFAULT_OCI_IMAGE_REFERENCE_SPEC)) emptySet() else this + +internal class ResolutionException : Exception()