Home > database >  Why does calling KotlinCoreEnvironment.createForProduction cause IlegalStateException?
Why does calling KotlinCoreEnvironment.createForProduction cause IlegalStateException?

Time:05-23

I am interested in parsing a Kotlin program into its PSI elements. After some readings, I first set up a project by creating an environment and convert Kotlin source as a string to a KtFile. However, it seems calling KotlinCoreEnvironment.createForProduction(parentDisposable: Disposable, configuration: CompilerConfiguration, configFiles: EnvironmentConfigFiles) seems to be triggering this java.lang.IllegalStateException error.

After googling, I tried changing JDK from 11 to 8, but it didn't resolve the issue. Any advice on how to resolve this problem?

Below is the error message:

Exception in thread "main" java.lang.IllegalStateException: LOGGING: Loading modules: [java.se, jdk.accessibility, jdk.attach, jdk.compiler, jdk.dynalink, jdk.httpserver, jdk.incubator.foreign, jdk.jartool, jdk.javadoc, jdk.jconsole, jdk.jdi, jdk.jfr, jdk.jshell, jdk.jsobject, jdk.management, jdk.management.jfr, jdk.net, jdk.nio.mapmode, 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.management.agent, jdk.jdwp.agent, jdk.internal.ed, jdk.internal.le, jdk.internal.opt] (no MessageCollector configured)
    at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.report(ClasspathRootsResolver.kt:332)
    at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.report$default(ClasspathRootsResolver.kt:330)
    at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.addModularRoots(ClasspathRootsResolver.kt:273)
    at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.computeRoots(ClasspathRootsResolver.kt:128)
    at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(ClasspathRootsResolver.kt:84)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:267)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:111)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:475)
    at docGenerator.DocKt.createKtFile(Doc.kt:70)
    at docGenerator.DocKt.generateDocs(Doc.kt:27)
    at MainKt.main(Main.kt:10)
    at MainKt.main(Main.kt)

Process finished with exit code 1

And my build.gradle is currently as follows:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.6.0'
    id 'org.jetbrains.intellij' version '1.2.1'
}
group 'me.ylee'
version '1.0-SNAPSHOT'

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.0"
    implementation "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.6.21"
    implementation 'net.java.dev.jna:jna:5.11.0'
    implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
}

intellij {
    plugins = ['Kotlin']
}

compileKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

compileTestKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

Just to be clear, when I am calling the KotlinCoreEnvironment.createForProduction in creating a KtFile, I believe I'm importing all the right ones... Please correct me if I am mistaken anywhere and see the code snippet below (the line marked with >>).

import org.jetbrains.kotlin.com.intellij.openapi.fileTypes.FileType
import org.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtFile

...

fun createKtFile(codeString: String, fileName: String): KtFile {
    val disposable: Disposable = Disposer.newDisposable()
    val config = CompilerConfiguration()
    val configFiles: EnvironmentConfigFiles = EnvironmentConfigFiles.JVM_CONFIG_FILES
    try {
        val proj =
>>          KotlinCoreEnvironment.createForProduction(disposable, config, configFiles).project
        val fileType: FileType = KotlinFileType.INSTANCE as FileType
        val file = LightVirtualFile(fileName, fileType, codeString)
        return PsiManager.getInstance(proj).findFile(file) as KtFile
    } finally {
        disposable.dispose()
    }
}

By the way, the suggestion I tried such as using the latest version of org.jetbrains.itellij didn't really work because with the latest version, I failed to build the project.

CodePudding user response:

As suggested in the comment by @Alexey Belkov, I had to add a message collector instance to the compiler configuration.

Below code works now.

fun createKtFile(codeString: String, fileName: String): KtFile {
    val disposable: Disposable = Disposer.newDisposable()
    val config = CompilerConfiguration()
    config.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false))
    val configFiles: EnvironmentConfigFiles = EnvironmentConfigFiles.JVM_CONFIG_FILES
    try {
        val env =
            KotlinCoreEnvironment.createForProduction(disposable, config, configFiles)
        val fileType: FileType = KotlinFileType.INSTANCE as FileType
        val file = LightVirtualFile(fileName, fileType, codeString.trimIndent())
        val res: KtFile = PsiManager.getInstance(env.project).findFile(file) as KtFile
        return res
    } finally {
        disposable.dispose()
    }
}
  • Related