tschuchortdev / kotlin-compile-testing

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

KSP: use compiler's message collector #244

Closed ting-yuan closed 2 years ago

ting-yuan commented 2 years ago

Now that KSP 1.0.3 exits with error message instead of explicit return code, it needs to inform compiler via compiler's message collector.

Also updated a test case to expect COMPILATION_ERROR instead of INTERNAL_ERROR, to reflect the behavior change.

PhilGlass commented 2 years ago

@ting-yuan I think you'll also want to explicitly bump the KSP dependency to 1.0.3, because there was a binary incompatible change to the constructor of SymbolProcessorEnvironment (which is part of the symbol-processing-api artifact that processors depend on):

e: java.lang.NoSuchMethodError: 'void com.google.devtools.ksp.processing.SymbolProcessorEnvironment.<init>(java.util.Map, kotlin.KotlinVersion, com.google.devtools.ksp.processing.CodeGenerator, com.google.devtools.ksp.processing.KSPLogger)'
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$3$1.invoke(KotlinSymbolProcessingExtension.kt:176)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$3$1.invoke(KotlinSymbolProcessingExtension.kt:174)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.handleException(KotlinSymbolProcessingExtension.kt:287)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.doAnalysis(KotlinSymbolProcessingExtension.kt:174)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:120)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:96)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:262)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:53)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:113)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:253)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:100)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:58)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:170)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:92)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
    at com.tschuchort.compiletesting.AbstractKotlinCompilation.compileKotlin(AbstractKotlinCompilation.kt:188)
    ...

A temporary workaround is to add a testImplementation dependency on com.google.devtools.ksp:symbol-processing:1.0.3.

ting-yuan commented 2 years ago

KSP 1.0.4 will be out to fix the ABI incompatibility.

ting-yuan commented 2 years ago

TL;DR: This binary incompatibility issue isn't related with this PR and the failing test directly. It should be fixed by bumping KSP to 1.0.3, which depends on this PR.

Details about the incompatibility: This is probably not cleanly fixable in KSP with another release. The issue here is that the new API jar, which is likely a dependency brought by some processor, is not compatible with some old KSP implementation used by KCT. Since there are added APIs, the version of implementation nevertheless need to match it. So @PhilGlass is right, the only fix should be bumping KSP to 1.0.3 in KCT for processors that compile with KSP 1.0.3. (Strictly speaking, for processors that uses new APIs in KSP 1.0.3).

ting-yuan commented 2 years ago

KSP 1.0.4 reverted to the old behavior (exit code) by default and only enables the workaround when invoked from Gradle. The tests and abi compatibility should be satisfied now.

Despite of the tests, getting message collector from compiler instead of creating one is more consistent with KSP's implementation and should be less surprising. Therefore the PR could still be useful.

ting-yuan commented 2 years ago

Well, the compiler prioritize error logs over return code so the test will still be broken with this PR.