Conceptually is there any reason why I shouldn't b...
# ktor
c
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
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
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:
Copy code
implementation("io.ktor:ktor-client-core:$ktor_version")
and in jvmMain:
Copy code
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
Could you please share some errors you encounter?
c
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
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
Would it be helpful if I provided my entire build.gradle?
b
Yes
c
Copy code
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
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
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
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
Sadly no alternative imports
b
I'm 99% sure koin does not support ktor 2.x anyways
๐Ÿ‘ 1
c
Removing the Ktor-client dependencies does resolve the issue
b
Odd. But to be fair, those changed the most with 2.x
c
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
Unfortunately, I cannot reproduce those errors using the dependencies you described. Could you please share a sample project?
c
Sure
sample-error.zip
a
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
Sure