InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.76k stars 695 forks source link

[3.6.0-Beta4] install(Koin) error - Koin can't be run #1882

Closed nowiwr01w closed 1 month ago

nowiwr01w commented 1 month ago

Describe the bug I set Koin like this:

internal fun Application.configureKoin() {
    install(Koin) {
        slf4jLogger(level = Level.DEBUG)
        modules(serverModules)
    }
}

When I run a Server, I get an error:

Exception in thread "main" java.lang.NoSuchMethodError: 'io.ktor.events.Events io.ktor.server.application.ApplicationEnvironment.getMonitor()'
    at org.koin.ktor.plugin.KoinPluginKt.setupMonitoring(KoinPlugin.kt:57)
    at org.koin.ktor.plugin.KoinPluginKt$Koin$2.invoke(KoinPlugin.kt:41)
    at org.koin.ktor.plugin.KoinPluginKt$Koin$2.invoke(KoinPlugin.kt:37)

If you go to KoinPlugin.kt at 57 like and check monitor variable type in this function:

internal fun PluginBuilder<KoinApplication>.setupMonitoring(koinApplication: KoinApplication) {
    val monitor = environment?.monitor
    monitor?.raise(KoinApplicationStarted, koinApplication)
    monitor?.subscribe(ApplicationStopping) {
        monitor.raise(KoinApplicationStopPreparing, koinApplication)
        koinApplication.koin.close()
        monitor.raise(KoinApplicationStopped, koinApplication)
    }
}

it says

val monitor: [Error type: Not found recorded type for environment?.monitor]

Koin module and version: Versions:

koin = "3.6.0-Beta4"
ktor = "3.0.0-wasm2"
ktor-plugin = "3.0.0-beta-1"
logback = "1.5.3"

Libraries:

koin-ktor = { module = "io.insert-koin:koin-ktor", version.ref = "koin" }
koin-ktor-logger = { module = "io.insert-koin:koin-logger-slf4j", version.ref = "koin" }

ktor = { id = "io.ktor.plugin", version.ref = "ktor-plugin" }
ktor-server-core = { module = "io.ktor:ktor-server-core", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }

logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }

Code example:

 plugins {
    alias(libs.plugins.ktor)
    alias(libs.plugins.kotlinJvm)
    alias(libs.plugins.kotlinSerialization)
}

dependencies {
    implementation(libs.logback)
    implementation(libs.koin.ktor)
    implementation(libs.koin.ktor.logger)
    implementation(libs.ktor.server.core)
    implementation(libs.ktor.server.netty)
}
package com.example.test_server

import io.ktor.server.application.install
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import org.koin.dsl.module
import org.koin.ktor.plugin.Koin
import org.koin.logger.slf4jLogger

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(Koin) {
            slf4jLogger()
            modules(serverModule)
        }
    }.start(wait = true)
}

val serverModule = module {
    single { MyService() }
}

class MyService

Fails with

 17:30:46.034 [main] INFO io.ktor.server.Application -- Autoreload is disabled because the development mode is off.
17:30:46.043 [main] INFO [Koin] -- Started 1 definitions in 0.875167 ms
Exception in thread "main" java.lang.NoSuchMethodError: 'io.ktor.events.Events io.ktor.server.application.ApplicationEnvironment.getMonitor()'
    at org.koin.ktor.plugin.KoinPluginKt.setupMonitoring(KoinPlugin.kt:57)
    at org.koin.ktor.plugin.KoinPluginKt$Koin$2.invoke(KoinPlugin.kt:41)
    at org.koin.ktor.plugin.KoinPluginKt$Koin$2.invoke(KoinPlugin.kt:37)
    at io.ktor.server.application.CreatePluginUtilsKt.setupPlugin(CreatePluginUtils.kt:300)
    at io.ktor.server.application.CreatePluginUtilsKt.createPluginInstance(CreatePluginUtils.kt:269)
    at io.ktor.server.application.CreatePluginUtilsKt.access$createPluginInstance(CreatePluginUtils.kt:1)
    at io.ktor.server.application.CreatePluginUtilsKt$createApplicationPlugin$2.install(CreatePluginUtils.kt:90)
    at io.ktor.server.application.CreatePluginUtilsKt$createApplicationPlugin$2.install(CreatePluginUtils.kt:83)
    at io.ktor.server.application.ApplicationPluginKt.install(ApplicationPlugin.kt:100)
    at com.example.test_server.MyClassKt.main$lambda$1(MyClass.kt:12)
    at io.ktor.server.engine.EmbeddedServer$instantiateAndConfigureApplication$1.invoke(EmbeddedServerJvm.kt:338)
    at io.ktor.server.engine.EmbeddedServer$instantiateAndConfigureApplication$1.invoke(EmbeddedServerJvm.kt:327)
    at io.ktor.server.engine.EmbeddedServer.avoidingDoubleStartup(EmbeddedServerJvm.kt:355)
    at io.ktor.server.engine.EmbeddedServer.instantiateAndConfigureApplication(EmbeddedServerJvm.kt:327)
    at io.ktor.server.engine.EmbeddedServer.createApplication(EmbeddedServerJvm.kt:142)
    at io.ktor.server.engine.EmbeddedServer.start(EmbeddedServerJvm.kt:270)
    at io.ktor.server.engine.EmbeddedServer.start$default(EmbeddedServerJvm.kt:267)
    at com.example.test_server.MyClassKt.main(MyClass.kt:16)
    at com.example.test_server.MyClassKt.main(MyClass.kt)
nowiwr01w commented 1 month ago

Also fails with 3.6.0-Beta3, 3.6.0-Beta2, 3.6.0-alpha3, 3.6.0-wasm-alpha2, 3.6.0-wasm-alpha1

nowiwr01w commented 1 month ago

Found out it fails with any Koin version, so I guess it's because Koin doesn't support Ktor 3+ now

When I changed to

ktor = "2.3.11"
ktor-plugin = "2.3.11"

it fixed

@arnaudgiuliani If it's possible can you please provide in release notes what max version of Ktor is supported by Koin?