Skip to content

Commit

Permalink
Fix platform constraint processing to avoid accidental exclude
Browse files Browse the repository at this point in the history
Previously, a constraint in a platform that applied to a transitive
dependency of the platform would prevent subsequent processing of
that dependency. This resulted in the dependency being
unintentionally excluded.

This commit updates the configuring of exclusions to only track a
dependency is seen when it's an actual dependency and not a
constraint.

Closes gh-368
  • Loading branch information
wilkinsona committed Oct 23, 2023
1 parent aa1f91d commit 786f0f2
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void handleResolvedDependency(ResolvedDependencyResult dependency, Node
Map<String, Exclusions> pomExclusionsById, LinkedList<Node> queue, Set<ResolvedComponentResult> seen) {
ResolvedComponentResult child = dependency.getSelected();
String childId = getId(child);
if (!node.excluded(childId) && seen.add(child) && !dependency.isConstraint()) {
if (!node.excluded(childId) && !dependency.isConstraint() && seen.add(child)) {
queue.add(new Node(child, childId, getChildExclusions(node, childId, pomExclusionsById)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ void dependenciesDeclaredInAPlatformAreNotAccidentallyExcluded() {
assertThat(readLines("resolved.txt")).containsExactly("spring-core-5.3.27.jar", "spring-jcl-5.3.27.jar");
}

@Test
void platformConstrainingATransitiveDependencyDoesNotAccidentallyExcludeThatDependency() {
this.gradleBuild.runner().withArguments("resolve").build();
assertThat(readLines("resolved.txt")).containsExactly("okhttp-4.11.0.jar", "okio-jvm-3.6.0.jar",
"kotlin-stdlib-jdk8-1.9.10.jar", "kotlin-stdlib-jdk7-1.9.10.jar", "kotlin-stdlib-1.9.10.jar",
"kotlin-stdlib-common-1.9.10.jar", "annotations-13.0.jar");
}

private void writeLines(Path path, String... lines) {
try {
Path resolvedPath = this.gradleBuild.runner().getProjectDir().toPath().resolve(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id "java"
id "io.spring.dependency-management"
}

repositories {
maven {
url file("maven-repo")
}
mavenCentral()
}

dependencies {
implementation(platform("test:platform-with-constrained-transitive-dependency:1.0"))
}

tasks.register("resolve") {
doFirst {
def files = project.configurations.compileClasspath.resolve()
def output = new File("${buildDir}/resolved.txt")
output.parentFile.mkdirs()
files.collect { it.name }.each { output << "${it}\n" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"formatVersion": "1.1",
"component": {
"group": "test",
"module": "platform-with-constrained-transitive-dependency",
"version": "1.0",
"attributes": {
"org.gradle.status": "release"
}
},
"createdBy": {
"gradle": {
"version": "8.4"
}
},
"variants": [
{
"name": "apiElements",
"attributes": {
"org.gradle.category": "platform",
"org.gradle.usage": "java-api"
},
"dependencies": [
{
"group": "com.squareup.okhttp3",
"module": "okhttp",
"version": {
"requires": "4.11.0"
}
}
],
"dependencyConstraints": [
{
"group": "com.squareup.okio",
"module": "okio",
"version": {
"requires": "3.6.0",
"prefers": "latest.release",
"rejects": [
"3.2.0"
]
}
}
]
},
{
"name": "runtimeElements",
"attributes": {
"org.gradle.category": "platform",
"org.gradle.usage": "java-runtime"
},
"dependencies": [
{
"group": "com.squareup.okhttp3",
"module": "okhttp",
"version": {
"requires": "4.11.0"
}
}
],
"dependencyConstraints": [
{
"group": "com.squareup.okio",
"module": "okio",
"version": {
"requires": "3.6.0",
"prefers": "latest.release",
"rejects": [
"3.2.0"
]
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>platform-with-constrained-transitive-dependency</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

0 comments on commit 786f0f2

Please sign in to comment.