Kotlin/kotlinx-cli

Error when using common and macos

jdiazcano opened this issue · 5 comments

I'm playing with a multiplatform project for native (macos for now and linux later) so I can create CLI utilities.

The code is pretty straightforward, there are only 3 files. build.gradle.kts, SampleCommon and system.

Build.gradle is pretty similar to the gist in #2

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithTests

plugins {
    id("org.jetbrains.kotlin.multiplatform") version "1.3.61"
    id("org.jetbrains.kotlin.plugin.serialization") version "1.3.61"
}

repositories {
    mavenCentral()
    jcenter()
    maven { url = uri("https://dl.bintray.com/kotlin/kotlin-dev") }
}

fun KotlinNativeTargetWithTests.nativeConfig() {
    binaries {
        executable {
            entryPoint = "sample.main"
        }
    }
}

kotlin {

    macosX64("macos") {
        compilations["main"].enableEndorsedLibs = true
        nativeConfig()
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common", "1.3.61"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0")
                implementation("org.jetbrains.kotlinx:kotlinx-cli:0.2.0-dev-6")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        val macosMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.14.0")
            }
        }
    }
}

Then the main file, SampleCommon:

package sample

import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration

fun main(args: Array<String>) {
    val parser = ArgParser("cdp")
    val option by parser.argument(ArgType.String).default("buildsrc")
    parser.parse(args)
    println(option)
    val json = Json(JsonConfiguration.Stable)
    val workspace = json.parse(Project.serializer(), config)
    val repository = workspace.repositories.first { it.alias == option }
    println(repository)
    system("echo \"cd \$WORKSPACE_ROOT/${repository.folder}\"")
}

expect fun system(command: String): Int

@Serializable
data class Project(
    val name: String,
    val folder: String,
    val repositories: List<Repository>
)

@Serializable
data class Repository(
    val name: String,
    val alias: String,
    val url: String,
    val folder: String
)

@Serializable
data class UserConfiguration(
    val projects: List<Project>
)

And system is just the implementation for native (and probably other implementations like java or js)

But when I try to compile, it fails on the kotlin compiler. With the following exception seen in this gist: https://gist.github.com/jdiazcano/3ffc909ec711710c352fb8a77154844f

Everything worked fine when I used it directly from the macosMain, but then I converted it to the common module and started failing.

Please, don't use compilations["main"].enableEndorsedLibs = true and exact dependency at the same time. K/N compiler in this case added klib, and it is in dependency of gradle build, you so get 2 conflicting entities. If you remove this line compilations["main"].enableEndorsedLibs = true, everything will work properly.

Or if you plan to write only for native targets, you can create some extra sourceSet which would be common for native targets and then remove implementation("org.jetbrains.kotlinx:kotlinx-cli:0.2.0-dev-6")


plugins {
    id("org.jetbrains.kotlin.multiplatform") version "1.3.61"
    id("org.jetbrains.kotlin.plugin.serialization") version "1.3.61"
}

repositories {
    mavenCentral()
    jcenter()
    maven { url = uri("https://dl.bintray.com/kotlin/kotlin-dev") }
}

fun KotlinNativeTargetWithTests.nativeConfig() {
    binaries {
        executable {
            entryPoint = "sample.main"
        }
    }
}

kotlin {

    macosX64("macos") {
        compilations["main"].enableEndorsedLibs = true
        nativeConfig()
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common", "1.3.61"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0")
            }
        }

       val nativeMain by sourceSets.creating {
            sourceSets["macosMain"].dependsOn(this)
            dependsOn(sourceSets["commonMain"])
        }

      .....
    }
}```

If i remove the explicit dependency from the commonMain neither IntelliJ or gradle will recognize the cli classes. As the endorsed libs is only done at macos level. If I add the same file in the macosMain everything works fine there.

If I add the explicit dependency and remove the endorsed lib, Gradle doesn't recognize the dependencies:

12:16:22: Executing tasks 'clean build --stacktrace'...


> Configure project :
Kotlin Multiplatform Projects are an experimental feature.

> Task :clean

> Task :compileKotlinMacos
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (6, 16): Unresolved reference: serialization
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (7, 16): Unresolved reference: serialization
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (8, 16): Unresolved reference: serialization
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (15, 16): Unresolved reference: Json
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (15, 21): Unresolved reference: JsonConfiguration
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (16, 40): Unresolved reference: serializer
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (17, 53): Unresolved reference: it
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (24, 2): Cannot access 'Serializable': it is internal in 'kotlin.io'
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (24, 2): This class does not have a constructor
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (31, 2): Cannot access 'Serializable': it is internal in 'kotlin.io'
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (31, 2): This class does not have a constructor
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (39, 2): Cannot access 'Serializable': it is internal in 'kotlin.io'
e: /Users/javier/workspace/github/nativeworkspace/src/commonMain/kotlin/sample/SampleCommon.kt: (39, 2): This class does not have a constructor

> Task :compileKotlinMacos FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlinMacos'.
> Process 'command '/Users/javier/.sdkman/candidates/java/11.0.4-amzn/bin/java'' finished with non-zero exit value 1

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

Caused by: org.gradle.process.internal.ExecException: Process 'command '/Users/javier/.sdkman/candidates/java/11.0.4-amzn/bin/java'' finished with non-zero exit value 1
	at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:396)
	at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:38)
	at org.gradle.process.internal.DefaultExecActionFactory.javaexec(DefaultExecActionFactory.java:129)
	at org.gradle.api.internal.project.DefaultProject.javaexec(DefaultProject.java:1103)
	at org.jetbrains.kotlin.compilerRunner.KonanCliRunner.run(KotlinNativeToolRunner.kt:126)
	at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinNativeCompile.compile(KotlinNativeTasks.kt:245)

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
2 actionable tasks: 2 executed
12:16:27: Tasks execution finished 'clean build --stacktrace'.

If you remove dependency, you have to add NativeMain preset and source code with parser should be there.

Please, don't use compilations["main"].enableEndorsedLibs = true and exact dependency at the same time. K/N compiler in this case added klib, and it is in dependency of gradle build, you so get 2 conflicting entities. If you remove this line compilations["main"].enableEndorsedLibs = true, everything will work properly.

This definitely worked! Thanks!