https://kotlinlang.org logo
Title
u

Udi Cohen

08/23/2021, 8:31 PM
Hi, I'm trying to use KSP from command line and while I do get the generated classes in the kotlinOutputDir, the
kotlinc
command doesn't compile the project's classes and places them in the output folder. As a repro example using the playground project, after running the following command I don't see anything in the
build2/classes
folder. I do however see the generated
AClassBuilder
class inside
build2/generated/ksp/main/kotlin
.
kotlinc \
-d /Users/udinic/projects/ksp-playground4/workload/build2/classes \
-verbose \
-cp /Users/udinic/projects/ksp-playground4/test-processor/build/libs/test-processor-1.0-SNAPSHOT.jar \
-Xplugin=/Users/udinic/tmp/ksp-stuff/symbol-processing-api-1.5.21-1.0.0-beta07.jar \
-Xplugin=/Users/udinic/tmp/ksp-stuff/symbol-processing-cmdline-1.5.21-1.0.0-beta07.jar \
-P plugin:com.google.devtools.ksp.symbol-processing:apclasspath=/Users/udinic/projects/ksp-playground4/test-processor/build/libs/test-processor-1.0-SNAPSHOT.jar \
-P plugin:com.google.devtools.ksp.symbol-processing:projectBaseDir=/Users/udinic/projects/ksp-playground4/workload/src/main/java \
-P plugin:com.google.devtools.ksp.symbol-processing:classOutputDir=/Users/udinic/projects/ksp-playground4/workload/build2/classes/kotlin/main \
-P plugin:com.google.devtools.ksp.symbol-processing:kotlinOutputDir=/Users/udinic/projects/ksp-playground4/workload/build2/generated/ksp/main/kotlin \
-P plugin:com.google.devtools.ksp.symbol-processing:javaOutputDir=/Users/udinic/projects/ksp-playground4/workload/build2/generated/ksp/main/java \
-P plugin:com.google.devtools.ksp.symbol-processing:resourceOutputDir=/Users/udinic/projects/ksp-playground4/workload/build2/generated/ksp/main/resources \
-P plugin:com.google.devtools.ksp.symbol-processing:cachesDir=/Users/udinic/projects/ksp-playground4/workload/build2/generated/ksp/main/cache \
-P plugin:com.google.devtools.ksp.symbol-processing:kspOutputDir=/Users/udinic/projects/ksp-playground4/workload/build2/ksp \
/Users/udinic/projects/ksp-playground4/workload/build2/generated/ksp/main/kotlin/ /Users/udinic/projects/ksp-playground4/workload/src/main/java
If I disable KSP, by removing the
-Xplugin
params, I will see the compiled classes in the classes folder, but without any new generated classes of course. Is there something missing in my command?
t

Ting-Yuan Huang

08/23/2021, 10:24 PM
Hi Udi, KSP currently runs in its own (compiler) invocation. Meaning that the compiler invocation with
-Xplugin
only calls processors to generate sources but not compile them into classes. You'll need a second compiler invocation (without
-Xplugin
and corresponding arguments) to compile them, as if the generated sources are provided / hand-written.
u

Udi Cohen

08/23/2021, 10:34 PM
Oh I see, good to know. Is there a plan to make it run in a single invocation? It sounds similar to the earlier KAPT, before all its steps were combined into one, I'm curious if this is something we can expect also in KSP or is there some technical limitation that prevents that from happening?
t

Ting-Yuan Huang

08/23/2021, 10:38 PM
We made the separation for incremental processing and incremental compilation. For command line usages, it's doable to merge them because incremenetal compilation/processing isn't available
u

Udi Cohen

08/23/2021, 10:47 PM
Makes sense. I assume build times will be slower if I need a separate kotlinc invocation for KSP, which might still be better than KAPT but it could make the difference less appealing. If that's something you're open to, and maybe don't have plans to do in the near future, could I help merging these steps for the command line option? If so, any code pointers I can start looking at?
t

Ting-Yuan Huang

08/23/2021, 11:07 PM
Sure! The first step that I can think of is adding an option to enable this one-invocation mode. When returning to compiler, provide the correct context and tell it to keep running. Eventually it'll return to here (K/N, JVM, ...) in compiler. Please note that the "continuing with compilation" behavior is implemented separately in each target and, IMHO, the major effort will be testing all targets.
u

Udi Cohen

08/23/2021, 11:31 PM
Cool I'll give it a try. Any development tips? Are you using something like https://github.com/tschuchortdev/kotlin-compile-testing to get a debugger support? I see from previous posts in the channel that
KSPLogger
is printing to the terminal when using
gradle --debug
, but is there a way to get
KSPLogger
to print to stdout when using command line? Thanks.
t

Ting-Yuan Huang

08/23/2021, 11:38 PM
I can't remember whether K/N uses a Kotlin compile daemon or not. If yes, and if you're using Gradle,
-Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=8765\,server=y\,suspend=y"
is useful. If running directly from command line, there should be a way to specify jvm options similar to the above, but I cannot really recall.
For logger,
logger.warn()
(either KSPLogger or it's underlying impl.) should make it always print to stdout.
u

Udi Cohen

08/23/2021, 11:55 PM
Ok thanks! BTW, for context, I’m adding KSP support in the Buck build system for building Android code. That’s why I need to use KSP from the command line.
👍 1