Skip to content

Commit

Permalink
Be more compatible with K2
Browse files Browse the repository at this point in the history
The compiler plugins are always loaded unconditioanlly regardless of K1
or K2 and causes FirKotlinToJvmBytecodeCompiler to throw. Because there
seems no way to load compiler plugins conditionally on language version,
we declare the KSP compiler plugin to be K2 compatible while doing
nothing if K2 is detected.

Tested locally with a new enough Kotlin compiler.
  • Loading branch information
ting-yuan committed Oct 23, 2023
1 parent a136cfe commit 36f7357
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension

Expand Down Expand Up @@ -64,6 +65,10 @@ class KotlinSymbolProcessingCommandLineProcessor : CommandLineProcessor {
@ExperimentalCompilerApi
class KotlinSymbolProcessingComponentRegistrar : ComponentRegistrar {
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
// KSP 1.x don't and will not support K2. Do not register if language version >= 2.
if (configuration.languageVersionSettings.languageVersion >= LanguageVersion.KOTLIN_2_0)
return

val contentRoots = configuration[CLIConfigurationKeys.CONTENT_ROOTS] ?: emptyList()
val options = configuration[KSP_OPTIONS]?.apply {
javaSourceRoots.addAll(contentRoots.filterIsInstance<JavaSourceRoot>().map { it.file })
Expand Down Expand Up @@ -91,4 +96,8 @@ class KotlinSymbolProcessingComponentRegistrar : ComponentRegistrar {
)
}
}

// FirKotlinToJvmBytecodeCompiler throws an error when it sees an incompatible plugin.
override val supportsK2: Boolean
get() = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.buildtools.api.SourcesChanges
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersion.Companion.LATEST_STABLE
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.internal.kapt.incremental.CLASS_STRUCTURE_ARTIFACT_TYPE
Expand Down Expand Up @@ -75,7 +74,6 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
const val KSP_GROUP_ID = "com.google.devtools.ksp"
const val KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kspPluginClasspath"
const val KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME_NON_EMBEDDABLE = "kspPluginClasspathNonEmbeddable"
val LANGUAGE_VERSION = KotlinVersion.fromVersion(LATEST_STABLE.toString())

@JvmStatic
fun getKspOutputDir(project: Project, sourceSetName: String, target: String) =
Expand Down Expand Up @@ -408,11 +406,18 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool

fun configureLanguageVersion(kspTask: KotlinCompilationTask<*>) {
kspTask.compilerOptions.useK2.value(false)
kotlinCompilation.compilerOptions.options.languageVersion.orNull?.let { version ->
if (version >= KotlinVersion.KOTLIN_2_0) {
kspTask.compilerOptions.languageVersion.value(LANGUAGE_VERSION)
val languageVersion = kotlinCompilation.compilerOptions.options.languageVersion
kspTask.compilerOptions.languageVersion.value(
project.provider {
languageVersion.orNull?.let { version ->
if (version >= KotlinVersion.KOTLIN_2_0) {
KotlinVersion.KOTLIN_1_9
} else {
version
}
}
}
}
)
}

val isIncremental = project.findProperty("ksp.incremental")?.toString()?.toBoolean() ?: true
Expand Down

0 comments on commit 36f7357

Please sign in to comment.