tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
642 stars 61 forks source link

Support K2 #302

Open ZacSweers opened 1 year ago

ZacSweers commented 1 year ago

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.

tschuchortdev commented 1 year ago

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.

ZacSweers commented 1 year ago

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

tschuchortdev commented 1 year ago

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

tschuchortdev commented 1 year ago

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.

ZacSweers commented 1 year ago

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

ZacSweers commented 1 year ago

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

tschuchortdev commented 1 year ago

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.

ZacSweers commented 1 year ago

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?

tschuchortdev commented 1 year ago

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 :smiley:.

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.

ZacSweers commented 1 year ago

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.

vdshb commented 2 weeks ago

Is it correct, that testing compiler-plugins/ksp-plugins for Kotlin 2.0.0 is not supported yet?