https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

Carter

10/10/2023, 4:03 PM
I’ve got a weird compile failure with Kotlin native with Kotlin 1.9.10. I’ve got a small Gradle module with an expect/actual helper function using posix to read lines from a file on native platforms, with a different implementation on JVM. This has been working for several years. When I try to bump kotlinx-datetime from 0.4.0 to 0.4.1, this now fails to compile with issues on the imports on a Linux CI server but still builds successfully locally on my Mac. I’ve tried ./gradlew clean and using --rerun-tasks, but I can’t reproduce except on Linux CI. I think the datetime version bump is a red herring but I can’t figure out why this suddenly started failing. These are the imports it is failing on:
Copy code
import platform.posix.fclose
import platform.posix.fgets
import platform.posix.fopen
Ideas?
a

Adam S

10/11/2023, 3:48 AM
Maybe there's something wrong with the targets or source sets. What's the full list of Kotlin targets your library uses? Does it match those supported by kotlinx-datetime? Have you defined custom source sets, like a nativeMain that extends commonMain and is extended by all native targets?
MacOS supports building all Kotlin targets, while Linux and windows are more limited, so that's why I suspect it's an issue with targets.
c

Carter

10/11/2023, 1:48 PM
If I entirely remove the datetime dependency, the issue persists so it seems like datetime was weirdly suppressing some kind of issue.
I think the Gradle configuration is about as basic as it can get.
Copy code
kotlin {
    jvm()
    sourceSets {
        getByName("commonMain") {
            dependencies {
                api(libs.kotlinx.datetime)
                implementation(libs.kotlinx.coroutines.core)
                implementation(kotlin("test"))
            }
        }
        getByName("commonTest") {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        getByName("jvmMain") {
            dependencies {
                implementation(libs.kotlinx.coroutines.test)
                implementation(kotlin("test"))
            }
        }
        getByName("jvmTest") {
            dependencies {
                implementation(kotlin("test"))
            }
        }

        if (project.properties["isNativeEnabled"].toString().toBoolean()) {
            getByName("nativeMain") {
            }
            getByName("nativeTest") {
                dependencies {
                    implementation(kotlin("test"))
                }
            }
        }
    }
}
Along with a convention plugin
Copy code
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
    extensions.findByType<org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension>()?.apply {
        targetHierarchy.default()
        val isNativeEnabled = project.properties["isNativeEnabled"].toString().toBoolean()

        if (isNativeEnabled) {
            macosX64()
            macosArm64()
            mingwX64()
            linuxX64()
        }
}
Following up with my message from a few days ago, there seems to be a bug with Gradle configuration cache and Kotlin native. This fails with the Kotlin 1.9.20-RC as well, which is supposed to have full configuration cache support. When config cache is enabled, any
import platform.posix
fails to build.