From 2fd524f1c00012ef102e995b59121de19a738b58 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Wed, 9 Jan 2019 20:06:07 +0300 Subject: [PATCH] Fix failure when creating a compilation in afterEvaluate (KT-28896) To configure a compilation's sources, we run an action in `whenEvaluated { ... }`, expecting that the compilation's task already exists. This was not true with compilations created from a user build script or a 3rd-party plugin in `afterEvaluate { ... }`. Fix this by expecting that a task may possibly not exist at that point, and also using `whenEvaluated { ... }` instead of `afterEvaluate { ... }` in several places that are executed for each new compilation as a workaround for https://github.com/gradle/gradle/issues/1135. Issue #KT-28896 Fixed --- .../sample-lib-gradle-kotlin-dsl/build.gradle.kts | 10 ++++++++++ .../new-mpp-lib-and-app/sample-lib/build.gradle | 10 ++++++++++ .../kotlin/gradle/plugin/mpp/kotlinCompilations.kt | 13 +++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib-gradle-kotlin-dsl/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib-gradle-kotlin-dsl/build.gradle.kts index 3215c96d7b4..bc03e196ba7 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib-gradle-kotlin-dsl/build.gradle.kts +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib-gradle-kotlin-dsl/build.gradle.kts @@ -53,4 +53,14 @@ publishing { repositories { maven { setUrl("file://${projectDir.absolutePath.replace('\\', '/')}/repo") } } +} + +// Check that a compilation may be created after project evaluation, KT-28896: +afterEvaluate { + kotlin { + jvm("jvm6").compilations.create("benchmark") { + defaultSourceSet.dependsOn(sourceSets["jvm6Main"]) + tasks["assemble"].dependsOn(compileKotlinTask) + } + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib/build.gradle b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib/build.gradle index 456372d83a4..747dd2deb97 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/new-mpp-lib-and-app/sample-lib/build.gradle @@ -83,4 +83,14 @@ publishing { repositories { maven { url "file://${projectDir.absolutePath.replace('\\', '/')}/repo" } } +} + +// Check that a compilation may be created after project evaluation, KT-28896: +afterEvaluate { + kotlin { + jvm('jvm6').compilations.create('benchmark') { + defaultSourceSet.dependsOn(sourceSets.jvm6Main) + assemble.dependsOn compileKotlinTask + } + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinCompilations.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinCompilations.kt index 8e5dafdcc44..d4dbc47e8d3 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinCompilations.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/kotlinCompilations.kt @@ -81,13 +81,22 @@ abstract class AbstractKotlinCompilation( commonSourceSet += sourceSet.kotlin } } - // Note! Invocation of getByName results in preliminary task instantiation. After fix of this issue the following code should be uncommented: + + // Note! Invocation of withType-all results in preliminary task instantiation. + // After fix of this issue the following code should be uncommented: // if (useLazyTaskConfiguration) { // (target.project.tasks.named(compileKotlinTaskName) as TaskProvider>).configure { // it.configureAction() // } // } - (target.project.tasks.getByName(compileKotlinTaskName) as AbstractKotlinCompile<*>).configureAction() + + target.project.tasks + // To configure a task that may have not yet been created at this point, use 'withType-matching-all`: + .withType(AbstractKotlinCompile::class.java) + .matching { it.name == compileKotlinTaskName } + .all { compileKotlinTask -> + compileKotlinTask.configureAction() + } } override fun source(sourceSet: KotlinSourceSet) {