Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-modules failing #1941

Open
kmbisset89 opened this issue Aug 10, 2024 · 2 comments
Open

Sub-modules failing #1941

kmbisset89 opened this issue Aug 10, 2024 · 2 comments

Comments

@kmbisset89
Copy link

Getting an error when using submodules that throws an exception.

To Reproduce
Steps to reproduce the behavior:
Use the includes keyword:
val commonModule = module {
includes(infrastructureModule, viewModels, useCases, repositories)
}

private val infrastructureModule = module {
single { CoroutineErrorCatcher(get()) }

single { DispatcherProvider(get<CoroutineErrorCatcher>().coroutineExceptionHandler) }

}

private val viewModels = module {

}

private val useCases = module {

}

private val repositories = module {

}

                                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List org.koin.core.module.Module.getIncludedModules()' on a null object reference

Expected behavior
Not throw exception

Koin module and version:
[e.g]: koin-core:4.0.0-RC1

@GodzerBoot
Copy link

Same issue

Not using includes() and writing all modules in the same file solved the problem, but... It's quite dirty

@kmbisset89
Copy link
Author

@arnaudgiuliani

@OptIn(KoinInternalApi::class)
fun flatten(modules: List<Module>): Set<Module> {
    // This is actually a DFS traversal of the module graph,
    // but we're using a stack instead of recursion to avoid stack overflows and performance overhead.

    val flatten = linkedSetOf<Module>()
    // I added a filter not null to get ride of this even though it should not be possible to have a null in that list. 
    val stack = ArrayDeque(modules.asReversed().filterNotNull())

    while (stack.isNotEmpty()) {

       /* The problem is here some how it is getting a null module */

        val current = stack.removeLast()

        // If the module is already in the set, that means we've already visited it, so we can skip it.
        if (!flatten.add(current)) {
            continue
        }

        // Add all the included modules to the stack if they haven't been visited yet.
        for (module in current.includedModules) {
            if (module !in flatten) {
                stack += module
            }
        }
    }

    return flatten
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants