diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt index 94614c8e438..9f0f19e0b01 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/Kapt3IT.kt @@ -556,7 +556,7 @@ open class Kapt3IT : Kapt3BaseIT() { assertKaptSuccessful() val regex = "(?m)^.*Kotlin compiler args.*-P plugin:org\\.jetbrains\\.kotlin\\.kapt3.*$".toRegex() val kaptArgs = regex.find(output)?.value ?: error("Kapt compiler arguments are not found!") - assert(kaptArgs.contains(arg)) { "Kapt compiler arguments should contain '$arg'" } + assert(kaptArgs.contains(arg)) { "Kapt compiler arguments should contain '$arg': $kaptArgs" } } } } @@ -1066,4 +1066,59 @@ open class Kapt3IT : Kapt3BaseIT() { } } } + + @DisplayName("KT-55452: KaptGenerateStubs task compiler options are not duplicated") + @GradleTest + fun testKaptGenerateStubsCompilerOptionsDup(gradleVersion: GradleVersion) { + project( + "simple".withPrefix, + gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG) + ) { + buildGradle.appendText( + """ + | + |tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { + | compilerOptions { + | jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 + | freeCompilerArgs.addAll([ + | "-P", + | "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true", + | "-P", + | "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + + | project.buildDir.absolutePath + "/compose_metrics" + | ]) + | } + |} + | + """.trimMargin() + ) + + build(":kaptGenerateStubsKotlin") { + val compilerArguments = output + .lineSequence() + .first { it.contains("Kotlin compiler args:") } + .substringAfter("Kotlin compiler args:") + .split(" ") + + val pOption = compilerArguments.filter { it == "-P" }.size + // 2 from freeArgs and 1 for kapt itself + assert(pOption <= 3) { + printBuildOutput() + "KaptGenerateStubs task compiler arguments contains $pOption times '-P' option: ${compilerArguments.joinToString("\n")}" + } + + val composeSuppressOption = compilerArguments + .filter { + it == "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true" + } + .size + assert(composeSuppressOption == 1) { + printBuildOutput() + "KaptGenerateStubs task compiler arguments contains $composeSuppressOption times option to suppress compose warning:" + + " ${compilerArguments.joinToString("\n")}" + } + } + } + } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt index 48e173be4bd..21eaa3690d2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt @@ -108,6 +108,10 @@ abstract class KaptGenerateStubsTask @Inject constructor( ) ) + // Workaround for freeCompiler args duplication when they were configured for both this task + // and linked KotlinCompile task with the same values. For now linked KotlinCompile task + // freeCompilerArgs is used as convention for this task freeCompilerArgs + args.freeArgs = emptyList() // Also use KotlinOptions configuration that was directly set to this task // as 'compileKotlinArgumentsContributor' has KotlinOptions from linked KotlinCompile task (compilerOptions as KotlinJvmCompilerOptionsDefault).fillCompilerArguments(args) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/AbstractKotlinCompileConfig.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/AbstractKotlinCompileConfig.kt index 0064354453e..21df1fbfc90 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/AbstractKotlinCompileConfig.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/AbstractKotlinCompileConfig.kt @@ -17,7 +17,7 @@ import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension import org.jetbrains.kotlin.gradle.dsl.topLevelExtension import org.jetbrains.kotlin.gradle.incremental.IncrementalModuleInfoBuildService -import org.jetbrains.kotlin.gradle.incremental.IncrementalModuleInfoProvider +import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithClosure @@ -46,6 +46,9 @@ internal abstract class AbstractKotlinCompileConfig project.runOnceAfterEvaluated("apply properties and language settings to ${taskProvider.name}") { taskProvider.configure { + // KaptGenerateStubs will receive value from linked KotlinCompile task + if (it is KaptGenerateStubsTask) return@configure + applyLanguageSettingsToCompilerOptions( languageSettings.get(), (it as org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask<*>).compilerOptions ) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/KaptGenerateStubsConfig.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/KaptGenerateStubsConfig.kt index f09f3619bea..10e5c493612 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/KaptGenerateStubsConfig.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/configuration/KaptGenerateStubsConfig.kt @@ -43,6 +43,7 @@ internal class KaptGenerateStubsConfig : BaseKotlinCompileConfig