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

Kotlin Multiplatform builds result in "Error class: Unknown class" #50

Closed
EdricChan03 opened this issue Apr 8, 2023 · 5 comments · Fixed by #52
Closed

Kotlin Multiplatform builds result in "Error class: Unknown class" #50

EdricChan03 opened this issue Apr 8, 2023 · 5 comments · Fixed by #52
Assignees
Labels
affects:kotlin-multiplatform Affects Kotlin/Multiplatform projects bug Something isn't working

Comments

@EdricChan03
Copy link
Contributor

Seems that Kotlin Multiplatform builds result in an issue similar to #44:

Screenshot of Dokka website with an Error class note

plugins {
    kotlin("multiplatform") // Using Kotlin 1.8.20
    dev.adamko.`dokkatoo-html` // Using v1.1.1
}

kotlin {
    jvm {
        compilations.all {
            compilerOptions.configure {
                jvmTarget = JvmTarget.JVM_1_8
            }
        }
        withJava()
        testRuns["test"].executionTask.configure {
            useJUnitPlatform()
        }
    }
    js(IR) {
        browser {
            commonWebpackConfig {
                cssSupport {
                    enabled = true
                }
            }
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(libs.ktor.client.core)
                implementation(libs.kotlinx.serialization.json)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val jvmMain by getting
        val jvmTest by getting
        val jsMain by getting
        val jsTest by getting

        val samples by creating {
            dependsOn(commonMain)
        }
    }
}

dokkatoo {
    dokkatooSourceSets.configureEach {
        if (name.endsWith("Test") || name == "samples") suppress = true // Hide test/samples sources

        includes.from("Module.md")

        samples.from("src/samples/kotlin")

        // Link to source
        sourceLink {
            localDirectory = file("src/${name}/kotlin")
            remoteUrl =
                URL("https://github.com/EdricChan03/<repo>/tree/main/${project.name}/src/${name}/kotlin")
        }

        externalDocumentationLinks {
            // KotlinX docs
            val `kotlinx-coroutines` by creating {
                url("https://kotlinlang.org/api/kotlinx.coroutines/")
            }
            val `kotlinx-serialization` by creating {
                url("https://kotlinlang.org/api/kotlinx.serialization/")
            }

            // Ktor API docs
            val ktor by creating {
                url("https://api.ktor.io/ktor-client/")
                packageListUrl("https://api.ktor.io/package-list")
            }
        }
    }
}

Let me know if additional information/a reproducer is required

@aSemy
Copy link
Contributor

aSemy commented Apr 9, 2023

Thanks, I've taken a look and I think that Dokkatoo that this error is just one symptom.

DokkatooKotlinAdapter is supposed to automatically convert any Kotlin Multiplatform source sets to an equivalent Dokka source set, but when I look in ./build/dokka-config/html/dokka_parameters.json the platform type for all source sets is common, while it should be jvm or js etc. The classpath doesn't seem to be shared across the source sets either.

I'll try to refactor DokkatooKotlinAdapter but I found it extremely difficult to do workaround Gradle's Configuration Cache restrictions.

For now, you can try and manually construct the source sets. I've not done this before, and there's no example, so it will need some experimentation. Here's a start:

dokkatoo {

  dokkatooSourceSets.clear() // clear the bugged source sets generated by Dokkatoo

  dokkatooSourceSets.create("common") {
    displayName.set("Common")
    analysisPlatform.set(KotlinPlatform.Common)
    sourceRoots.from("src/commonMain/kotlin")
    val dokkatooCommonClasspath by configurations.registering {
      isCanBeResolved = true
      isCanBeConsumed = false
      extendsFrom(
        configurations.getByName("commonMainImplementation"),
        configurations.getByName("commonMainCompileOnly"),
        configurations.getByName("commonMainRuntimeOnly"),
        configurations.getByName("commonMainApi"),
      )
    }

    classpath.from(dokkatooCommonClasspath.map { it.incoming.artifacts.artifactFiles })
  }

  dokkatooSourceSets.create("jvm") {
    displayName.set("JVM")
    analysisPlatform.set(KotlinPlatform.JVM)
    sourceRoots.from("src/jvmMain/kotlin")

    val dokkatooJvmClasspath by configurations.registering {
      isCanBeResolved = true
      isCanBeConsumed = false
      extendsFrom(
        configurations.getByName("jvmCompileClasspath"),
        configurations.getByName("jvmRuntimeClasspath"),
      )
    }

    classpath.from(dokkatooJvmClasspath.map { it.incoming.artifacts.artifactFiles })
  }
}

@aSemy aSemy self-assigned this Apr 13, 2023
@aSemy aSemy added bug Something isn't working affects:kotlin-multiplatform Affects Kotlin/Multiplatform projects labels Apr 13, 2023
@aSemy aSemy closed this as completed in #52 Apr 16, 2023
@aSemy aSemy reopened this Apr 16, 2023
@aSemy
Copy link
Contributor

aSemy commented Apr 16, 2023

@EdricChan03 I've refactored the Kotlin Multiplatform configuration, and added some tests to verify that this problem doesn't occur in the same projects. Please could you check if 1.2.0-SNAPSHOT resolves the issue? If you could also check #51 at the same time, it would be much appreciated!

@EdricChan03
Copy link
Contributor Author

I can confirm the sources seem to work correctly:

Screenshot of updated generated Dokka docs, with sources correctly added

@chrisbanes
Copy link

chrisbanes commented Apr 20, 2023

I'm currently playing around with migrating to Dokkatoo in cashapp/redwood but we're seeing the same issue with 1.2.0. My WIP branch is here: cashapp/redwood#936

@aSemy
Copy link
Contributor

aSemy commented Apr 20, 2023

I'm currently playing around with migrating to Dokkatoo in cashapp/redwood but we're seeing the same issue with 1.2.0. My WIP branch is here: cashapp/redwood#936

hi @chrisbanes, I can take a look at the branch. Can you open a new issue please? Ideally with a small reproducer.

At first glance I can see that the project is using the legacy method of applying plugins, which might mean that Dokkatoo can't automatically pick up the classpath of a module (which is performed in DokkatooKotlinAdapter).

You can look in the generated my-subproject/build/dokka-config/html/dokka_parameters.json file in each subproject to see what classpath has been picked up.

For a quick-fix you could try the workaround mentioned in #44 (comment) and manually add files to the classpath

// build.gradle.kts

dokkatoo {
  dokkatooSourceSets.configureEach {
    // workaround for https://github.com/adamko-dev/dokkatoo/issues/44
    classpath.from(configurations.compileClasspath.map { it.incoming.artifactView { lenient(true) }.artifacts.artifactFiles })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects:kotlin-multiplatform Affects Kotlin/Multiplatform projects bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants