Fix KaptGenerateStubsTask freeCompilerArgs duplication

Now this task uses configured freeCompilerArgs both from itself and from
 linked KotlinCompile task. When these tasks are configured with the
 same value this leads to freeCompilerArgs duplication.

Added workaround is to use linked KotlinCompile freeCompilerArgs as
convention value for KaptGenerateStubsTask. Proper fix will be done via
KT-54468.

^KT-55452 Fixed
^KT-55565 Fixed
This commit is contained in:
Yahor Berdnikau
2023-02-22 22:05:13 +01:00
committed by Space Team
parent a49906f4e4
commit 473a2dc87c
4 changed files with 65 additions and 2 deletions
@@ -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")}"
}
}
}
}
}
@@ -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)
@@ -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<TASK : AbstractKotlinCompile
configureTaskProvider { taskProvider ->
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
)
@@ -43,6 +43,7 @@ internal class KaptGenerateStubsConfig : BaseKotlinCompileConfig<KaptGenerateStu
task.libraries.from({ kotlinCompileTask.libraries - project.files(kaptClassesDir) })
task.compileKotlinArgumentsContributor.set(providers.provider { kotlinCompileTask.compilerArgumentsContributor })
task.pluginOptions.addAll(kotlinCompileTask.pluginOptions)
task.compilerOptions.freeCompilerArgs.convention(kotlinCompileTask.compilerOptions.freeCompilerArgs)
// KotlinCompile will also have as input output from KaptGenerateStubTask and KaptTask
// We are filtering them to avoid failed UP-TO-DATE checks
val kaptJavaSourcesDir = Kapt3GradleSubplugin.getKaptGeneratedSourcesDir(