https://kotlinlang.org logo
Join SlackCommunities
Powered by
# ksp
  • z

    Zach G

    02/13/2025, 10:06 PM
    Is there anyway to "mock" references to other packages that should be used by my annotation processor? I'm trying to build one in IntelliJ for use in an Android project later, but the androidx libraries are not included. Should I just import them in the project using the annotation processor like normal to test?
    a
    j
    • 3
    • 6
  • r

    Rupesh Bansal

    02/16/2025, 9:45 PM
    Hi Folks, Can we enable annotation processing on a project's dependencies? Right now when I use the Resolver in a Symbol Processor for a library, only the annotated files of this library are picked up. Is there a way to enable the resolver to pick files from dependent libraries?
    m
    y
    • 3
    • 6
  • d

    Daniel Pejcinovski

    02/18/2025, 3:00 PM
    Hello! I'm having approximately the same error as the following post on StackOverflow: https://stackoverflow.com/questions/79311813/kotlin-ksp-how-to-generate-aggregating-output-files I'm trying to, in a symbol processor, get every class implementing a specific interface, create a single new file and to it add a line for each class implementing that interface. I open the file once during processing, passing
    containingFile
    for every
    KSClassDeclaration
    I find that matches my criterion, and setting the
    aggregating
    flag to true. I also wrap each
    KSClassDeclaration
    in a
    KSTopDownVisitor
    to write my data. As far as I understand KSP, this should be everything necessary to not reprocess the classes in future runs, but I can see that it runs multiple times and as soon as it does it causes a
    fileAlreadExistsException
    . Any ideas?
    d
    j
    • 3
    • 3
  • z

    Zac Sweers

    02/21/2025, 3:39 AM
    Found what seems like a pretty significant bug in KSP1 (not sure if this also affects KSP2/if KSP2 will support compilation avoidance) https://github.com/google/ksp/issues/2347
    👀 3
  • r

    Robert Jaros

    02/21/2025, 6:13 PM
    Some time ago I've opened an issue - https://youtrack.jetbrains.com/issue/KT-70010 about the use case for
    ACTUAL_WITHOUT_EXPECT
    compiler error suppression. I'm using this suppression on the actual class, for which the corresponding expect class is generated by the KSP plugin. This suppression allows opening the clean project in the IDE without errors, even before the KSP processor is executed. Unfortunately my request was just declined by the compiler team with a suggestion to forward the problem to the KSP team. So can I ask for your suggestions on how to solve this problem?
  • r

    Robert Jaros

    02/26/2025, 6:52 PM
    I have a problem migrating to KSP2. It looks like https://github.com/google/ksp/issues/1998 but I'm on
    2.1.20-RC-1.0.30
    .
    t
    • 2
    • 5
  • d

    Daniel Pejcinovski

    03/04/2025, 1:03 PM
    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
    • 2
    • 9
  • j

    Jason Dusek

    03/04/2025, 6:15 PM
    What is a good way to generate declarations for nested classes?
  • d

    Dmitry Stakhov

    03/07/2025, 7:53 AM
    Hi folks. I am using a KSP2, so the KSP version doesn't need to be aligned with the Kotlin compiler version anymore, right? If yes, how can I disable the warning about the versions incompatibility:
    Please upgrade ksp or downgrade kotlin-gradle-plugin
  • r

    rad

    03/09/2025, 11:35 AM
    Hello, having an issue with
    CodeGenerator#createNewFileByPath
    where it will sometimes just throw a
    FileAlreadyExistsException
    :
    Copy code
    public class AxiProcessor(
        env: SymbolProcessorEnvironment,
    ) : SymbolProcessor {
        private companion object Identifiers {
            const val COMMAND: String = "net.radstevee.axi.core.command.Command"
        }
    
        private val gen: CodeGenerator = env.codeGenerator
    
        override fun process(resolver: Resolver): List<KSAnnotated> {
            val services = mutableListOf<String>()
    
            // ...
    
            gen.createNewFileByPath(
                Dependencies(false),
                "META-INF/services/$COMMAND",
                ""
            ).use { out ->
                OutputStreamWriter(out, StandardCharsets.UTF_8).use { writer ->
                    services.forEach { svc ->
                        writer.write(svc)
                        writer.write("\n")
                    }
                }
            }
    
            return emptyList()
        }
    }
    Copy code
    [ksp] kotlin.io.FileAlreadyExistsException: /home/radsteve/dev/axi/example/build/generated/ksp/main/resources/META-INF/services/net.radstevee.axi.core.command.Command
    	at com.google.devtools.ksp.common.impl.CodeGeneratorImpl.createNewFile(CodeGeneratorImpl.kt:154)
    	at com.google.devtools.ksp.common.impl.CodeGeneratorImpl.createNewFileByPath(CodeGeneratorImpl.kt:78)
    	at net.radstevee.axi.ksp.AxiProcessor.process(AxiProcessor.kt:63)
    How can I prevent this? Because I am not creating the file multiple times. The whole processing logic is also only checking for annotated declarations and adding onto the
    services
    list. If I just wrap the
    createNewFileByPath
    call in a try/catch, the file does get created but is just empty. I can't directly remove it since I don't know where it will be located. This doesn't happen if I clean build of course
    d
    • 2
    • 1
  • d

    Denis Stepanov

    03/10/2025, 8:18 AM
    I having an issue with KSP2 when generating
    .class
    files and not seing them on the classpath https://github.com/google/ksp/issues/2365 That's how we do it in Micronaut but it looks like KSP2 is having some kind of issue with. Any idea what can be wrong?
  • e

    eygraber

    03/10/2025, 2:50 PM
    I'm getting closer to flipping the KSP2 switch on, but I'm concerned about reported performance issues. Has anyone else noticed such drastic regressions?
    e
    • 2
    • 1
  • e

    eygraber

    03/12/2025, 4:47 AM
    If I wanted to "unwrap" a value class would I just look at the type of the first constructor property?
    j
    • 2
    • 4
  • r

    Remi Latapy

    03/14/2025, 3:35 PM
    👋 Is it conceivable to use KSP to generate lint rules ? I would like to create an annotation like
    @Replacing("androidx.compose.material3")
    to enforce design system use in Compose. I don't see how to do it in pure Lint rule
    e
    w
    d
    • 4
    • 5
  • j

    jbarr

    03/19/2025, 9:12 PM
    any reason there isn't an
    artifacts.zip
    for 2.1.10-1.0.31? bazel's rules_kotlin uses these for compilation
    t
    • 2
    • 2
  • e

    eygraber

    03/20/2025, 4:25 PM
    Is a release planned for 2.1.20 today, or will it come later?
    👀 3
    j
    z
    • 3
    • 5
  • z

    Zach G

    03/31/2025, 1:22 PM
    Is the Visitor pattern still considered the best approach for processing multiple separate annotations to build one or more files?
    d
    • 2
    • 1
  • r

    RTAkland

    04/01/2025, 2:54 PM
    hi, I want to use ksp to generate new code, but I dont want to generate a new file, just add the code into the generated class file, I google it, but the result says I should use asm to do this, Is this correct?
    h
    a
    • 3
    • 2
  • r

    RTAkland

    04/03/2025, 11:38 AM
    Hi, I want to use kcp to insert a new function to a class, how?, is there a example?
    e
    • 2
    • 6
  • n

    Nitesh Singh

    04/04/2025, 11:25 AM
    hey there, I am getting error how i will fix that issue ?
    g
    • 2
    • 2
  • n

    Nitesh Singh

    04/04/2025, 11:25 AM
    image.png
  • f

    Forrest Pangborn

    04/10/2025, 1:32 PM
    I have a processor that creates KotlinPoet
    TypeName
    instances from
    KType
    values, using the
    asTypeName()
    extension to
    KType
    provided by KotlinPoet. Looks like the implementation of
    asTypeName()
    relies on
    kotlin-reflect
    (the
    typeParameters
    property on
    KClass
    ). In KSP1, using
    asTypeName()
    like this works but with KSP2 I’m getting an exception:
    Copy code
    Caused by: kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
        at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:88)
        at kotlin.jvm.internal.ClassReference.getTypeParameters(ClassReference.kt:38)
        at com.squareup.kotlinpoet.ParameterizedTypeName$Companion.get$kotlinpoet(ParameterizedTypeName.kt:236)
        at com.squareup.kotlinpoet.ParameterizedTypeNames.asTypeName(ParameterizedTypeName.kt:275)
    My processor already includes
    kotlin-reflect
    as an implementation dependency – and I've tried adding it for the consumer of the processor too, but same failure. Running with latest KSP version but in legacy mode (via
    useKsp2 = false
    ) works fine as well – so seems definitely an issue with KSP2. Anyone have any suggestions on other things I can try?
  • t

    Tomáš Procházka

    04/14/2025, 10:35 AM
    Hi. Is there please any way how to get more detail performance statistics from KSP? In our project KSP take more than kotlin compilation, we are using room, dagger, anvil and some custom KSP plugin. I would like to know which KSP plugin took how much time, it will be somehow shared, right? But it is there a way how to somehow find which plugin is the bottleneck? I care mainly about incremental IDE build, where change in one class cause 25s KSP task and only 2s kotlin compilation. I would like to found what causing that 25s delay after just one class change.
    👀 1
    t
    t
    t
    • 4
    • 9
  • t

    trevjones

    04/14/2025, 9:23 PM
    Ting-Yuan you alluded to a better solution being more possible end of year here? Just trying to track down all my loose ends for turning on
    useKSP2
    and found you had given them a nudge on what appears to be my last blocker now. Any chance there is a more stable API we can ask the moshi maint team to look at now?
    t
    • 2
    • 2
  • d

    Denis Stepanov

    04/23/2025, 1:08 PM
    @Ting-Yuan Huang It looks like your last commits broke Micronaut Framework integration https://github.com/google/ksp/commit/498cbe60c132fb355dc36cc7ac6c8d7da229671a I have almost finished figuring out all the changes in KSP2 and the last snapshot change completly broke annotation reading. Cannot properly tell was is missing. I had to build a local snapshot to finish the work. Can you please investigate what can be wrong?
    t
    • 2
    • 1
  • a

    Aru Jeganathan

    04/24/2025, 5:56 PM
    I have a maven project in my company and we're using Kotlin 1.9 with kapt. I wanna migrate to kotlin 2.x. Is there a way I can migrate from kapt to ksp with maven?
  • o

    Olivier Notteghem

    04/28/2025, 11:39 PM
    Could someone tell me how to get traction on https://github.com/google/ksp/issues/1385 ? This bug affects most bazel + rules_kotlin users and introduces a bad performance bottleneck. Thanks in advance.
    t
    • 2
    • 8
  • j

    Jake Woods

    05/01/2025, 12:33 AM
    Is there a way to get the return type of a generic constructor in ksp2 that preserves the type parameters? I have a class like this:
    Copy code
    sealed class KonvertResult<out T, out E> {
      data class Ok<out T>(val value: T) : KonvertResult<T, Nothing>()
      data class Err<out E>(val error: E) : KonvertResult<Nothing, E>()
    }
    I'm getting a reference to it's constructor in ksp like this:
    Copy code
    private val okConstructor = resolver
        .getClassDeclarationByName<KonvertResult.Ok<*>>()
        ?.getConstructors()
        ?.firstOrNull()
    In ksp1 when I run
    okConstructor.returnType
    I get
    Ok<T>
    . But in ksp2 when I return
    okConstructor.returnType
    I get
    Ok<*>
    . Is there any way I can get back to
    Ok<T>
    ?
  • z

    Zac Sweers

    05/07/2025, 8:01 PM
    wanted to nudge this issue - https://github.com/google/ksp/issues/1789#issuecomment-2860100164 With AGP 8.10.0, we're more or less not able to apply the KSP plugin on-demand with our convention plugin anymore due to its odd interaction with AGP's lifecycle
    👀 1
    t
    • 2
    • 4
  • j

    JP Sugarbroad

    05/08/2025, 7:50 PM
    Anyone run into an issue where ksp generates classes with incorrect name mangling? (2.0.21 was fine, 2.1.20 is broken)
    • 1
    • 1