[KAPT] KT-65006 fix issues when using Kapt+Serialization

This MR fixes two independent issues causing build failures when using Kapt in combination with the Kotlin serialization plugin (and, very likely, with other plugins as well):
Kapt applied compiler plugins twice, causing errors similar to the one described in KT-65006
Kapt failed to generate @Metadata annotations for plugin-generated companion objects because the corresponding SLC missed containingFile needed for generation of method and class signatures

Merge-request: KT-MR-13970
Merged-by: Pavel Mikhailovskii <Pavel.Mikhailovskii@jetbrains.com>
This commit is contained in:
strangepleasures
2024-01-23 20:35:32 +00:00
committed by Space Team
parent 8e4dc1fc31
commit 85c9c57da4
8 changed files with 66 additions and 55 deletions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.kapt4
import com.intellij.openapi.extensions.ExtensionPoint
import com.intellij.openapi.util.Disposer
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiJavaFile
@@ -78,23 +77,27 @@ private class Kapt4AnalysisHandlerExtension : FirAnalysisHandlerExtension() {
buildKtModuleProviderByCompilerConfiguration(updatedConfiguration)
registerProjectService(KtLifetimeTokenProvider::class.java, KtAlwaysAccessibleLifetimeTokenProvider())
registerProjectService(KtCompilerPluginsProvider::class.java, StandaloneCompilerPluginsProvider())
registerProjectService(KtCompilerPluginsProvider::class.java, object : KtCompilerPluginsProvider() {
private val extensionStorage = CompilerPluginRegistrar.ExtensionStorage().apply {
for (registrar in updatedConfiguration.getList(CompilerPluginRegistrar.COMPILER_PLUGIN_REGISTRARS)) {
with(registrar) { registerExtensions(updatedConfiguration) }
}
}
override fun <T : Any> getRegisteredExtensions(
module: KtSourceModule,
extensionType: ProjectExtensionDescriptor<T>,
): List<T> {
@Suppress("UNCHECKED_CAST")
return (extensionStorage.registeredExtensions[extensionType] as? List<T>) ?: emptyList()
}
override fun isPluginOfTypeRegistered(module: KtSourceModule, pluginType: CompilerPluginType): Boolean = false
})
}
val (module, files) = standaloneAnalysisAPISession.modulesWithFiles.entries.single()
val extensionStorage = CompilerPluginRegistrar.ExtensionStorage()
for (registrar in configuration.getList(CompilerPluginRegistrar.COMPILER_PLUGIN_REGISTRARS)) {
with(registrar) { extensionStorage.registerExtensions(configuration) }
}
for ((extensionPoint, extensions) in extensionStorage.registeredExtensions) {
for (extension in extensions) {
@Suppress("TestOnlyProblems")
module.project.extensionArea.getExtensionPointIfRegistered<Any>(extensionPoint.extensionPointName.name)
?.registerExtension(extension, module.project)
}
}
optionsBuilder.apply {
projectBaseDir = projectBaseDir ?: module.project.basePath?.let(::File)
val contentRoots = configuration[CLIConfigurationKeys.CONTENT_ROOTS] ?: emptyList()
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kapt4
import org.jetbrains.kotlin.analysis.project.structure.KtCompilerPluginsProvider
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
import org.jetbrains.kotlin.extensions.internal.InternalNonStableExtensionPoints
import org.jetbrains.kotlin.resolve.extensions.AssignResolutionAltererExtension
internal class StandaloneCompilerPluginsProvider : KtCompilerPluginsProvider() {
override fun <T : Any> getRegisteredExtensions(module: KtSourceModule, extensionType: ProjectExtensionDescriptor<T>): List<T> {
return extensionType.getInstances(module.project)
}
@OptIn(InternalNonStableExtensionPoints::class)
override fun isPluginOfTypeRegistered(module: KtSourceModule, pluginType: CompilerPluginType): Boolean {
val extension = when (pluginType) {
CompilerPluginType.ASSIGNMENT -> AssignResolutionAltererExtension
else -> return false
}
return extension.getInstances(module.project).isNotEmpty()
}
}