Gradle: do not set fragments/fragmentRefines for non-MPP

Otherwise kapt-generated Kotlin sources do not end up in any module,
which causes an error in KT-58301. The error is only happening with K2
because fragments/fragmentRefines arguments are checked more strictly
than commonSources used in K1.

 #KT-58301 Fixed
This commit is contained in:
Alexander Udalov
2023-05-01 15:55:57 +02:00
committed by Space Team
parent b72b1ad7cd
commit 926aa07ca1
8 changed files with 109 additions and 1 deletions
@@ -1180,4 +1180,44 @@ open class Kapt3IT : Kapt3BaseIT() {
}
}
}
@DisplayName("Kapt-generated Kotlin sources can be used in Kotlin")
@GradleTest
internal fun useGeneratedKotlinSource(gradleVersion: GradleVersion) {
project("useGeneratedKotlinSource".withPrefix, gradleVersion) {
build("build") {
assertKaptSuccessful()
assertTasksExecuted(":kaptGenerateStubsKotlin", ":kaptKotlin", ":compileKotlin")
}
}
}
@DisplayName("Kapt-generated Kotlin sources can be used in Kotlin with languageVersion = 2.0")
@GradleTest
internal 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.")
}
}
}
}
@@ -0,0 +1,17 @@
plugins {
id "java"
id "org.jetbrains.kotlin.jvm"
id "org.jetbrains.kotlin.kapt"
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation(project(":processor"))
kapt(project(":processor"))
}
compileKotlin.kotlinOptions.allWarningsAsErrors = true
@@ -0,0 +1,11 @@
plugins {
id "java"
id "org.jetbrains.kotlin.jvm"
}
repositories {
mavenLocal()
mavenCentral()
}
compileKotlin.kotlinOptions.allWarningsAsErrors = true
@@ -0,0 +1,30 @@
package processor
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.tools.StandardLocation
class GenerateKotlinSourceFile : AbstractProcessor() {
override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {
val annotation = annotations.singleOrNull() ?: return true
for (type in roundEnv.getElementsAnnotatedWith(annotation)) {
if (type !is TypeElement) continue
val packageName = processingEnv.elementUtils.getPackageOf(type).qualifiedName.toString()
val simpleName = type.simpleName.toString() + "Generated"
val file = processingEnv.filer.createResource(StandardLocation.SOURCE_OUTPUT, "example", "$simpleName.kt", type)
file.openWriter().use { writer ->
writer.write("package $packageName\n\nclass $simpleName")
}
}
return true
}
override fun getSupportedSourceVersion(): SourceVersion =
SourceVersion.RELEASE_6
override fun getSupportedAnnotationTypes(): Set<String> =
setOf("example.Generate")
}
@@ -0,0 +1,8 @@
package example
annotation class Generate
@Generate
class TestClass
fun testClass(): TestClassGenerated = TestClassGenerated()
@@ -230,7 +230,7 @@ abstract class KotlinCompile @Inject constructor(
args.javaPackagePrefix = javaPackagePrefix
if (compilerOptions.usesK2.get()) {
if (compilerOptions.usesK2.get() && multiPlatformEnabled.get()) {
args.fragments = multiplatformStructure.fragmentsCompilerArgs
args.fragmentRefines = multiplatformStructure.fragmentRefinesCompilerArgs
}