Hi guys, has anyone had any success loading KSP-ge...
# ksp
m
Hi guys, has anyone had any success loading KSP-generated sources using the classloader after a
kotlin-compile-testing
compilation? It seems to be possible using KAPT (using
result.classLoader.loadClass("com.pkg.GeneratedClass")
) but so far I haven’t got it to work with KSP. It seems like running the compilation only generates source files, but no
.class
files, so I’m guessing there’s some other gradle task involved that is not being run and it’s not possible yet…?
m
thx @ephemient, but it doesn’t seem to be related to that 😕 It’s not that I can’t see the source files generated by KSP (in fact just calling
compilation.kspSourcesDir
I’m able to browse through the generated files), but rather than these generated files are not compiled & added to the classpath, so I can’t load them later at runtime in the test code to perform assertions
☝️ 1
So, apparently this is known issue (https://github.com/tschuchortdev/kotlin-compile-testing/issues/72#issuecomment-744460788) with a working work-around! It’s kinda hacky, but doing a second compile pass seems to work and I’m able to load the generated files into the classpath.
j
Ran into the same thing. I think there is a fix coming
🙌 1
m
@jameskleeh it's been 4 months, do you happen to know about any fixes? Still doesn't work for me. @Miguel Vera Belmonte I have an issue with the double compilation workaround, maybe you will know how to help. It tries to compile but fails on unresolved references from the generated code to classes which should be in the classpath (they are when I use kapt for the same thing, e.g.
Unresolved reference: Flow
). It's like I'm missing one more piece at
sources = compilation.sources + compilation.kspGeneratedSourceFiles
line?
eg this source file
Copy code
package com.example
                            
                            import com.futuremind.koru.ToNativeInterface
                            import kotlinx.coroutines.flow.Flow
                            
                            @ToNativeInterface
                            interface A {
                                suspend fun a(whatever: Int) : Float
                            }
will result in unresolved reference on ToNativeInterface and Flow
163 Views