kotlinkotlin-multiplatformkotlin-native

Stacktrace of assertion failure in Kotlin Native unit test doesn't show line number of source code properly


If I run a failing test in Kotlin Native, line number of source code won't show properly in the failure message.

kotlin.AssertionError: Expected value to be false.
    at kotlin.Throwable#<init>(Unknown Source)
    at kotlin.Error#<init>(Unknown Source)
    at kotlin.AssertionError#<init>(Unknown Source)
    at kotlin.test.DefaultAsserter#fail(Unknown Source)
xxx.TempTest.helloWorld FAILED
      kotlin.AssertionError at null:-1

Expecting line number of source code instead of Unkown Source and kotlin.AssertionError at null:-1.

I also post my build.gradle.kts here. Mainly followed the template from the kotlin official doc.

plugins {
    kotlin("multiplatform") version "1.8.10"
}

... // group and version declaration

repositories {
    mavenCentral()
}

kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTarget.apply {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }

    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val nativeMain by getting
        val nativeTest by getting

        nativeMain.dependsOn(commonMain)
    }
}


Solution

  • Since Kotlin 1.6.20, you can add kotlin.native.binary.sourceInfoType=libbacktrace to gradle.properties and get line numbers in exception stack traces on Linux/Mac. https://kotlinlang.org/docs/whatsnew1620.html#better-stack-traces-with-libbacktrace