tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
672 stars 63 forks source link

ClassNotFoundException ScriptingCompilerConfigurationComponentRegistrar #389

Open soygabimoreno opened 1 year ago

soygabimoreno commented 1 year ago

Hi! šŸ™‚

Not sure if this is actually an issue with the library or if I have a version conflict among dependencies in my project.

I see this error when I run my tests:

Exception on loading scripting plugin: java.lang.ClassNotFoundException: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar
v: Scripting plugin will not be loaded: not all required jars are present in the classpath (missing files: [./kotlin-scripting-compiler.jar, ./kotlin-scripting-compiler-impl.jar, ./kotlinx-coroutines-core-jvm.jar, ./kotlin-scripting-common.jar, ./kotlin-scripting-jvm.jar, ./js.engines.jar])
v: Using JVM IR backend
v: Configuring the compilation environment
v: Loading modules: [java.se, jdk.accessibility, jdk.attach, jdk.compiler, jdk.dynalink, jdk.httpserver, jdk.jartool, jdk.javadoc, jdk.jdi, jdk.jfr, jdk.jsobject, jdk.management, jdk.management.jfr, jdk.net, jdk.sctp, jdk.security.auth, jdk.security.jgss, jdk.unsupported, jdk.unsupported.desktop, jdk.xml.dom, java.base, java.compiler, java.datatransfer, java.desktop, java.xml, java.instrument, java.logging, java.management, java.management.rmi, java.rmi, java.naming, java.net.http, java.prefs, java.scripting, java.security.jgss, java.security.sasl, java.sql, java.transaction.xa, java.sql.rowset, java.xml.crypto, jdk.internal.jvmstat, jdk.jdwp.agent]
e: java.lang.NoSuchMethodError: 'void org.jetbrains.kotlin.incremental.storage.BasicMap.<init>(java.io.File, org.jetbrains.kotlin.com.intellij.util.io.KeyDescriptor, org.jetbrains.kotlin.com.intellij.util.io.DataExternalizer)'
    at com.google.devtools.ksp.PersistentMap.<init>(Incremental.kt:56)
    at com.google.devtools.ksp.FileToSymbolsMap.<init>(Incremental.kt:62)
    at com.google.devtools.ksp.IncrementalContext.<init>(Incremental.kt:189)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$1.invoke(KotlinSymbolProcessingExtension.kt:171)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$1.invoke(KotlinSymbolProcessingExtension.kt:168)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.handleException(KotlinSymbolProcessingExtension.kt:390)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.doAnalysis(KotlinSymbolProcessingExtension.kt:168)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:123)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:99)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:257)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:42)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:115)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:248)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:88)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
    at com.tschuchort.compiletesting.AbstractKotlinCompilation.compileKotlin(AbstractKotlinCompilation.kt:254)
    at com.tschuchort.compiletesting.KotlinCompilation.compileJvmKotlin(KotlinCompilation.kt:452)
    at com.tschuchort.compiletesting.KotlinCompilation.compile(KotlinCompilation.kt:628)

I have this configuration:

[versions]
agp = "8.1.1"
kotlin = '1.8.20'
kotlinpoet = "1.14.2"
ksp = "1.8.20-1.0.11"
kotlin-compile-testing = "1.5.0"

[libraries]
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlinpoet = { group = "com.squareup", name = "kotlinpoet", version.ref = "kotlinpoet" }
kotlinpoet-ksp = { group = "com.squareup", name = "kotlinpoet-ksp", version.ref = "kotlinpoet" }
ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
ksp-gradlePlugin = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" }
compiletest = { group = "com.github.tschuchortdev", name = "kotlin-compile-testing", version.ref = "kotlin-compile-testing" }
compiletest-ksp = { group = "com.github.tschuchortdev", name = "kotlin-compile-testing-ksp", version.ref = "kotlin-compile-testing" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp"}

Do you now what can be the source of the error?

Could it be because the KSP version is not compatible?

ext.ksp_version = '1.8.0-1.0.8'

Thank you so much for the help! šŸ˜Š

soygabimoreno commented 1 year ago

I think it could be because the Kotlin version from the library (1.8.0) is different from the one I am currently using in my project (1.8.20). It seems it works with KAPT, but not with KSP.

I tried updating to 1.9.0 using this fork and it worked. To get the generated files I did this that I saw among the comments in the issues:


    private val JvmCompilationResult.kspGeneratedSources: List<File>
        get() {
            val kspWorkingDir = outputDirectory.parentFile.resolve(KSP_GENERATED_FOLDER)
            val kspGeneratedDir = kspWorkingDir.resolve(SOURCES_GENERATED_FOLDER)
            val kotlinGeneratedDir = kspGeneratedDir.resolve(KOTLIN_GENERATED_FOLDER)
            return kotlinGeneratedDir.listFilesRecursively()
        }

    private fun File.listFilesRecursively(): List<File> {
        return listFiles()?.flatMap { file ->
            if (file.isDirectory) {
                file.listFilesRecursively()
            } else {
                listOf(file)
            }
        } ?: emptyList()
    }

// ...

private const val KSP_GENERATED_FOLDER = "ksp"
private const val SOURCES_GENERATED_FOLDER = "sources"
private const val KOTLIN_GENERATED_FOLDER = "kotlin"