InsertKoinIO / koin

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

flatten modules order error #1998

Open luozejiaqun opened 1 month ago

luozejiaqun commented 1 month ago

Describe the bug The order of modules after flattened is wrong.

Koin module and version: koin-core:4.0.0

Snippet:

    interface ComponentInterface1
    class Component1 : ComponentInterface1
    class Component2 : ComponentInterface1

    val m1 = module {
        single<Simple.ComponentInterface1> { Simple.Component1() }
    }
    val m2 = module {
        single<Simple.ComponentInterface1> { Simple.Component2() }
    }
    val m3 = module { includes(m1, m2) }
    val app = koinApplication {
        modules(m3)
    }

    val koin = app.koin
    val component = koin.get<Simple.ComponentInterface1>()
    assertTrue { component is Simple.Component2 } // this fails

Expected behavior This is because when m3 gets flattened, we get modules in order m3, m2, m1, the expected order is m3, m1, m2.

arnaudgiuliani commented 4 days ago

any regression over 3.5?

luozejiaqun commented 1 day ago

@arnaudgiuliani 3.5 works fine. The module flattening refactor in 4.0 introduced this issue.