Skip to content

Commit

Permalink
KT-65684: KAPT: (Re)enable fallback to K1 KAPT and make it default
Browse files Browse the repository at this point in the history
[KAPT] KT-65684 Set languageVersion=1.9 in KaptToolIntegrationTestGenerated

[KAPT] KT-65684 Re-enable a few now-passing tests in Kapt4IT

[KAPT] KT-65684 Fix the logic setting -Xuse-kapt4 flag in Kapt4IT.forceKapt4()

The change is needed to make sure that all the tests have the flag set,
 otherwise some of them would silently switch to the fallback node.
Also disables a few now failing tests.

[KAPT] KT-65684 Revert "KT-64385 Enable K2 KAPT by default"

This reverts commit 7e9d6e6

Merge-request: KT-MR-14291
Merged-by: Pavel Mikhailovskii <Pavel.Mikhailovskii@jetbrains.com>
(cherry picked from commit 7ab9e03)


Merge-request: KT-MR-14298
Merged-by: Pavel Mikhailovskii <Pavel.Mikhailovskii@jetbrains.com>
  • Loading branch information
strangepleasures authored and Space Team committed Feb 9, 2024
1 parent 6bbd7b4 commit dc57957
Show file tree
Hide file tree
Showing 40 changed files with 460 additions and 128 deletions.
Expand Up @@ -77,6 +77,7 @@ fun copyK2JVMCompilerArguments(from: K2JVMCompilerArguments, to: K2JVMCompilerAr
to.typeEnhancementImprovementsInStrictMode = from.typeEnhancementImprovementsInStrictMode
to.useFastJarFileSystem = from.useFastJarFileSystem
to.useJavac = from.useJavac
to.useKapt4 = from.useKapt4
to.useOldBackend = from.useOldBackend
to.useOldClassFilesReading = from.useOldClassFilesReading
to.useOldInlineClassesManglingScheme = from.useOldInlineClassesManglingScheme
Expand Down
Expand Up @@ -836,6 +836,16 @@ This option is deprecated and will be deleted in future versions."""
field = value
}

@Argument(
value = "-Xuse-kapt4",
description = "Enable the experimental KAPT 4."
)
var useKapt4 = false
set(value) {
checkFrozen()
field = value
}

override fun configureAnalysisFlags(collector: MessageCollector, languageVersion: LanguageVersion): MutableMap<AnalysisFlag<*>, Any> {
val result = super.configureAnalysisFlags(collector, languageVersion)
result[JvmAnalysisFlags.strictMetadataVersionSemantics] = strictMetadataVersionSemantics
Expand Down
30 changes: 30 additions & 0 deletions compiler/cli/src/org/jetbrains/kotlin/cli/common/arguments.kt
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.cli.common
import com.intellij.ide.highlighter.JavaFileType
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.ManualLanguageFeatureSetting
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
Expand Down Expand Up @@ -47,6 +48,7 @@ fun CompilerConfiguration.setupCommonArguments(
}
}

switchToFallbackModeIfNecessary(arguments, messageCollector)
setupLanguageVersionSettings(arguments)

val usesK2 = arguments.useK2 || languageVersionSettings.languageVersion.usesK2
Expand All @@ -55,6 +57,34 @@ fun CompilerConfiguration.setupCommonArguments(
buildHmppModuleStructure(arguments)?.let { put(CommonConfigurationKeys.HMPP_MODULE_STRUCTURE, it) }
}

private fun switchToFallbackModeIfNecessary(arguments: CommonCompilerArguments, messageCollector: MessageCollector) {
fun warn(message: String) {
if (!arguments.suppressVersionWarnings) messageCollector.report(CompilerMessageSeverity.STRONG_WARNING, message)
}

if (arguments !is K2JVMCompilerArguments) return
val isK2 =
arguments.useK2 || (arguments.languageVersion?.startsWith('2') ?: (LanguageVersion.LATEST_STABLE >= LanguageVersion.KOTLIN_2_0))
val isKaptUsed = arguments.pluginOptions?.any { it.startsWith("plugin:org.jetbrains.kotlin.kapt3") } == true
when {
isK2 && isKaptUsed && !arguments.useKapt4 -> {
warn("Kapt currently doesn't support language version 2.0+. Falling back to 1.9.")
arguments.languageVersion = LanguageVersion.KOTLIN_1_9.versionString
if (arguments.apiVersion?.startsWith("2") == true) {
arguments.apiVersion = ApiVersion.KOTLIN_1_9.versionString
}
arguments.useK2 = false
arguments.skipMetadataVersionCheck = true
arguments.skipPrereleaseCheck = true
arguments.allowUnstableDependencies = true
}
arguments.useKapt4 -> warn(
if (isK2) "K2 kapt is an experimental feature. Use with caution."
else "-Xuse-kapt4 flag can be only used with language version 2.0+."
)
}
}

fun CompilerConfiguration.setupLanguageVersionSettings(arguments: CommonCompilerArguments) {
languageVersionSettings = arguments.toLanguageVersionSettings(getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY))
}
Expand Down
1 change: 1 addition & 0 deletions compiler/testData/cli/jvm/extraHelp.out
Expand Up @@ -143,6 +143,7 @@ where advanced options include:
See KT-45671 for more details.
-Xuse-fast-jar-file-system Use the fast implementation of Jar FS. This may speed up compilation time, but it is experimental.
-Xuse-javac Use javac for Java source and class file analysis.
-Xuse-kapt4 Enable the experimental KAPT 4.
-Xuse-old-backend Use the old JVM backend.
-Xuse-old-class-files-reading Use the old implementation for reading class files. This may slow down the compilation and cause problems with Groovy interop.
This can be used in the event of problems with the new implementation.
Expand Down
Expand Up @@ -12,17 +12,17 @@ import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.DisplayName

@DisplayName("Kapt3 caching inside Gradle daemon")
@DisplayName("Kapt caching inside Gradle daemon")
@DaemonsGradlePluginTests
open class Kapt3AndGradleDaemon : KGPDaemonsBaseTest() {
class Kapt3AndGradleDaemon : KGPDaemonsBaseTest() {

override val defaultBuildOptions: BuildOptions = super.defaultBuildOptions
.copy(
kaptOptions = BuildOptions.KaptOptions(
verbose = true,
includeCompileClasspath = false
)
).copyEnsuringK1()
)

@DisplayName("Javac should be loaded only once")
@GradleTest
Expand Down Expand Up @@ -51,7 +51,7 @@ open class Kapt3AndGradleDaemon : KGPDaemonsBaseTest() {
// which is not compatible with classloaders caching.
@GradleTestVersions(maxVersion = TestVersions.Gradle.G_7_6)
@GradleTest
open fun testAnnotationProcessorClassIsLoadedOnce(gradleVersion: GradleVersion) {
fun testAnnotationProcessorClassIsLoadedOnce(gradleVersion: GradleVersion) {
project(
"javacIsLoadedOnce".withPrefix,
gradleVersion,
Expand Down
Expand Up @@ -147,10 +147,18 @@ open class Kapt3ClassLoadersCacheIT : Kapt3IT() {
override fun useGeneratedKotlinSource(gradleVersion: GradleVersion) {
}

@Disabled("classloaders cache is leaking file descriptors that prevents cleaning test project")
override fun useGeneratedKotlinSourceK2(gradleVersion: GradleVersion) {
}

@Disabled("classloaders cache is leaking file descriptors that prevents cleaning test project")
override fun testMultipleProcessingPasses(gradleVersion: GradleVersion) {
}

@Disabled("classloaders cache is leaking file descriptors that prevents cleaning test project")
override fun useK2KaptProperty(gradleVersion: GradleVersion) {
}

override fun testAnnotationProcessorAsFqName(gradleVersion: GradleVersion) {
project("annotationProcessorAsFqName".withPrefix, gradleVersion) {
//classloaders caching is not compatible with includeCompileClasspath
Expand Down Expand Up @@ -384,13 +392,13 @@ open class Kapt3IT : Kapt3BaseIT() {

@DisplayName("Kapt is working with incremental compilation")
@GradleTest
open fun testSimpleWithIC(gradleVersion: GradleVersion) {
fun testSimpleWithIC(gradleVersion: GradleVersion) {
doTestSimpleWithIC(gradleVersion)
}

@DisplayName("Kapt is working with incremental compilation, when kotlin.incremental.useClasspathSnapshot=true")
@GradleTest
open fun testSimpleWithIC_withClasspathSnapshot(gradleVersion: GradleVersion) {
fun testSimpleWithIC_withClasspathSnapshot(gradleVersion: GradleVersion) {
doTestSimpleWithIC(gradleVersion, useClasspathSnapshot = true)
}

Expand Down Expand Up @@ -997,7 +1005,7 @@ open class Kapt3IT : Kapt3BaseIT() {

@DisplayName("Kapt with MPP/Jvm")
@GradleTest
open fun testMPPKaptPresence(gradleVersion: GradleVersion) {
fun testMPPKaptPresence(gradleVersion: GradleVersion) {
project(
"mpp-kapt-presence".withPrefix,
gradleVersion,
Expand Down Expand Up @@ -1268,6 +1276,99 @@ open class Kapt3IT : Kapt3BaseIT() {
}
}

@DisplayName("Kapt runs in fallback mode with useK2 = true")
@GradleTest
open fun fallBackModeWithUseK2(gradleVersion: GradleVersion) {
project("simple".withPrefix, gradleVersion) {
buildGradle.appendText(
"""
|tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
| compilerOptions {
| freeCompilerArgs.addAll([
| "-Xuse-fir-ic",
| "-Xuse-fir-lt"
| ])
| }
| kotlinOptions {
| useK2 = true
| }
|}
|
|compileKotlin.kotlinOptions.allWarningsAsErrors = false
""".trimMargin()
)
build("build") {
assertKaptSuccessful()
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin")
assertOutputContains("Falling back to 1.9.")
}
}
}

@DisplayName("Kapt runs in fallback mode with languageVersion = 2.0")
@GradleTest
open fun fallBackModeWithLanguageVersion2_0(gradleVersion: GradleVersion) {
project("simple".withPrefix, gradleVersion) {
buildGradle.appendText(
"""
|tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
| compilerOptions {
| freeCompilerArgs.addAll([
| "-Xuse-fir-ic",
| "-Xuse-fir-lt"
| ])
| }
| kotlinOptions {
| languageVersion = "2.0"
| }
|}
|
|compileKotlin.kotlinOptions.allWarningsAsErrors = false
""".trimMargin()
)
build("build") {
assertKaptSuccessful()
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin")
assertOutputContains("Falling back to 1.9.")
}
}
}

@DisplayName("K2 Kapt can be enabled via Gradle property kapt.use.k2")
@GradleTest
open fun useK2KaptProperty(gradleVersion: GradleVersion) {
project("simple".withPrefix, gradleVersion) {
buildGradle.appendText(
"""
|tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
| compilerOptions {
| freeCompilerArgs.addAll([
| "-Xuse-fir-ic",
| "-Xuse-fir-lt"
| ])
| }
| kotlinOptions {
| languageVersion = "2.0"
| }
|}
|
|compileKotlin.kotlinOptions.allWarningsAsErrors = false
""".trimMargin()
)
build("-Pkapt.use.k2=true", "build") {
assertKaptSuccessful()
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin")
assertOutputDoesNotContain("Falling back to 1.9.")
assertOutputContains("K2 kapt is an experimental feature. Use with caution.")
}
build("-Pkapt.use.k2=true", "cleanCompileKotlin", "compileKotlin") {
assertTasksExecuted(":compileKotlin")
// The warning should not be displayed for the compile task.
assertOutputDoesNotContain("K2 kapt is an experimental feature. Use with caution.")
}
}
}

@DisplayName("Kapt-generated Kotlin sources can be used in Kotlin")
@GradleTest
open fun useGeneratedKotlinSource(gradleVersion: GradleVersion) {
Expand All @@ -1279,6 +1380,35 @@ open class Kapt3IT : Kapt3BaseIT() {
}
}

@DisplayName("Kapt-generated Kotlin sources can be used in Kotlin with languageVersion = 2.0")
@GradleTest
open fun useGeneratedKotlinSourceK2(gradleVersion: GradleVersion) {
project("useGeneratedKotlinSource".withPrefix, gradleVersion) {
buildGradle.appendText(
"""
|tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
| compilerOptions {
| freeCompilerArgs.addAll([
| "-Xuse-fir-ic",
| "-Xuse-fir-lt"
| ])
| }
| kotlinOptions {
| languageVersion = "2.0"
| }
|}
|
|compileKotlin.kotlinOptions.allWarningsAsErrors = false
""".trimMargin()
)
build("build") {
assertKaptSuccessful()
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin")
assertOutputContains("Falling back to 1.9.")
}
}
}

@DisplayName("KT-58745: compiler plugin options should be passed to KaptGenerateStubs task")
@GradleTest
fun kaptGenerateStubsConfiguredWithCompilerPluginOptions(gradleVersion: GradleVersion) {
Expand Down

This file was deleted.

0 comments on commit dc57957

Please sign in to comment.