https://kotlinlang.org logo
#ksp
Title
# ksp
d

David Herman

11/01/2023, 5:50 PM
I was using the
generatedByKsp
sourcesets in my project for code that I generate externally that should not get processed by ksp. That just went away in 1.9.20-RC2. Any advice for migration steps?
To make things a bit more nuanced (in case it matters), the code that gets generated externally is not generated by ksp but ksp is run first in order to process the user's code and then generate some metadata that I use to generate that additional code. Basically a tag team between ksp and Gradle.
t

Ting-Yuan Huang

11/01/2023, 8:56 PM
Can you try to add them directly to
compileKotlin
tasks? KSP takes source set from KGP, not
compileKotlin
, so sources direclty added to
compileKotlin
won't get processed by default.
d

David Herman

11/01/2023, 8:59 PM
We'll try, thanks!
Any guidance on how to add source files to the
compileKotlin
tasks? We tried a few options but are still not sure how we're supposed to add them. For example, there is a "source" function but when we try to use it we get "The value for this file collection cannot be changed". Any example code you can link to by any chance?
t

Ting-Yuan Huang

11/01/2023, 10:06 PM
May I know when you call
source
? IIUC it should be fine in the configuration phase of Gradle.
Here is an example when KSP cannot add generated source to the source set, and it has to add to the
compileKotlin
directly: https://github.com/google/ksp/blob/main/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt#L539
A common cause that
source
cannot be changed is that it is already resolved accidentally at configuration time.
d

David Herman

11/01/2023, 10:18 PM
We are setting
source
at configuration time. Here's the rough code (a bunch of noise ellided):
Copy code
Plugin<Project> {
  override fun apply(project: Project) {
    project.extensions.getByType<KotlinMultiplatformExtension>().targets.withType<KotlinJsIrTarget>().configureEach {
    project.tasks.withType(Kotlin2JsCompile::class.java).configureEach {
      this.source(genTask)
    }
  }
}
where
genTask
is a Gradle task that is associated with an output source dir.
We're of course not confident yet, but we wonder if you have access to an earlier point in the Kotlin compilation lifecycle since you're overriding a method given to you from the Kotlin Compiler plugin that we don't have access to when registering with Gradle.
t

Ting-Yuan Huang

11/01/2023, 10:28 PM
this.source(genTask)
looks fishy to me. Should it be
it
?
d

David Herman

11/01/2023, 10:28 PM
Sorry, that is code we're trying to simplify
genTask
is a task we registered ourselves elsewhere, but we didn't include it in the example above
t

Ting-Yuan Huang

11/01/2023, 10:29 PM
I mean, should it be
it.source(genTask)
d

David Herman

11/01/2023, 10:29 PM
Oh, that's what you mean
Sorry
Let me check
Ah, it's
this
and not
it
because we're using the
kotlin-dsl
plugin.
this
is
Kotlin2JsCompile
t

Ting-Yuan Huang

11/01/2023, 10:30 PM
but isn't the parameter of
configureEach
it
?
d

David Herman

11/01/2023, 10:35 PM
The type is
Copy code
void configureEach(Action<? super T> action);

// where...
@HasImplicitReceiver
public interface Action<T> {
    void execute(T t);
}
where I believe the
@HasImplicitReceiver
is what results in the value being a "this" and not an "it"
You can see the "this" annotations in the screenshot here
t

Ting-Yuan Huang

11/01/2023, 10:38 PM
I see. Sorry for the silly question.
d

David Herman

11/01/2023, 10:39 PM
No, not silly at all. I had to dig in and check. Sometimes the fix is simple stuff like that so I'm glad you confirmed.
t

Ting-Yuan Huang

11/01/2023, 10:45 PM
Another thing:
project.tasks.withType(Kotlin2JsCompile::class.java)
also returns ksp tasks. You may need to filter them out: https://github.com/google/ksp/blob/main/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KotlinFactories.kt#L242
d

David Herman

11/01/2023, 11:04 PM
Ah so yeah we might be setting source on irrelevant tasks. We'll look into that.
OK looks like we're working on our end. Setting
source
was the way to go, and a lot easier than what we thought we had to do. Thank you!
👍 1
Sorry to follow up on this, but even though I thought things were working for me, I tried using Kotlin 2.0.0-Beta1 to see if things would work, and our KSP fix is failing with it. (For a reminder, this thread is about marking generated code as something that should not get processed by KSP) Code is here for how we're currently doing this (by setting
source
) In Kotlin 2.0.0, it looks like this approach is going to stop working. I'm seeing the error
Source '...\kobweb\playground\site\build\generated\kobweb\app\src\jvmMain\kotlin\ApisFactoryImpl.kt' does not belong to any module
Have you dealt with this by any chance and already figured out how to resolve it? Does it make sense for KSP to expose an API where we can tell it to exclude processing some code?
t

Ting-Yuan Huang

11/29/2023, 9:06 AM
Is that error message produced by some ksp processors? I'd also make sure allowSourcesFromOtherPlugins isn't turned on; It causes ksp to get sources directly from compilation tasks, instead of source sets.
d

David Herman

11/30/2023, 12:53 AM
The error message comes from Kotlin
It doesn't like what we're doing with
source
apparently
9 Views