Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation error is not thrown from KSPLogger logging error in 1.6.10-1.0.3 #854

Closed
seankim-android opened this issue Feb 17, 2022 · 7 comments

Comments

@seankim-android
Copy link

I have a SymbolProcessor which was throwing a KotlinCompilation.ExitCode.Compilation.ExitCode.COMPILATION_ERROR from environment.logger.error() as expected in 1.0.2. I have some unit tests to catch the error, which is now failing with 1.0.3 because it returns KotlinCompilation.ExitCode.Compilation.ExitCode.OK, and the KSPLogger API doesn't fail the compilation.

Is this intended? If so, is there a more preferred way to fail the compilation?

@seankim-android seankim-android changed the title Compilation error is not thrown from KSPLogger logging error in 1.0.3 Compilation error is not thrown from KSPLogger logging error in 1.6.10-1.0.3 Feb 17, 2022
@ting-yuan
Copy link
Collaborator

Yes it is. It is changed from returning COMPILATION_ERROR to OK to workaround this issue:
https://youtrack.jetbrains.com/issue/KT-30172

All callers to the AnalysisHandlerExtension compiler plugin (KSP is one of them) in the compiler handle errors by checking error logs. Therefore in Gradle and command line users shouldn't notice the difference.

This reminds me that there can be issues when running from kotlin-compile-testing, for example 😢 May I know how you run KSP?

@ting-yuan
Copy link
Collaborator

btw, the upstream fix to KT-30172 is here: JetBrains/kotlin#3950

We'll roll back to return COMPILATION_ERROR once it is merged.

@ZacSweers
Copy link
Contributor

We run it via kotlin-compile-testing in unit tests

@seankim-android
Copy link
Author

seankim-android commented Feb 17, 2022

Adding some sample code snippet

Sample SymbolProcessorProvider

@AutoService(SymbolProcessorProvider::class)
class MySymbolProcessorProvider : SymbolProcessorProvider {
  override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
    return object : SymbolProcessor {
      override fun process(resolver: Resolver): List<KSAnnotated> {
        environment.logger.error("throw an error")
        return emptyList()
      }
    }
  }
}

Sample test

@Test
fun `Compilation fails with KSPLogger error`() {
  val compilation = newCompilation(
    """
      package example.test

      interface SomeClass
      """
  )
  val result = compilation.compile()
  Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
}

Output from the test in 1.0.2

e: Error occurred in KSP, check log for detail
e: [ksp] throw an error

Test passed

Output from the test in 1.0.3

e: Error occurred in KSP, check log for detail
e: [ksp] throw an error

expected: COMPILATION_ERROR
but was : OK
expected: COMPILATION_ERROR
but was : OK
	at app//slack.guinness.compiler.MySymbolProcessorTest.Compilation fails with KSPLogger error(MySymbolProcessorTest.kt:25)

@seankim-android
Copy link
Author

seankim-android commented Feb 18, 2022

Thanks @ting-yuan for your response! Is there a workaround for raising the COMPILATION_ERROR with ksp for the example test case provided above, or do we have wait for the upstream fix for this? I'm trying to see if there is another way to fail compilation without changing the unit test.

@ting-yuan
Copy link
Collaborator

Here is a patch to KCT to partially fix this problem: Unfortunately INTERNAL_ERROR is no longer possible; Any previous INTERNAL_ERROR in KSP becomes COMPILATION_ERROR until the fix lands in Kotlin compiler and KSP switches back.

I cannot think of any workaround in processors or outside of kotlin-compile-testing, because KSP always returns OK. It returns OK even when processor throws an exception.

@ting-yuan
Copy link
Collaborator

Fixed in another way in #865.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants