From d722afb04980d0d8f6333024d9bcf69ccdfa60a4 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Wed, 24 Jul 2024 07:12:56 -0500 Subject: [PATCH] Fix jarJar edge case with available-at variants (#229) --- .../common/dependency/JarJarArtifacts.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/net/neoforged/gradle/common/dependency/JarJarArtifacts.java b/common/src/main/java/net/neoforged/gradle/common/dependency/JarJarArtifacts.java index 883c532a1..68d2d396c 100644 --- a/common/src/main/java/net/neoforged/gradle/common/dependency/JarJarArtifacts.java +++ b/common/src/main/java/net/neoforged/gradle/common/dependency/JarJarArtifacts.java @@ -32,6 +32,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -141,6 +142,8 @@ private static List getIncludedJars(DependencyFilter fil if (version != null && versionRange != null) { data.add(new ResolvedJarJarArtifact(result.getFile(), version, versionRange, jarIdentifier.group(), jarIdentifier.artifact())); + } else { + throw new IllegalStateException("Could not determine version or version range for " + jarIdentifier.group()+":"+jarIdentifier.artifact()); } } return data.stream() @@ -150,18 +153,22 @@ private static List getIncludedJars(DependencyFilter fil private static void collectFromComponent(ResolvedComponentResult rootComponent, Set knownIdentifiers, Map versions, Map versionRanges) { for (DependencyResult result : rootComponent.getDependencies()) { - if (!(result instanceof ResolvedDependencyResult)) { + if (!(result instanceof ResolvedDependencyResult resolvedResult)) { continue; } - ResolvedDependencyResult resolvedResult = (ResolvedDependencyResult) result; ComponentSelector requested = resolvedResult.getRequested(); - ResolvedVariantResult variant = resolvedResult.getResolvedVariant(); - + ResolvedVariantResult originalVariant = resolvedResult.getResolvedVariant(); + ResolvedVariantResult variant = originalVariant; + // We do this to account for any available-at usage in module metadata -- the actual artifact will only have + // the module ID of the final target of available-at, but the resolved dependency lets us get the whole + // hierarchy. + while (variant.getExternalVariant().isPresent()) { + variant = variant.getExternalVariant().get(); + } DependencyManagementObject.ArtifactIdentifier artifactIdentifier = capabilityOrModule(variant); if (artifactIdentifier == null) { continue; } - ContainedJarIdentifier jarIdentifier = new ContainedJarIdentifier(artifactIdentifier.getGroup(), artifactIdentifier.getName()); knownIdentifiers.add(jarIdentifier); @@ -183,6 +190,13 @@ private static void collectFromComponent(ResolvedComponentResult rootComponent, } String version = getVersionFrom(variant); + String originalVersion = getVersionFrom(originalVariant); + + if (!Objects.equals(version, originalVersion)) { + throw new IllegalStateException("Version mismatch for " + originalVariant.getOwner() + ": available-at directs to " + + version + " but original is " + originalVersion + " which jarJar cannot handle well; consider depending on the available-at target directly" + ); + } if (version != null) { versions.put(jarIdentifier, version);