Hi there, as all the factory functions for `Kotlin...
# compiler
t
Hi there, as all the factory functions for
KotlinCoreEnvironment
are now marked deprecated in the latest
kotlin-compiler-embeddable
and are to be removed from version 2.3+, is it safe to assume the reworked alternative will be available in version 2.3 or we should look for some other alternatives 😄?
h
Why do you need a custom KotlinCoreEnvieonment?
t
I want to use it to create an instance of PsiManager (
PsiManager.getInstance(KotlinCoreEnvieonment.project)
), which in turn is needed for finding and parsing kotlin files (KtFile).
I use it currently for automating a similar patterned code changes to some kotlin files. I have it automated in a gradle task because I want to be able to run it in a CI job.
d
We plan to completely remove the
KotlinCoreEnvironment
eventually, in the context of removing the dependency on the intellij core from the compiler. As an alternative you can just create
MockProject
manually, without creating the whole environent:
As an alternative you can use `JavaCoreProjectEnvironment`:
Copy code
fun test() {
    val disposable = Disposer.newDisposable()
    val applicationEnvironment = JavaCoreApplicationEnvironment(disposable)
    val projectEnvironment = JavaCoreProjectEnvironment(disposable, applicationEnvironment)
    val manager = PsiManager.getInstance(projectEnvironment.project)
}
❤️ 1
t
Beautiful! Thanks a lot @dmitriy.novozhilov.
in the context of removing the dependency on the intellij core from the compiler
I see that
JavaCoreProjectEnvironment
is under an
**.intellij.core
package, is there a chance it also depends on the intellij core dependency which you mentioned?
d
It is the part of intellij infrastructure. It does not belong to the compiler.
👍 1
t
@dmitriy.novozhilov Does that mean I can't run it in a e.g stand-alone gradle task that runs independent of intellij infrastructure? I tried your code but creation of
JavaCoreApplicationEnvironment
fails because
<http://org.jetbrains.kotlin.com|org.jetbrains.kotlin.com>.intellij.openapi.application.ApplicationManager.getApplication()
returns null
d
Does that mean I can't run it in a e.g stand-alone gradle task that runs independent of intellij infrastructure?
Let me clarify on this. By "intellij infrastructure" I mean
intellij-core.jar
which is a dependency of the kotlin compiler. It contains some basic services which are used by the real intellij, but they also could be run independently (and that's how kotlinc utilizes them). The whole story with
Project
,
CoreEnvironment
and even PSI itself goes from this
intellij-core
, and by "getting rid of intellij" I meant removing this dependency and replacing all usages in the compiler with something else.
I tried your code but creation of
JavaCoreApplicationEnvironment
fails because
<http://org.jetbrains.kotlin.com|org.jetbrains.kotlin.com>.intellij.openapi.application.ApplicationManager.getApplication()
returns null
That's very strange, because
JavaCoreApplicationEnvironment
creates the application in the contstructor, it doesn't retrieve it. Could you please share the stacktrace of the exception and exact code you've run?
👍 1
For the reference, I've wrote the following test in the repo with just a dependency on
org.jetbrains.kotlin.kotlin-compiler.jar
and it successfully passes.
Copy code
class SomeTest {
    @Test
    fun someTest() {
        val disposable = Disposer.newDisposable()
        val applicationEnvironment = JavaCoreApplicationEnvironment(disposable)
        val projectEnvironment = JavaCoreProjectEnvironment(disposable, applicationEnvironment)
        val manager = PsiManager.getInstance(projectEnvironment.project)
        val application = ApplicationManager.getApplication()
        Assertions.assertNotNull(manager)
        Assertions.assertNotNull(application)
    }
}
👍 1
t
Thanks for the detailed explanation. I still get the error but I will try to create a small project just to be sure whether it's my setup.