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

startKoin crashes on iOS on non empty modules parameter list #1985

Closed chokokatana closed 2 months ago

chokokatana commented 2 months ago

Describe the bug I have a compose multiplatform app that I'm trying to add Koin to. Started by creating a database module and testing on Android, everything works. However, I get strange crashes on iOS side trying to initialise Koin.

To Reproduce Steps to reproduce the behavior:

Add to sourceSets commonMain.dependencies with koin_bom being 4.0.0:

        implementation(project.dependencies.platform("io.insert-koin:koin-bom:$koin_bom"))
        implementation("io.insert-koin:koin-core")
        implementation("io.insert-koin:koin-compose")

Add to the iOS specific platform the following code:

val testModule = module {
}

fun initKmpKoin() {
    startKoin {
        modules(testModule)
    }
}

Then sometime in a swift UIViewController call IosKoinKmpHelperKt.doInitKmpKoin(). This crashes with:

E: [IosExceptions] KMP Unhandled: kotlin.native.internal.IrLinkageError: Can not get instance of singleton 'Companion': No class found for symbol 'kotlin.uuid/Uuid.Companion|null[0]'
Uncaught Kotlin exception: kotlin.native.internal.IrLinkageError: Can not get instance of singleton 'Companion': No class found for symbol 'kotlin.uuid/Uuid.Companion|null[0]'
    at 0   KmmLocations                        0x108f4107b        kfun:kotlin.Throwable#<init>(kotlin.String?){} + 119 (/opt/buildAgent/work/b2e1db4d8d903ca4/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:28:44)
    at 1   KmmLocations                        0x108f3a447        kfun:kotlin.Error#<init>(kotlin.String?){} + 115 (/opt/buildAgent/work/b2e1db4d8d903ca4/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:12:51)
    at 2   KmmLocations                        0x108f7851b        kfun:kotlin.native.internal.IrLinkageError#<init>(kotlin.String?){} + 115 (/opt/buildAgent/work/b2e1db4d8d903ca4/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt:162:51)
    at 3   KmmLocations                        0x108f785d3        kfun:kotlin.native.internal#ThrowIrLinkageError(kotlin.String?){}kotlin.Nothing + 163 (/opt/buildAgent/work/b2e1db4d8d903ca4/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt:166:11)
    at 4   KmmLocations                        0x109ebd843        kfun:org.koin.mp#generateId__at__org.koin.mp.KoinPlatformTools(){}kotlin.String + 63 (/Users/runner/work/koin/koin/projects/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTools.kt:39:47)
    at 5   KmmLocations                        0x109eae1d3        kfun:org.koin.core.module.Module#<init>(kotlin.Boolean){} + 231 (/Users/runner/work/koin/koin/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt:42:5)
    at 6   KmmLocations                        0x109ebcdef        kfun:org.koin.dsl#module(kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>){}org.koin.core.module.Module + 179 (/Users/runner/work/koin/koin/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/ModuleDSL.kt:35:18)
    at 7   KmmLocations                        0x109ebcef7        kfun:org.koin.dsl#module$default(kotlin.Boolean;kotlin.Function1<org.koin.core.module.Module,kotlin.Unit>;kotlin.Int){}org.koin.core.module.Module + 155 (/Users/runner/work/koin/koin/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/ModuleDSL.kt:33:1)
    at 8   KmmLocations                        0x108f11303        kfun:/Users/gradha/project/geodb/wallace-kmm-locations/KmmLocations/src/iosMain/kotlin/com/geodb/wallace/kmm/utils/IosKoinKmpHelper.kt.com.geodb.wallace.kmm.utils#$init_global(){} + 107 (/Users/gradha/project/geodb/wallace-kmm-locations/KmmLocations/src/iosMain/kotlin/com/geodb/wallace/kmm/utils/IosKoinKmpHelper.kt:1:1)
    at 9   KmmLocations                        0x1090fc9df        CallInitGlobalPossiblyLock + 539 
    at 10  KmmLocations                        0x108f1143f        kfun:com.geodb.wallace.kmm.utils#initKmpKoin(){} + 127 (/Users/gradha/project/geodb/wallace-kmm-locations/KmmLocations/src/iosMain/kotlin/com/geodb/wallace/kmm/utils/IosKoinKmpHelper.kt:23:1)
    at 11  KmmLocations                        0x107d40acb        objc2kotlin_kfun:com.geodb.wallace.kmm.utils#initKmpKoin(){} + 95 
    at 12  iosApp                              0x100caf2df        $s6iosApp18MainViewControllerC5coderACSgSo7NSCoderC_tcfc + 319 (/Users/gradha/project/geodb/wallace-kmm-locations/iosApp/iosApp/MainView.swift:43:28)
    at 13  iosApp                              0x100cb0d5f        $s6iosApp18MainViewControllerC5coderACSgSo7NSCoderC_tcfcTo + 39 
    at 14  UIKitCore                           0x18498b2b7        -[UIClassSwapper initWithCoder:] + 2175 
…

If instead of calling modules(testModule) I call modules(listOf()) then nothing crashes, but that's not very useful.

Expected behavior No crash.

Koin module and version: 4.0.0

chokokatana commented 2 months ago

Oh, found https://github.com/InsertKoinIO/koin/issues/1964. Indeed, if I downgrade from 4.0.0 to 4.0.0-RC1 then everything works. Welp, quite a compatibility bummer.