Trying to write a small compiler plugin following ...
# compiler
r
Trying to write a small compiler plugin following https://bnorm.medium.com/writing-your-second-kotlin-compiler-plugin-part-1-project-setup-7b05c7d93f6c How can I debug why the kotlin compiler plugin is not being activated? I can see that the gradle plugin part is being activated OK, and I can set a breakpoint in the debugger for it when running gradle in debug mode. But the kotlin compiler part (inheriting from CompilerPluginRegistrar) is not running, and not printing my test message as per this code:
Copy code
@OptIn(ExperimentalCompilerApi::class)
@AutoService(CompilerPluginRegistrar::class)
class MyPlugin : CompilerPluginRegistrar() {
    override val supportsK2: Boolean
        get() = true

    override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {

        val messageCollector = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)

        messageCollector.report(
            CompilerMessageSeverity.WARNING,
            "*** Hello from MyPlugin ***"
        )
    }
}
in the project which uses the plugin, I have defined
Copy code
tasks.withType<KotlinCompile> {
    kotlin {
        jvmToolchain(19)
        kotlinOptions {
            verbose = true
            useK2 = false
        }
    }
}
and when running
./gradlew --debug
i can grep for KOTLIN in the logs and see some of the compiler output, where my plugin is mentioned:
Copy code
2023-03-26T12:50:40.339+0200 [DEBUG] [org.gradle.api.Project] [KOTLIN] Subplugin my-plugin loaded
2023-03-26T12:50:40.339+0200 [DEBUG] [org.gradle.api.Project] [KOTLIN] Loading subplugin my-plugin
2023-03-26T12:50:40.339+0200 [DEBUG] [org.gradle.api.Project] [KOTLIN] Adding 'se.rrva:my-plugin:1.0.5' to 'kotlinCompilerPluginClasspathTest' configuration
annotation processing was not working for @AutoService, now it does and I got my printouts as expected
thanks for the article by the way @bnorm
e
@rrva what did you have to do to get the annotation processing working with @Autoservice?
r
in the dependencies section in gradle I had forgotten the annotation processor:
Copy code
implementation("com.google.auto.service:auto-service:1.0.1")
    kapt("com.google.auto.service:auto-service:1.0.1")
as well as the kapt plugin in the plugins section
Copy code
kotlin("kapt") version("1.8.20")