koin icon indicating copy to clipboard operation
koin copied to clipboard

Repeated module includes override overriden beans: Diamond problem

Open dant3 opened this issue 1 year ago • 0 comments

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

dant3 avatar Feb 15 '24 13:02 dant3