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

Wasm throws exceptions using koin on IPv4 [_this.randomUUID is not a function] #2024

Closed crowforkotlin closed 4 days ago

crowforkotlin commented 1 month ago
ERROR
_this.randomUUID is not a function
TypeError: _this.randomUUID is not a function
    at kotlin.uuid.randomUUID_$external_fun (webpack-internal:///./kotlin/ComposeApp.uninstantiated.mjs:182:69)
    at <MordecaiX:composeApp>.kotlin.uuid.randomUUID_$external_fun__externalAdapter (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[13386]:0x48c6ec)
    at <MordecaiX:composeApp>.kotlin.uuid.secureRandomUuid (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[13384]:0x48c6bf)
    at <MordecaiX:composeApp>.kotlin.uuid.Companion.random (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[11818]:0x47d234)
    at <MordecaiX:composeApp>.org.koin.mp.generateId (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[63113]:0x8d55a6)
    at <MordecaiX:composeApp>.org.koin.core.module.Module.<init> (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[62929]:0x8d0f13)
    at <MordecaiX:composeApp>.org.koin.dsl.module (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[63094]:0x8d52d2)
    at <MordecaiX:composeApp>.org.koin.dsl.module$default (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[63095]:0x8d531c)
    at <MordecaiX:composeApp>.com.crow.mordecaix.model.di.<init properties ViewModelModule.kt> (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[68699]:0x94bde0)
    at <MordecaiX:composeApp>.com.crow.mordecaix.model.di.<get-viewmodelModule> (http://192.168.1.127:8080/ComposeApp.wasm:wasm-function[68690]:0x94bbcf)
commonMain.dependencies {
            implementation(compose.runtime)
            implementation(compose.foundation)
            implementation(compose.material)
            implementation(compose.material3)
            implementation(compose.ui)
            implementation(compose.components.resources)
            implementation(compose.components.uiToolingPreview)

            implementation(libs.jetbrains.kotlinx.coroutines)
            implementation(libs.jetbrains.kotlinx.collections)
            implementation(libs.jetbrains.lifecycle.viewmodel)
            implementation(libs.jetbrains.lifecycle.runtime.compose)
            implementation(libs.jetbrains.compose.materialWindow)
            implementation(libs.jetbrains.compose.material3.adaptive)
            implementation(libs.jetbrains.compose.material3.adaptive.layout)
            implementation(libs.jetbrains.compose.material3.adaptive.navigation)
            implementation(libs.jetbrains.androidx.navigation)
            implementation(libs.koin.core)
            implementation(libs.koin.compose)
            implementation(libs.koin.compose.viewmodel)
            implementation(libs.koin.compose.viewmodel.navigation)
            implementation(libs.ktor.client.core)

            implementation(libs.kotlinx.datetime)
        }
wasmJs {
        moduleName = "ComposeApp"
        browser {
            val projectDirPath = project.projectDir.path
            commonWebpackConfig {
                outputFileName = "composeApp.js"
                devServer = (devServer ?: KotlinWebpackConfig.DevServer()).apply {
                    static = (static ?: mutableListOf()).apply {
                        // Serve sources to debug inside browser
                        add(projectDirPath)
                    }
                }
            }
        }
        binaries.executable()
    }
compose-plugin = "1.7.0-rc01"
kotlin = "2.0.20"
koin = "4.0.0"

// ============================================
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    initializeApplication()
    ComposeViewport(document.body!!) {
        App()
    }
}
@Composable
fun App() {
    MordecaiXTheme {
        MordecaiXApp()
    }
}
@Composable
fun MordecaiXApp() { }
// ============================================

fun initializeApplication() {
    startKoin {
        modules(
            viewmodelModule, 
        )
    }
}

val viewmodelModule = module {
    viewModel { AppViewModel() }
}
class AppViewModel : ViewModel() {}
// ============================================

./gradlew wasmJsBrowerRun It's normal to use localhost access

startkoin is normal, it is also normal when I delete modules, and there is a problem when modules are added

artemyto commented 6 days ago

It seems like the problem is in this line: https://github.com/InsertKoinIO/koin/blob/4c56aa0f4bd27c3ab0837b939989d8060b82b3b2/projects/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTools.kt#L39

Introduced in this commit: https://github.com/InsertKoinIO/koin/commit/926a7b6c90275a05b8c913a88cc3f7d4d6be82c7

Something is wrong with Uuid.random() on wasmJs

artemyto commented 6 days ago

It seems like it is required to use https or localhost to access Uuid.random() on wasmJs: https://stackoverflow.com/a/77981051

arnaudgiuliani commented 4 days ago

4.0.1 will fix it 👍