From 294bd8c7fac17ed6da83503a7e613deb053e2062 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Mon, 8 Oct 2018 16:38:38 +0300 Subject: [PATCH] Fix resolution of Java Usage artifacts for configurations with no usage When Gradle resolves a dependency on a publication with no metadata (or with metadata disabled), it represents it as two variants, one with Usage `java-api`, the other with `java-runtime`. It turns out that the `java-api` one does not contain transitive dependencies with Maven scope `runtime`. This commit makes the Kotlin Gradle plugin disambiguation rules behave the same way as those defined in the JavaBasePlugin: when no Usage is requested, prefer the runtime usages as they are more complete. --- .../kotlin/gradle/VariantAwareDependenciesIT.kt | 17 +++++++++++++++++ .../kotlin/gradle/plugin/mpp/KotlinUsages.kt | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/VariantAwareDependenciesIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/VariantAwareDependenciesIT.kt index ebae6f325bb..8e35b424570 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/VariantAwareDependenciesIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/VariantAwareDependenciesIT.kt @@ -224,6 +224,23 @@ class VariantAwareDependenciesIT : BaseGradleIT() { testResolveAllConfigurations("sample-lib") } + @Test + fun testConfigurationsWithNoExplicitUsageResolveRuntime() = + // Starting with Gradle 5.0, plain Maven dependencies are represented as two variants, and resolving them to the API one leads + // to transitive dependencies left out of the resolution results. We need to ensure that our attributes schema does not lead to the API + // variants chosen over the runtime ones when resolving a configuration with no required Usage: + with(Project("simpleProject", GradleVersionRequired.AtLeast("5.0-milestone-1"))) { + setupWorkingDir() + gradleBuildScript().appendText("\ndependencies { compile 'org.jetbrains.kotlin:kotlin-compiler-embeddable' }") + + testResolveAllConfigurations { + assertContains(">> :compile --> kotlin-compiler-embeddable-${defaultBuildOptions().kotlinVersion}.jar") + + // Check that the transitive dependencies with 'runtime' scope are also available: + assertContains(">> :compile --> kotlin-script-runtime-${defaultBuildOptions().kotlinVersion}.jar") + } + } + private fun Project.embedProject(other: Project) { setupWorkingDir() other.setupWorkingDir() diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinUsages.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinUsages.kt index 8c7672a3ad1..bd64c2ce9d2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinUsages.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinUsages.kt @@ -71,10 +71,10 @@ object KotlinUsages { details.closestMatch(candidateValues.single { it?.name == KOTLIN_RUNTIME }!!) } if (JAVA_API in candidateNames && JAVA_RUNTIME_JARS in candidateNames && values.none { it in candidateNames }) { - details.closestMatch(candidateValues.single { it?.name == JAVA_API }!!) + details.closestMatch(candidateValues.single { it?.name == JAVA_RUNTIME_JARS }!!) } if (JAVA_API in candidateNames && JAVA_RUNTIME in candidateNames && values.none { it in candidateNames }) { - details.closestMatch(candidateValues.single { it?.name == JAVA_API }!!) + details.closestMatch(candidateValues.single { it?.name == JAVA_RUNTIME }!!) } if (JAVA_RUNTIME_CLASSES in candidateNames && JAVA_RUNTIME_RESOURCES in candidateNames && KOTLIN_RUNTIME in candidateNames) { closestMatch(candidateValues.single { it?.name == KOTLIN_RUNTIME }!!)