https://kotlinlang.org logo
Title
c

cah

12/27/2021, 12:00 AM
Conceptually is there any reason why I shouldn't be making HTTP requests from a Ktor-server to other rest endpoints, i.e have dependencies on both Ktor-server and Ktor-client in one project?
b

Big Chungus

12/27/2021, 12:33 AM
None, that's quite usual for microservices based architectures - a microservice is often both, the server and the client calling other microservices to fulfil requests.
👍 1
💯 1
c

cah

12/27/2021, 12:39 AM
Thanks 🙂 In which case I suspect I may have a problem with my versions
I am trying to use ktor_version = 2.0.0-beta-1 with the following in a Multiplatform project
in commonMain:
implementation("io.ktor:ktor-client-core:$ktor_version")
and in jvmMain:
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
This causes a bunch of import and general compilation failures
If anyone could spot anything obviously wrong with this setup it would be much appreciated
a

Aleksei Tirman [JB]

12/27/2021, 9:48 AM
Could you please share some errors you encounter?
c

cah

12/27/2021, 12:33 PM
Sure
Wherever I try to access call in my routing I get the following
Cannot access class 'io.ktor.util.pipeline.PipelineContext'. Check your module classpath for missing or conflicting dependencies
In my Koin modules when trying to declare singles I get:
Cannot access class 'kotlin.Pair'. Check your module classpath for missing or conflicting dependencies
Which I'm guessing means I have more than one Ktor / stdlib?
b

Big Chungus

12/27/2021, 12:35 PM
Are both modules on the same jdk and kotlin versions?
Also this is stdlib issue, not ktor
Ktor might just be pulling in multiple stdlibs, which should be a bug
c

cah

12/27/2021, 12:36 PM
Would it be helpful if I provided my entire build.gradle?
b

Big Chungus

12/27/2021, 12:36 PM
Yes
c

cah

12/27/2021, 12:37 PM
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack

plugins {
    kotlin("multiplatform") version "1.6.10"
    id("org.jetbrains.compose") version "1.0.1"
    application
}

group = "com.redacted"
version = "1.0"

val ktor_version = "2.0.0-beta-1"
val koin_version = "3.1.4"
val coroutines_version = "1.0.0"
val logback_version = "1.0.0"

repositories {
    google()
    mavenCentral()
    maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
}

kotlin {
    jvm {
        withJava()
    }
    js(IR) {
        browser()
        binaries.executable()
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                //compose
                implementation(compose.runtime)

                //ktor
                implementation("io.ktor:ktor-client-core:$ktor_version")

                //coroutines
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
            }
        }
        val commonTest by getting
        val jsMain by getting {
            dependencies {
                //compose
                implementation(compose.web.core)

                //ktor
                implementation("io.ktor:ktor-client-js:$ktor_version")
            }
        }
        val jsTest by getting {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
        val jvmMain by getting {
            dependencies {
                //koin
                implementation("io.insert-koin:koin-core:$koin_version")
                implementation("io.insert-koin:koin-ktor:$koin_version")

                //ktor
                implementation("io.ktor:ktor-client-cio:$ktor_version")
                implementation("io.ktor:ktor-client-websockets:$ktor_version")
                implementation("io.ktor:ktor-server-core:$ktor_version")
                implementation("io.ktor:ktor-server-netty:$ktor_version")

                //logback
                implementation("ch.qos.logback:logback-classic:$logback_version")
            }
        }
        val jvmTest by getting
    }
}

application {
    mainClass.set("com.redacted.server.ServerKt")
}

// include JS artifacts in any JAR we generate
tasks.getByName<Jar>("jvmJar") {
    val taskName = if (project.hasProperty("isProduction")
        || project.gradle.startParameter.taskNames.contains("installDist")
    ) {
        "jsBrowserProductionWebpack"
    } else {
        "jsBrowserDevelopmentWebpack"
    }
    val webpackTask = tasks.getByName<KotlinWebpack>(taskName)
    dependsOn(webpackTask) // make sure JS gets compiled first
    from(File(webpackTask.destinationDirectory, webpackTask.outputFileName)) // bring output file along into the JAR
}

tasks.getByName<JavaExec>("run") {
    classpath(tasks.getByName<Jar>("jvmJar")) // so that the JS artifacts generated by `jvmJar` can be found and served
}
b

Big Chungus

12/27/2021, 12:38 PM
Try removing all koin dependencies and see if that fixes the issue.
I suspect that your koin-ktor dependency expects ktor 1.x (which has entirely different plugin API surface), which is why it breaks when ktor 2.x is on the classpath instead.
c

cah

12/27/2021, 12:41 PM
Removing Koin dependencies changes the compilation error when trying to use call within routing to:
Cannot access class 'io.ktor.http.ContentType'. Check your module classpath for missing or conflicting dependencies
Cannot access class 'io.ktor.http.HttpStatusCode'. Check your module classpath for missing or conflicting dependencies
Ah okay so it could be a case of Koin not yet being compatible with ktor 2.x
b

Big Chungus

12/27/2021, 12:41 PM
See if ij suggests alternative imporrs for those. Lots of things changed with 2.x
Easiest fix for now would be to simply downgrade to ktor 1.x
👍 1
c

cah

12/27/2021, 12:42 PM
Sadly no alternative imports
b

Big Chungus

12/27/2021, 12:43 PM
I'm 99% sure koin does not support ktor 2.x anyways
👍 1
c

cah

12/27/2021, 12:43 PM
Removing the Ktor-client dependencies does resolve the issue
b

Big Chungus

12/27/2021, 12:44 PM
Odd. But to be fair, those changed the most with 2.x
c

cah

12/27/2021, 12:44 PM
Sounds like the best thing to do is wait a while and try update my versions further down the line
👍 1
Thanks for your help 🙂
a

Aleksei Tirman [JB]

12/27/2021, 12:52 PM
Unfortunately, I cannot reproduce those errors using the dependencies you described. Could you please share a sample project?
c

cah

12/27/2021, 1:45 PM
Sure
a

Aleksei Tirman [JB]

12/27/2021, 2:42 PM
Thank you. Since these errors are in the IDEA only and don't affect compilation, could you please file an issue to the Kotlin IDEA plugin issue tracker with your sample project attached?
c

cah

12/27/2021, 2:58 PM
Sure