https://kotlinlang.org logo
#ktor
Title
# ktor
z

zeugederunity

04/15/2022, 6:24 AM
Hey everyone, I'm using ktor 2.0.0 in a Kotlin-1.6.20 MPP subproject. I have a strange error where the JVM-ktor-dependencies do not only load the normal library versions like ktor-server-core:2.0.0 but also the ktor-server-core: jvmAndNixMain:2.0.0. Ktor is the only library causing this issue, so I think I either found a bug or did some misstake somewhere in my gradle buldscript. My build.gradle.kts looks basically like this:
Copy code
kotlin {
    val ktorVersion: String by project /* 2.0.0 */
    jvm { /* some blabla */ }
    js { /* some blabla */ }
    sourceSet {
        val commonMain by getting { /* some blabla */ }
        val jsMain by getting { /* some blabla */ }
        val jvmMain by getting {
            dependencies {
                implementation("io.ktor:ktor-server-netty:$ktorVersion")
                implementation("io.ktor:ktor-server-status-pages:$ktorVersion")
                /*And some more */
            }
        }
        val jvmTest by getting {
            dependencies {
                implementation("io.ktor:ktor-server-test-host:$ktorVersion")
            }
        }
    }
}
The external libraries look like this:
Copy code
/*ktor-server-auth has no jvmAndNixMain*/
Gradle: io.ktor:ktor-server-auth-jvm:2.0.0
Gradle: io.ktor:ktor-server-auth-jwt-jvm:2.0.0
Gradle: io.ktor:ktor-server-auto-head-response:jvmAndNixMain:2.0.0
Gradle: io.ktor:ktor-server-auto-head-respone:2.0.0

/*Some more libs with the each the corresponding jvmAndNixMain */

Gradle: io.ktor:ktor-server-core-jvm:jvmAndNixMain:2.0.0
Gradle: io.ktor:ktor-server-core-jvm:2.0.0
Is this a bug or does anyone know what I did wrong? Thank you for your help in advance!
a

Aleksei Tirman [JB]

04/15/2022, 8:51 AM
Since 2.0.0 Ktor, supports a server on the native target. Could you please describe what problem causes
jvmAndNixMain
dependencies?
h

hfhbd

04/15/2022, 9:44 AM
There is no
jvmAndNix
artifact 🤔 https://repo.maven.apache.org/maven2/io/ktor/
a

Aleksei Tirman [JB]

04/15/2022, 9:53 AM
I see it inside this JAR.
h

hfhbd

04/15/2022, 9:58 AM
Hm, but it should not be required to use the same name from the library when consuming the lib. Instead, Gradle should only compare the modules by its target platform. What happens, if I choose the name
nixAndJvmMain
? It should work too.
z

zeugederunity

04/16/2022, 9:52 AM
@Aleksei Tirman [JB] @hfhbd This one will be a little bit longer, so I'll also attach it as "message.txt". But before I start with my message: Thank you for your help and interrest! The problem these nix-libs cause show themselves in various ways, here are some of the errors caused by it: 1. Looking at your code in IntelliJ, the IDE shows shows an error, that it can not resolve the classes of ktor. 2. When gradle tries to build the application with this nixAndJvmMain it either fails to resolve the classes or I get a "Out of heapspace"-exception. 3. When I run unit tests they start to run just fine, but when it tries to call some code from from "io.ktorktor server test hostjvmAndNixMain:2.0.0" I get an exception saying "Function in module not found". As far as I can say, all these errors are somehow related to ktor and its nix-libraries. I'll go into further details with the following examples: ===== Example 1 ===== My Code:
Copy code
kotlin
fun initializeServer(applicationConfig: ApplicationConfig){
  require(!initialized) {
    "The server is already initialized!"
  }
  val config = applicationConfig.getFSConfig()

  try {
    config.config("fs")
  } catch (e: ApplicationConfigurationException) {
    logger.error(e) { "Tried to load a not-fs config!" }
    exitProcess(WRONG_CONFIG)
  }

  //<...>
}
What IntelliJ does: IntelliJ underlines the
e: ApplicationConfigurationException
and shows the following errors and documentations: Error:
Copy code
Type mismatch.
Required: Throwable
Found:    ApplicationConfigurationException
Documentation:
Copy code
public final class ApplicationConfigurationException
Thrown when an application is misconfigured
  io.ktor.server.config   0_config.knm 
  Gradle: io.ktor:ktor-server-core:jvmAndNixMain:2.0.0 (io.ktor-ktor-server-core-jvmAndNixMain.klib)
The path to the source file, when I jump to the declaration of ApplicationConfigException via IntelliJ is
C:\Users\felix\.gradle\caches\modules-2\files-2.1\io.ktor\ktor-server-core\2.0.0\7064a323594c9b548ea52254325d1013f02da942\ktor-server-core-2.0.0-sources.jar!\jvmAndNixMain\io\ktor\server\config\ApplicationConfig.kt
. (This path does not exist in the filesystem.) ===== Example 2 ===== My Code:
Copy code
kotlin
val testApplication = TestApplication {
  environment {
    config = loadTestConfig("test_application.conf")
  }
  application {
    environment.monitor.subscribe(FSInitialized) {
      if (rootUser !in it.userManager)
        it.userManager.put(rootUser)
      if (adminUser !in it.userManager)
        it.userManager.put(adminUser)
      if (normalUser !in it.userManager)
        it.userManager.put(normalUser)
    }
    fsServer.apply { main() }
  }
}
The IDE marks
environment.monitor.<...>
as error with the message
Copy code
Cannot
access 'environment': it is internal in 'TestApplicationBuilder'
. The documentation of
TestApplication
says:
Copy code
@[ERROR : io.ktor.util.KtorDsl]
public fun TestApplication(
  block: TestApplicationBuilder.() -> Unit
): TestApplication
Creates an instance of TestApplication configured with the builder block. Make sure to call TestApplication.stop after your tests.
See Also:
testApplication
  io.ktor.server.testing   1_testing.knm 
  Gradle: io.ktor:ktor-server-test-host:jvmAndNixMain:2.0.0 (io.ktor-ktor-server-test-host-jvmAndNixMain.klib)
