google / ksp

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

KSP does not work in KMP project with a single target #1525

Open NorbertSandor opened 1 year ago

NorbertSandor commented 1 year ago

build.gradle.kts extract:

import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization")
    id("org.jetbrains.compose")
    id("com.google.devtools.ksp")
}

kotlin {
    targetHierarchy.default()
    jvm { }
//    js(IR) {
//        browser()
//    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(projects.kotlinw.kotlinwImmutatorApi)
                implementation(compose.runtime)
                implementation(libs.kotlinx.collections.immutable)
                implementation(libs.kotlinx.datetime)
                implementation(libs.kotlinx.serialization.json)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
//        val jsMain by getting {
//            dependencies {
//            }
//        }
//        val jsTest by getting {
//            dependencies {
//                implementation(kotlin("test-js"))
//            }
//        }
        val jvmMain by getting {
            dependencies {
            }
        }
        val jvmTest by getting {
            dependencies {
                implementation(kotlin("test-junit5"))
                implementation(libs.logback.classic)
            }
        }
    }
}

compose {
    kotlinCompilerPlugin = dependencies.compiler.forKotlin("1.8.20")
    kotlinCompilerPluginArgs.add("suppressKotlinVersionCompatibilityCheck=1.9.0")
}

dependencies {
    add("kspCommonMainMetadata", projects.kotlinw.kotlinwImmutatorProcessor)
//    add("kspJs", projects.kotlinw.kotlinwRemotingProcessor)
//    add("kspJsTest", projects.kotlinw.kotlinwRemotingProcessor)
//    add("kspJvm", projects.kotlinw.kotlinwRemotingProcessor)
//    add("kspJvmTest", projects.kotlinw.kotlinwRemotingProcessor)
}

tasks.withType<KotlinCompile<*>>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}

kotlin.sourceSets.commonMain {
    kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}

The error message is:

Task with path 'kspCommonMainKotlinMetadata' not found in project

If I add the js target as well then compilation works as expected.

NorbertSandor commented 1 year ago

Maybe it is related to these issues?

whataa commented 3 months ago

I also encountered the same problem, is there any solution?

darussian commented 2 months ago

Same, this is very frustrating as it requires us to enable a target in which we don't need. That leads to obvious issues such as increased syncs, NOOP expect actuals, longer task configuration times... etc.