google / google/ksp

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

Open
#2,356 7 comments 4 reactions 1 assignee Claimed by @jaschdoc View on GitHub
bug P3
Dominant language
Kotlin
Stars
3.5k
Forks
415
Avg merge
1d 2h
Merged PRs (30d)
53

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.