The documentation of
application
says:
Copy code
@[ERROR : io.ktor.util.KtorDsl]
public final fun application(
  block: [ERROR : io.ktor.server.application.Application].() -> Unit
): Unit
Adds a module to TestApplication.
See Also:
testApplication
  io.ktor.server.testing.TestApplicationBuilder 
  Gradle: io.ktor:ktor-server-test-host:jvmAndNixMain:2.0.0 (io.ktor-ktor-server-test-host-jvmAndNixMain.klib)
The declarations in the IDE are also pointing to some invalid paths. ===== Example 3 ===== Regardless to the errors in "Example 2" I can compile and run the Unit-Tests. But then the code throws the following exception, when it reaches code from any ktor package with a jvmAndNixMain-twin it throws a massive amount of exceptions. (see example3_stacktrace.txt)
Copy code
Module function cannot be found for the fully qualified name 'de.fs.web.PluginsKt.plugins'
io.ktor.server.engine.internal.ReloadingException: Module function cannot be found for the fully qualified name 'de.fs.web.PluginsKt.plugins'
	at app//io.ktor.server.engine.internal.CallableUtilsKt.executeModuleFunction(CallableUtils.kt:27)
	...
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:46)
	at de.fs.web.BasicTests$testBasicConfigDownload$1$invoke$lambda-1$$inlined$withConfiguredTestApplication$1.invokeSuspend(BasicTests.kt:681)
Caused by: io.ktor.server.engine.internal.ReloadingException: Module function cannot be found for the fully qualified name 'de.fs.web.PluginsKt.plugins'
	at app//io.ktor.server.engine.internal.CallableUtilsKt.executeModuleFunction(CallableUtils.kt:27)
	...
	at app//kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
===== Example 4 ===== When I check "Project -> External Libraries", the nixAndJvmMain-libs are loaded from a different path than the other, normal working libs: Normal:
Copy code
Gradle: io.ktor:ktor-server-core-jvm:2.0.0
Classes:
    C:\Users\felix\.gradle\caches\modules-2\files-2.1\io.ktor\ktor-server-core-jvm\2.0.0\2bf374bfde9bd8e96f7b3ed3ff85e3ccf11aa8b0\ktor-server-core-jvm-2.0.0.jar
Sources:
    C:\Users\felix\.gradle\caches\modules-2\files-2.1\io.ktor\ktor-server-core-jvm\2.0.0\24cf9fccb8a1b7cff506364391b7263607880689\ktor-server-core-jvm-2.0.0.jar
Nix:
Copy code
Gradle: io.ktor:ktor-server-core:jvmAndNixMain:2.0.0
Classes:
    D:\git\firmensuche\.gradle\kotlin\sourceSetMetadata\search.firmensuche\jvmMain\implementation\io.ktor-ktor-server-core

Sources:
    C:\Users\felix\.gradle\caches\modules-2\files-2.1\io.ktor\ktor-server-core-jvm\2.0.0\7064a323594c9b548ea52254325d1013f02da942\ktor-server-core-2.0.0-sources.jar
Like in "Example 1", the sources-path of the io.ktorktor server corejvmAndNixMain:2.0.0 doesn't exist. So my guesses right now are: A) The library is not correctly rersolved in IntelliJ/gradle and erroneously loaded as dependency. B) The jvmAndNixMain should not be loaded as library at all and the MPP-Plugin has some serious issues with either modules and (only) ktor. Right now we have a public holiday in germany, so I won't be able to respond do any of your questions until tuesday. (Sorry for that!)
a

Aleksei Tirman [JB]

04/18/2022, 9:09 AM
@zeugederunity could you please file an issue with a sample project attached to reproduce those problems?
👍 1
z

zeugederunity

04/19/2022, 9:13 AM
@Aleksei Tirman [JB] Add the described code-examples from above or only the gradle configuration?
a

Aleksei Tirman [JB]

04/19/2022, 9:31 AM
@zeugederunity it would be great if you add a complete project.
z

zeugederunity

04/19/2022, 10:20 AM
@Aleksei Tirman [JB] Is this minimal example sufficient? https://github.com/FelixEngl/ktor-gradle-problem-me
@Aleksei Tirman [JB] Created an Issue: https://youtrack.jetbrains.com/issue/KTOR-4188/Dependency-resolution-problem:-Gradle-+-MPP-+-ktor If you need more informations you can ask me directly via DM on slack. Thank you for your time and patience!
🙏 1
a

Aleksei Tirman [JB]

04/19/2022, 11:58 AM
Seems like all the described problems are IDE related. The only compilation error you have is
Test.kt: (36, 34): Unresolved reference: enviroment
because you mistyped the “enviro~*n*~ment” property. Is that correct?
53 Views