From 727b043cf00e797330ba5c214cd3361ec31aecf2 Mon Sep 17 00:00:00 2001 From: Tom Tresansky Date: Tue, 23 Jan 2024 08:14:04 -0500 Subject: [PATCH] Simplify resolver to remove some special casing no longer needed - Also adds tests to verify kotlin stdlib behavior. --- .../gradle/versions/updates/Resolver.kt | 11 ++-- ...ovy => KotlinDependencyUpdatesSpec.groovy} | 54 ++++++++++++++++--- 2 files changed, 52 insertions(+), 13 deletions(-) rename gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/{PluginUpdateDetectionSpec.groovy => KotlinDependencyUpdatesSpec.groovy} (57%) diff --git a/gradle-versions-plugin/src/main/kotlin/com/github/benmanes/gradle/versions/updates/Resolver.kt b/gradle-versions-plugin/src/main/kotlin/com/github/benmanes/gradle/versions/updates/Resolver.kt index 9e10e665..28fce619 100644 --- a/gradle-versions-plugin/src/main/kotlin/com/github/benmanes/gradle/versions/updates/Resolver.kt +++ b/gradle-versions-plugin/src/main/kotlin/com/github/benmanes/gradle/versions/updates/Resolver.kt @@ -89,12 +89,8 @@ class Resolver( revision: String, currentCoordinates: Map, ): Configuration { - // Kotlin deps anywhere in the hierarchy are a special case we'll handle later separately, unless they are being - // forced, as is the case with plugins, in which case handle those deps here - val kotlinDeps = { dependency: ExternalDependency -> (dependency.group?.startsWith("org.jetbrains.kotlin") ?: false) && dependency.version != null } val latest = configuration.allDependencies .filterIsInstance() - .filterNot { kotlinDeps(it) && it.isForce } .mapTo(mutableListOf()) { dependency -> createQueryDependency(dependency as ModuleDependency) } @@ -123,11 +119,12 @@ class Resolver( } // Resolve using the latest version of explicitly declared dependencies and retains Kotlin's - // inherited stdlib dependencies from the super configurations. This is required for variant - // resolution, but the full set can break consumer capability matching. + // inherited dependencies (importantly, including stdlib) from the super configurations. This + // is required for variant resolution, but the full set can break consumer capability matching. + val isKotlinDep = { dependency: ExternalDependency -> (dependency.group?.startsWith("org.jetbrains.kotlin") ?: false) } val inheritedKotlin = configuration.allDependencies .filterIsInstance() - .filter(kotlinDeps) + .filter { d -> isKotlinDep(d) } .minus(configuration.dependencies) // Adds the Kotlin 1.2.x legacy metadata to assist in variant selection diff --git a/gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/PluginUpdateDetectionSpec.groovy b/gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDependencyUpdatesSpec.groovy similarity index 57% rename from gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/PluginUpdateDetectionSpec.groovy rename to gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDependencyUpdatesSpec.groovy index 57975fec..95cbb51a 100644 --- a/gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/PluginUpdateDetectionSpec.groovy +++ b/gradle-versions-plugin/src/test/groovy/com/github/benmanes/gradle/versions/KotlinDependencyUpdatesSpec.groovy @@ -8,8 +8,10 @@ import spock.lang.Specification import static org.gradle.testkit.runner.TaskOutcome.SUCCESS -final class PluginUpdateDetectionSpec extends Specification { - private static final KOTLIN_VERSION = '1.6.0' +final class KotlinDependencyUpdatesSpec extends Specification { + private static final DECLARED_KOTLIN_VERSION = '1.7.0' + private static final DECLARED_KOTLIN_STD_VERSION = '1.8.0' + private static final CURRENT_KOTLIN_VERSION = '2.0.0-Beta3' @Rule final TemporaryFolder testProjectDir = new TemporaryFolder() @@ -45,7 +47,7 @@ final class PluginUpdateDetectionSpec extends Specification { } """.stripIndent() - testProjectDir.newFile('gradle.properties') << "kotlin_version = $KOTLIN_VERSION" + testProjectDir.newFile('gradle.properties') << "kotlin_version = $DECLARED_KOTLIN_VERSION" when: def result = GradleRunner.create() @@ -56,7 +58,7 @@ final class PluginUpdateDetectionSpec extends Specification { then: result.output.contains """The following dependencies have later milestone versions: - - org.jetbrains.kotlin:kotlin-gradle-plugin [$KOTLIN_VERSION -> """ + - org.jetbrains.kotlin:kotlin-gradle-plugin [$DECLARED_KOTLIN_VERSION -> $CURRENT_KOTLIN_VERSION]""" result.task(':dependencyUpdates').outcome == SUCCESS where: @@ -83,7 +85,7 @@ final class PluginUpdateDetectionSpec extends Specification { } """.stripIndent() - testProjectDir.newFile('gradle.properties') << "kotlin_version = $KOTLIN_VERSION" + testProjectDir.newFile('gradle.properties') << "kotlin_version = $DECLARED_KOTLIN_VERSION" when: def result = GradleRunner.create() @@ -94,7 +96,47 @@ final class PluginUpdateDetectionSpec extends Specification { then: result.output.contains """The following dependencies have later milestone versions: - - org.jetbrains.kotlin:kotlin-gradle-plugin [$KOTLIN_VERSION -> """ + - org.jetbrains.kotlin:kotlin-gradle-plugin [$DECLARED_KOTLIN_VERSION -> $CURRENT_KOTLIN_VERSION]""" result.task(':dependencyUpdates').outcome == SUCCESS } + + @See("https://github.com/ben-manes/gradle-versions-plugin/issues/423") + def "kotlin stdlib is properly handled (when added explicitly: #explicitStdLibVersion)"() { + given: + testProjectDir.newFile('build.gradle') << + """ + plugins { + id 'com.github.ben-manes.versions' version '0.46.0' + id 'org.jetbrains.kotlin.jvm' version '$DECLARED_KOTLIN_VERSION' + } + + repositories { + mavenCentral() + } + + dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib${explicitStdLibVersion ? ":$DECLARED_KOTLIN_STD_VERSION" : ""}" + } + """.stripIndent() + + when: + def result = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withArguments('dependencyUpdates', '--info') + .withPluginClasspath() + .build() + + then: + result.output.contains """The following dependencies have later milestone versions: + - org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable [$DECLARED_KOTLIN_VERSION -> $CURRENT_KOTLIN_VERSION] + https://kotlinlang.org/ + - org.jetbrains.kotlin:kotlin-stdlib [${explicitStdLibVersion ? DECLARED_KOTLIN_STD_VERSION : DECLARED_KOTLIN_VERSION} -> $CURRENT_KOTLIN_VERSION] + https://kotlinlang.org/ + - org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin [$DECLARED_KOTLIN_VERSION -> $CURRENT_KOTLIN_VERSION] + https://kotlinlang.org/""" + result.task(':dependencyUpdates').outcome == SUCCESS + + where: + explicitStdLibVersion << [true, false] + } }