google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.87k stars 269 forks source link

KSP2: "Unresolved reference" in common code for entities annotated with SealedObjectInstances #1651

Open Tapchicoma opened 10 months ago

Tapchicoma commented 10 months ago

Originally reported in Kotlin bug tracker here

In Kotlin multiplatform module that depends on SealedObjectInstances KSP library to generate the sealedObjectInstances() extension on the Entity Companion object build fails with Kotlin 2.0.0-Beta1 and KSP 2.0.0-Beta1-1.0.15 versions:

e: file:///home/runner/work/SealedObjectInstances/SealedObjectInstances/multi/src/commonMain/kotlin/com/example/Entity.kt:27:47 Unresolved reference 'sealedObjectInstances'.
> Task :multi:compileKotlinJvm FAILED

Repro is available here: https://github.com/SimonMarquis/SealedObjectInstances/pull/163

SimonMarquis commented 6 months ago

The current workaround is to provides a bridge layer of actual / expect (https://github.com/SimonMarquis/SealedObjectInstances/pull/163/commits/295938412c43e0d1271ea6f75fe308138c817efc)

// src/commonMain/kotlin/com/example/Entity.kt
internal expect fun Entity.Companion.all(): Set<Entity>
// src/jvmMain/kotlin/com/example/Entity.jvm.kt
internal actual fun Entity.Companion.all(): Set<Entity> = sealedObjectInstances()

Note that the workaround mentioned here does not seem to work: