Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KSP2 version 2.1.20-RC-1.0.30 crashes with NoSuchElementException: Collection contains no element matching the predicate #2356

Open
rjaros opened this issue Feb 28, 2025 · 1 comment
Milestone

Comments

@rjaros
Copy link

rjaros commented Feb 28, 2025

After migration from KSP1 to KSP2 I have a problem when I try to read a parameter of an annotation on a class, which has a default value. The annotation is defined like this:

@Target(AnnotationTarget.CLASS)
public annotation class SimpleHtmlComponent(val tagName: String, val withText: Boolean = false)

and the code of the processor looks like this:

val simpleHtmlComponent = classDeclaration.getAnnotationsByType(SimpleHtmlComponent::class).first()
val tagName = simpleHtmlComponent.tagName
val withText = simpleHtmlComponent.withText

The last line throws the exception:

Caused by: java.util.NoSuchElementException: Collection contains no element matching the predicate.
        at com.google.devtools.ksp.UtilsKt.createInvocationHandler$lambda$8(utils.kt:587)
        at jdk.proxy10/jdk.proxy10.$Proxy212.withText(Unknown Source)
        at dev.kilua.ksp.KiluaProcessor.process(KiluaProcessor.kt:62)
        at com.google.devtools.ksp.impl.KotlinSymbolProcessing$execute$1$1.invoke(KotlinSymbolProcessing.kt:581)
        at com.google.devtools.ksp.impl.KotlinSymbolProcessing$execute$1$1.invoke(KotlinSymbolProcessing.kt:579)
        at ksp.com.google.devtools.ksp.common.IncrementalContextBase.closeFilesOnException(IncrementalContextBase.kt:404)
        at com.google.devtools.ksp.impl.KotlinSymbolProcessing.execute(KotlinSymbolProcessing.kt:579)
        at com.google.devtools.ksp.impl.KSPLoader$Companion.loadAndRunKSP(KSPLoader.kt:37)
        at com.google.devtools.ksp.impl.KSPLoader.loadAndRunKSP(KSPLoader.kt)

To reproduce:

  1. Clone https://github.com/rjaros/kilua project
  2. Checkout ksp2issue branch
  3. Run ./gradlew allTest --stacktrace
  4. See the crash

The annotations are in the modules/kilua-annotations module. The processor in the plugins/kilua-ksp-processor module.

The same code works fine with KSP1.

@ting-yuan ting-yuan modified the milestones: 1.0.0, 1.0.32 Mar 13, 2025
@ting-yuan
Copy link
Collaborator

ting-yuan commented Mar 14, 2025

The exception happened when it tries to get the default value of the annotation in a KMP build. It works on KSP1 by accident, where 1) JVM is part of the targets, and 2) KSP1 didn't check the targets and unconditionally tries to load the annotation as a Java class instance. Therefore it works when JVM is present in KSP1.

In KMP, annotation default values from libraries are generally not available other than the JVM platform. We are awaiting the fix in the compiler: https://youtrack.jetbrains.com/issue/KT-59526/Store-annotation-default-values-in-metadata-on-JVM (ignore the -on-JVM part of the description.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants