tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support K2

ZacSweers opened this issue · comments

Currently the new K2 compiler cannot even be enabled in KCT tests due to its own registrar not supporting it

e: There are some plugins incompatible with K2 compiler:
  com.tschuchort.compiletesting.MainComponentRegistrar

It would be helpful to enable this so plugin authors can start testing their plugins.

K2 should work now on the latest snapshot release. Both the compilerPlugins and commandLineProcessors have to be empty to prevent the resources path (containing the MainCommandlineProcessor) from being added to the compilation's plugin classpath.

I commented on the PR as well - why not just make this project's plugin compatible? It's just a boolean property that needs to be overridden to return true

Oh, I didn't know that was an option. Sounds good!

Do you perhaps have some documentation on hand about what makes a plugin compatible/incompatible? The MainComponentRegistrar also registers KaptComponentRegistrar, so I need to know if KAPT is compatible with K2. This blog post suggests that KAPT is compatible, but it is not listed here.

Do you perhaps have some documentation on hand about what makes a plugin compatible/incompatible

Plugins must opt-in by returning true to supportsK2 like so: https://github.com/ZacSweers/redacted-compiler-plugin/blob/z/firTesting/redacted-compiler-plugin/src/main/kotlin/dev/zacsweers/redacted/compiler/RedactedPlugin.kt#L34-L35

Kapt is not compatible with K2 and I'm not sure it ever will be. That said, I don't think that's a blocker. I think this plugin should only apply kapt if the test using it needs kapt, not always. Would actually be a nice perf boost for tests that don't need kapt

in much the same way that it doesn't automatically apply KSP, for instance

I think this plugin should only apply kapt if the test using it needs kapt, not always

It seems to me this is already the case (at least the project components don't get registered and I believe the extra call to K2JVMCompiler is skipped as well).

I've been looking at the Kotlin compiler source code a bit and this property you're talking about only appears in 1.7.20-beta. Current version is 1.7.10, so I won't be able to include it in the stable release. Can you make do with the workaround until then or do you need it to test your own plugins? In that case, I'll have to make a pre-release for 1.7.20-beta.

It seems to me this is already the case (at least the project components don't get registered and I believe the extra call to K2JVMCompiler is skipped as well).

I don't think so, it appears to always be registered here

only appears in 1.7.20-beta. Current version is 1.7.10, so I won't be able to include it in the stable release.

You can compile against 1.7.20-beta without forcing it on older consumers, no?

I don't think so, it appears to always be registered here

Yes, but if you go into that method, it has an early exit when no annotation processors are given 😃.

You can compile against 1.7.20-beta without forcing it on older consumers, no?

Perhaps. The additional supportsK2 property should not lead to binary incompatibilities, but there may be other changes. I'll have to check it.

I took a pass at it in #306. The compileOnly approach there to avoid imposing the version works, but as I've mentioned before I think this library should just tie versions to the kotlinc version it's built against.