Hello, I'm just beginning to familiarize myself wi...
# ksp
a
Hello, I'm just beginning to familiarize myself with KSP and compiler plugins in general. I decided to rely on KSP to reduce the manual boilerplate in my codebase, but I'm having trouble getting it to work. Here's the error I'm currently facing: https://pastebin.com/JiV8d7iy
It's a Kotlin/Multiplatform project. And here's the processor file itself:
Clean rebuild does not help either
d
I didn't read the code in detail, but one thing that stands out is you are always writing the file even if there are no relevant symbols found. This is a bit unusual and I wonder if it would interfere with multiround if there are other processors deferring symbols since the rounds stop when no processor produces output. Maybe you could try refactoring it so that you only write the file in the case where some symbols of interest are actually found?
j
e: *[*ksp*]* kotlin.io.FileAlreadyExistsException
make sure you are not creating a same file multiple times.
👍 2
a
@David Rawson Hey, I did as you said and the error is gone. The project builds fine now but the KSP generated file is oddly empty. Could I possibly be doing something wrong?
d
@axeon do you have a test for this processor to help diagnose other issues? There are examples that use https://github.com/tschuchortdev/kotlin-compile-testing to write tests against a processor
a
Hey, I was able to resolve the problem by filtering instances of
KSFunctionDeclaration
rather than
KSFunction
. Now, the generated file has unchecked cast warnings that I want to suppress using
@file:Suppress("UNCHECKED_CAST")
. How can I possibly do that? It may look like bad practice, but I'm confident that all the casts are valid.
d
Looking at the KotlinPoet docs, maybe something like this: https://square.github.io/kotlinpoet/annotations/
Copy code
val utils = FileSpec.builder("com.example", "Utils")
  .addAnnotation(
    AnnotationSpec.builder(Suppress::class)
      .useSiteTarget(UseSiteTarget.FILE)
      .addMember("names = %S", "UNCHECKED_CAST")
      .build()
  )
a
Works flawlessly, thank you!
🙏 1
191 Views