I configured Arrow with KSP for my multiplatform p...
# arrow
a
I configured Arrow with KSP for my multiplatform project. I just want a simple domain commonMain module with an immutable data structure, and uses Optics to modify them. However, I can't get KSP to work correctly. It generates the code, but it misses dependencies.... See thread for details
This is the gradle conf:
Copy code
plugins {
    kotlin("multiplatform")
    id("com.google.devtools.ksp")
}

kotlin {
    js(IR) {
        browser()
    }

    jvm()

    sourceSets["commonMain"].dependencies {
        implementation(libs.uuid)
        implementation(libs.arrow.core)
        implementation(libs.arrow.optics)
    }
}

dependencies {
    add("kspCommonMainMetadata", "io.arrow-kt:arrow-optics-ksp-plugin:1.2.0-RC")
    add("kspJvm", "io.arrow-kt:arrow-optics-ksp-plugin:1.2.0-RC")
    add("kspJs", "io.arrow-kt:arrow-optics-ksp-plugin:1.2.0-RC")
}
I use the example code:
Copy code
package nl.avwie.flow.model

import arrow.optics.optics

@optics
data class Person(val name: String, val age: Int, val address: Address) {
    companion object
}

@optics
data class Address(val street: Street, val city: City) {
    companion object
}

@optics
data class Street(val name: String, val number: Int?) {
    companion object
}

@optics
data class City(val name: String, val country: String) {
    companion object
}
This generates the code like follows:
Copy code
public inline val nl.avwie.flow.model.Address.Companion.iso: arrow.optics.Iso<nl.avwie.flow.model.Address, kotlin.Pair<nl.avwie.flow.model.Street, nl.avwie.flow.model.City>> inline get() = arrow.optics.Iso(
  get = { address: nl.avwie.flow.model.Address -> kotlin.Pair(address.`street`, address.`city`) },
  reverseGet = { pair: kotlin.Pair<nl.avwie.flow.model.Street, nl.avwie.flow.model.City> -> nl.avwie.flow.model.Address(pair.first, pair.second) }
)
But I can't use them anywhere. It can not resolve this code anywhere.
Ah, found the issue, it is not me: https://github.com/google/ksp/issues/567 And you should add this in the build.gradle:
Copy code
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().all {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}

kotlin.sourceSets.commonMain {
    kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
you need to configure Gradle to take into account your generated code
a
Thanks!
However, removing
Copy code
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().all {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}
Causes the
build
to fail with:
Copy code
A problem was found with the configuration of task ':features:model:features-model-domain:compileKotlinJs' (type 'Kotlin2JsCompile').
  - Gradle detected a problem with the following location: '/Users/avanwieringen/Development/private/kotlin/flow/features/model/domain/build/generated/ksp/metadata/commonMain/kotlin'.

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.internal.execution.WorkValidationException: A problem was found with the configuration of task ':features:model:features-model-domain:compileKotlinJs' (type 'Kotlin2JsCompile').
  - Gradle detected a problem with the following location: '/Users/avanwieringen/Development/private/kotlin/flow/features/model/domain/build/generated/ksp/metadata/commonMain/kotlin'.
    
    Reason: Task ':features:model:features-model-domain:compileKotlinJs' uses this output of task ':features:model:features-model-domain:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
    Possible solutions:
      1. Declare task ':features:model:features-model-domain:kspCommonMainKotlinMetadata' as an input of ':features:model:features-model-domain:compileKotlinJs'.
      2. Declare an explicit dependency on ':features:model:features-model-domain:kspCommonMainKotlinMetadata' from ':features:model:features-model-domain:compileKotlinJs' using Task#dependsOn.
      3. Declare an explicit dependency on ':features:model:features-model-domain:kspCommonMainKotlinMetadata' from ':features:model:features-model-domain:compileKotlinJs' using Task#mustRunAfter.
    
    Please refer to <https://docs.gradle.org/8.0/userguide/validation_problems.html#implicit_dependency> for more details about this problem.
	at org.gradle.internal.execution.WorkValidationException$BuilderWithSummary.build(WorkValidationException.java:133)
197 Views