8000 Multiplatform example in IntelliJ IDEA: "Unresolved reference: Foo" · Issue #963 · google/ksp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Multiplatform example in IntelliJ IDEA: "Unresolved reference: Foo" #963
Open
@OliverO2

Description

@OliverO2

I have experimented with KSP's examples/multiplatform with IntelliJ IDEA 2022.1, commenting out androidNative* and mingwX64 targets in examples/multiplatform/workload/build.gradle.kts (sections kotlin and dependencies).

Being unaware of generated files, the IDE complains about examples/multiplatform/workload/src/linuxX64Main/kotlin/Main.kt:

Unresolved reference: Foo

This can be fixed by adding the following to kotlin.sourceSets:

        val commonMain by getting {
            kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
        }

However, then executing gradlew :workload:clean :workload:allTests produces Gradle warnings and compilation errors (excerpt):

> Task :workload:kspKotlinJs FAILED
e: Source file or directory not found: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin/Foo.kt
> Task :workload:kspKotlinJvm FAILED
e: Source file or directory not found: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin/Foo.kt
> Task :workload:kspCommonMainKotlinMetadata
Execution optimizations have been disabled for task ':workload:kspCommonMainKotlinMetadata' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: '/home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain'. Reason: Task ':workload:kspKotlinJs' uses this output of task ':workload:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
  - Gradle detected a problem with the following location: '/home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain'. Reason: Task ':workload:kspKotlinJvm' uses this output of task ':workload:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
  - Gradle detected a problem with the following location: '/home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin'. Reason: Task ':workload:kspKotlinJs' uses this output of task ':workload:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
  - Gradle detected a problem with the following location: '/home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin'. Reason: Task ':workload:kspKotlinJvm' uses this output of task ':workload:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
w: [ksp] [Bar.kt, Baz.kt]
w: [ksp] [Bar.kt, Baz.kt, Foo.kt]
> Task :workload:kspKotlinLinuxX64
Execution optimizations have been disabled for task ':workload:kspKotlinLinuxX64' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: '/home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin'. Reason: Task ':workload:kspKotlinLinuxX64' uses this output of task ':workload:kspCommonMainKotlinMetadata' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
w: [ksp] [Bar.kt, Baz.kt, Main.kt]
w: [ksp] [Bar.kt, Baz.kt, Main.kt, Foo.kt]
FAILURE: Build completed with 2 failures.

NOTE: Trying to fix the Gradle warnings with the idea plugin's module configuration as suggested in KSP quickstart appears to have no effect with multiplatform.

The Gradle warnings can be fixed by adding the required dependencies:

afterEvaluate {  // WORKAROUND: both register() and named() fail – https://github.com/gradle/gradle/issues/9331
    tasks {
        withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>> {
            if (name != "kspCommonMainKotlinMetadata")
                dependsOn("kspCommonMainKotlinMetadata")
        }
    }
}

Now, executing gradlew :workload:clean :workload:allTests proceeds without the Gradle warnings, but still exhibits compilation errors (excerpt):

> Task :workload:compileKotlinLinuxX64 FAILED
e: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/linuxX64/linuxX64Main/kotlin/Foo.kt: (3, 7): Redeclaration: Foo
e: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin/Foo.kt: (3, 7): Redeclaration: Foo
> Task :workload:compileKotlinJs FAILED
e: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/js/jsMain/kotlin/Foo.kt: (3, 7): Redeclaration: Foo
e: /home/oliver/Repositories/experimental.nobackup/ksp/examples/multiplatform/workload/build/generated/ksp/metadata/commonMain/kotlin/Foo.kt: (3, 7): Redeclaration: Foo
FAILURE: Build completed with 2 failures.

This can be fixed by making KSP stop generating the same class multiple times (in commonMain as well as per target), reducing the dependencies section to:

dependencies {
    add("kspCommonMainMetadata", project(":test-processor"))
}

Now gradlew :workload:clean :workload:allTests completes successfully. And IntelliJ IDEA is happy as well.

Could this be integrated into the example and documentation be added to KSP with Kotlin Multiplatform?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0