square / moshi

A modern JSON library for Kotlin and Java.
https://square.github.io/moshi/1.x/
Apache License 2.0
9.74k stars 758 forks source link

KSP2 crash when using @JsonQualifier #1874

Closed Jean-Daniel closed 2 months ago

Jean-Daniel commented 2 months ago

When trying to generate Adapter for classes using a @JsonQualifier, KSP fails with the error:


> Task :app:kspDebugKotlin FAILED
e: [ksp] Error preparing ApiUserItem: jdk.proxy5/jdk.proxy5.$Proxy154.value(Unknown Source)
com.squareup.moshi.kotlin.codegen.ksp.MoshiApiUtilKt.generator(MoshiApiUtil.kt:77)
com.squareup.moshi.kotlin.codegen.ksp.JsonClassSymbolProcessor.adapterGenerator(JsonClassSymbolProcessorProvider.kt:122)
com.squareup.moshi.kotlin.codegen.ksp.JsonClassSymbolProcessor.process(JsonClassSymbolProcessorProvider.kt:92)
com.google.devtools.ksp.impl.KotlinSymbolProcessing.execute(KotlinSymbolProcessing.kt:538)
com.google.devtools.ksp.impl.KSPLoader$Companion.loadAndRunKSP(KSPLoader.kt:36)
com.google.devtools.ksp.impl.KSPLoader.loadAndRunKSP(KSPLoader.kt)
…

The class being compiled is simply:

@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class ISO8061

@JsonClass(generateAdapter = true)
open class ApiUserItem(
  @field:Json(name = "slug") val slug: String,
  @field:Json(name = "date_added") @field:ISO8061 val date: Long,
)

You can use this reduced test case to reproduce the bug.

git clone https://github.com/Jean-Daniel/moshiksp2
cd moshiksp2
./gradlew :app:assembleDebug
> Task :app:kspDebugKotlin FAILED
…
ZacSweers commented 2 months ago

Upstream KSP2 bug, please file with them

ting-yuan commented 2 months ago

This is caused by a breaking change in KSP2. @ZacSweers could this copy be deleted and let moshi use the implementation from KSP?

ting-yuan commented 2 months ago

Removing it doesn't seem to break any tests: https://github.com/square/moshi/pull/1875