Hello! I’m using the <kotlin-compile-testing> libr...
# ksp
g
Hello! I’m using the kotlin-compile-testing library by @Zac Sweers to test my Processor with Kotlin 2.1.0-RC and KSP2. However, I’m facing compilation issues related to the generated files. Although the files are correct, the compiler throws an
Unresolved reference
error for the following imports:
Copy code
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
I’ve tried specifying the required
.jar
files and plugins containing these references using the
classpaths
and
pluginClasspaths
variables from
KotlinCompilation
, but the issue persists. Here’s the packages I’m referencing: JARs:
org.jetbrains.compose.runtime
org.jetbrains.compose.ui
Gradle Plugins:
org.jetbrains.compose
org.jetbrains.kotlin.multiplatform
org.jetbrains.kotlin.plugin.compose
I managed to resolve all other missing references, but these two remain problematic. Interestingly, if I don’t use
useKsp2()
and set the
languageVersion
to 1.9, the tests pass without any
Unresolved reference
issues. Could you help me understand why this happens and how to fix it? Thanks!
r
Maybe a little far fetched, but could it be related to this: https://kotlinlang.slack.com/archives/C013BA8EQSE/p1729726539158869 I assume this happens to you on a Mac?
g
I’m using a mac but my output error is different:
Copy code
Expected :OK
Actual   :COMPILATION_ERROR
Because of a few, ex:
Unresolved reference 'ComposeUIViewController'.
I use this configuration:
Copy code
return KotlinCompilation().apply {
    useKsp2()
    usesKsp2 = precursorTools.contains("ksp2")
    languageVersion = if (!usesKsp2) "1.9" else languageVersion
...
}
and if I comment
useKsp2()
it works. The references are found or they aren’t checked, I don’t know. 🤷‍♂️ I’m migrating from
tschuchortdev/kotlin-compile-testing
to
ZacSweers/kotlin-compile-testing/issues
because I could not use Kotlin 2.1.0 with
tschuchortdev
, and also I’m starting to realize
tschuchortdev
’s didn’t offer the capability of testing with KSP2. I thought using
ksp.useKSP2=true
in the project’s root
gradle.properties
would be enough but guess not. When running the apps it works correctly (both for 2.1.0-RC2 and 2.1.0-RC2-1.0.28, KSP1 or KSP2) . It only fails with KSP2 in Tests because of the references, the code is always generated correctly.
I’ve been doing some investigation and it seams that not using KSP2 would make
kspWithCompilation = false
. So that’s why when using
tschuchortdev/kotlin-compile-testing
this was working (it wasn’t compiling). I’ve tested using
tschuchortdev
with
kspWithCompilation = true
and also
ZacSweers
without
useKsp2()
with
languageVersion = 1.9
and
kspWithCompilation = true
and both equally fail, as expected.
So in the end, I guess both libs are working as expected, I just have to understand how to add to the classpath both:
Copy code
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
Which I believe they are inside
.klib
files 😕
Copy code
File("~/.konan/kotlin-native-prebuilt-macos-aarch64-2.1.0-RC2/klib/platform/ios_simulator_arm64/org.jetbrains.kotlin.native.platform.UIKit/default/linkdata/package_platform.UIKit/04_UIKit.knm")
Copy code
File("~/.gradle/caches/modules-2/files-2.1/org.jetbrains.compose.ui/ui-uikitsimarm64/1.7.1/6314e65b9b0f9dc7e0a5fdd70256def5ffb4377c/ui.klib!/default/linkdata/package_androidx.compose.ui.window/1_window.knm")
Can’t find a way to add them in the classpath 🤔
I tried adding the plugins and dependencies in the module’s build.gradle:
Copy code
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.kotlin.compose.compiler)
implementation(compose.runtime)
implementation(compose.ui)
but continues to fail 🤷‍♂️
r
The compile testing library runs on the JVM, it’s not a multiplatform project and therefore cannot reference code from other platforms. I think this may be the issue. Try to reference iOS code on Android, it doesn’t work.
g
That’s a good point
@ralf so I found a workaround 😅
Copy code
private val dummySourceA = """
    package androidx.compose.ui.window

    import androidx.compose.runtime.Composable
    import platform.UIKit.UIViewController

    fun ComposeUIViewController(content: @Composable () -> Unit): UIViewController = UIViewController()
""".trimIndent()

private val dummySourceB = """
    package platform.UIKit
    open class UIViewController
""".trimIndent()

val compilation = prepareCompilation(kotlin("Screen.kt", code), kotlin("ComposeUIViewController.kt", dummySourceA), kotlin("UIViewController.kt", dummySourceB))
will compile. So, I have 2 options until the library supports KMP, either comment
Copy code
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
or use this dummy data 😅