I'm having an issue, a sort of continuation on my ...
# ksp
d
I'm having an issue, a sort of continuation on my earlier issue. In a KMP project, I have a generator, which generates a source file to
build/generated/ksp/metadata/common/kotlin/MyGeneratedFile.kt
and an additional in
build/generated/ksp/jvm/jvmMain/kotlin/MyGeneratedFile.kt
. This is fine and good, but now I want to use the generated files, and I just can't figure out how to add it as a dependency or source sets. I'm using the following to add it as a dependency:
Copy code
dependencies {
    add("kspCommonMainMetadata", project(":myProcessors"))
    add("kspJvm", project(":myProcessors"))
    add("kspJvmTest", project(":myProcessors"))
}
Again, this works, and is what gets me the files when I build. However, I use Kotlin MPP specific annotations when generating the files, such as
@JsExport
and
@JsStatic
, which aren't processable on the JVM platform and I get
Declaration annotated with '@OptionalExpectation' can only be used in common module sources
errors. If I remove the
KspJvm
and
KspJvmTest
lines I only get
unresolved
errors and the processor does not run on building. There is something I'm just not getting here, any ideas?
j
I may not be reading it correctly; but it sounds like you're trying to add the generated files as a dependency? I'm not sure why you'd need to do that, though.
d
Yep, the files I process end up creating a .kt file that I now intend to consume, and I need to add a dependency somewhere so that my processor runs when I build.
j
I think what you are describing there, is a dependency that you add in the processor.
Say that you create a new file with a
CodeGenerator
, like this:
Copy code
val file = codeGenerator.createNewFile(...)
The first argument is the dependency specification of the generated file.
For example:
Copy code
val file = codeGenerator.createNewFile(Dependencies(, file1, file2, ...), ...)
The annotated symbols have a
.containingFile
that you can use for this purpose.
d
In this case I'm not consuming annotations in order to generate code, I use interfaces to generate what can be considered static/global information which needs to be consumed. I'll see if I can find a way to determine which files would be dependent on the code being generated.
Then again, the way I'm doing it might be unintended, I guess what I might be able to do is generate extension methods for those classes implementing the interfaces and add those as dependencies, and I guess anything depending on them would in turn be dependent?