InsertKoinIO / koin

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

Repeated module includes override overriden beans: Diamond problem #1784

Closed dant3 closed 2 months ago

dant3 commented 7 months ago

Describe the bug I was debugging my issue with being unable to override a bean from other module for quite a long time and stumbled upon a core reason being that koin always overrides bean when the same module is included multiple times. This issue is very hard to spot on a big project consisting of tens of modules!

To Reproduce Steps to reproduce the behavior:

  1. have a diamond configuration of modules, one of which does override
  2. Depending on the end order of the modules your override of bean from leaf module can be overriden back to the original definition

Expected behavior every module is processed and included exactly once

Koin module and version: koin-core:3.4.1

Snippet or Sample project to help reproduce Consider this example:

val moduleA = module {
    // bean A
}

val moduleB = module {
    includes(moduleA) // depends on module a beans

    // override bean A
}

val moduleC = module {
    includes(moduleA) // depends on module a beans
}

val topLevelModule = module {
   includes(moduleB, moduleC)
}

startKoin { modules(topLevelModule) }

In this example all modules observe original bean A from module A, because its override in moduleB is overwriten by moduleC including the module A. Changing order of include of modules moduleB and moduleC changes the outcome of resolution of bean A

stale[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.