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 throws NoSuchElementException for data classes with @JsonAdapter(generateAdapter = true) #1870

Closed
Hiraev opened this issue Jul 27, 2024 · 1 comment
Labels

Comments

@Hiraev
Copy link

Hiraev commented Jul 27, 2024

Issue

When the ksp.useKSP2 flag is enabled, KSP2 throws a java.util.NoSuchElementException under two specific
cases:

  • when data classes are annotated with @JsonAdapter(generateAdapter = true)
  • when enum classes are annotated with @JsonAdapter(generateAdapter = false) (notwithstanding their lack of necessity)

Environment

  • Kotlin 2.0.0
  • KSP 2.0.0-1.0.23
  • Moshi 1.15.1
  • Java 17
  • Gradle 8.5
  • macOS Sonoma 14.1

Example

To recreate the issue, follow these instructions:

  1. Prepare directories for project
mkdir -p k2 k2/gradle k2/src/main/kotlin/org/example; cd k2
  1. Create libs.versions.toml
echo '
[versions]
kotlin = "2.0.0"
ksp = "2.0.0-1.0.23"
moshi = "1.15.1"

[libraries]
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
moshi-codegen = { module = "com.squareup.moshi:moshi-kotlin-codegen", version.ref = "moshi" }

[plugins]
jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
' > gradle/libs.versions.toml
  1. Create build.gradle.kts
echo '
plugins {
    alias(libs.plugins.jvm)
    alias(libs.plugins.ksp)
}

repositories {
    mavenCentral()
}

dependencies {
    ksp(libs.moshi.codegen)
    implementation(libs.moshi)
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
' > build.gradle.kts
  1. Create Kotlin file
echo '
package org.example

import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class Person(val a: Int)

@JsonClass(generateAdapter = false)
enum class Cell {
    BLACK,
    WHITE,
}
' > src/main/kotlin/org/example/Example.kt
  1. Add Gradle wrapper
gradle wrapper --gradle-version 8.5 --distribution-type all
  1. Build project without KSP2
./gradlew build -Pksp.useKSP2=false

This build finishes successfully.

  1. Build project with ksp2
./gradlew build -Pksp.useKSP2=true

This build fails with exception

> Task :kspKotlin FAILED
e: [ksp] java.util.NoSuchElementException: Collection contains no element matching the predicate.

Removing the data modifier from the Person class and the annotation from the enum class allows the KSP processor to complete its task without encountering any issues.

Interesting point. Next test in JsonClassSymbolProcessorTest passes successfully.

  @Test
  fun generateAdapterForDataClass() {
    val result = compile(
      kotlin(
        "source.kt",
        """
          package test
          import com.squareup.moshi.JsonClass

          @JsonClass(generateAdapter = true)
          data class Person(val a: Int)
          """,
      ),
    )

    assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
  }
@Hiraev Hiraev added the bug label Jul 27, 2024
@Hiraev Hiraev changed the title KSP2 throws java.util.NoSuchElementException for data classes with @JsonAdapter(generateAdapter = true) KSP2 throws NoSuchElementException for data classes with @JsonAdapter(generateAdapter = true) Jul 27, 2024
@ZacSweers
Copy link
Collaborator

Upstream bug google/ksp#1998

@ZacSweers ZacSweers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants