Skip to content

Commit

Permalink
Refactor OCI variants WIP
Browse files Browse the repository at this point in the history
Improve resolution failure handling
  • Loading branch information
SgtSilvio committed Oct 24, 2024
1 parent d49aeb9 commit f2063d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ internal abstract class OciImageDependenciesImpl @Inject constructor(
private val allDependencies = lazy { indexConfiguration.allDependencies.filterIsInstance<ModuleDependency>() }

final override fun resolve(platformSelectorProvider: Provider<PlatformSelector>): Provider<List<OciImagesTask.ImageInput>> {
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<Platform, ArrayList<OciVariantGraphRoot>>()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal data class VariantArtifact(val variantId: VariantId, val file: File)
internal val ArtifactCollection.variantArtifacts: Set<VariantArtifact>
get() {
val variantArtifacts = LinkedHashSet<VariantArtifact>()
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 {
Expand Down Expand Up @@ -48,9 +49,14 @@ internal val ArtifactCollection.variantArtifacts: Set<VariantArtifact>
)
}

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ internal fun resolveOciVariantGraph(rootComponent: ResolvedComponentResult): Lis
// rootNodesToDependencySelectors is linked to preserve the dependency order
val rootNodesToSelectors = LinkedHashMap<OciVariantNode, HashSet<VariantSelector>>()
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()
}
Expand All @@ -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
Expand Down Expand Up @@ -112,9 +110,8 @@ internal fun collectOciImageSpecs(rootComponent: ResolvedComponentResult): List<
val firstLevelComponentAndVariantToSelectors =
LinkedHashMap<Pair<ResolvedComponentResult, ResolvedVariantResult>, HashSet<VariantSelector>>()
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()
}
Expand All @@ -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
Expand All @@ -149,3 +145,5 @@ internal fun Set<VariantSelector>.collectReferenceSpecs() = flatMapTo(LinkedHash

private fun Set<OciImageReferenceSpec>.normalize(): Set<OciImageReferenceSpec> =
if ((size == 1) && contains(DEFAULT_OCI_IMAGE_REFERENCE_SPEC)) emptySet() else this

internal class ResolutionException : Exception()

0 comments on commit f2063d7

Please sign in to comment.