dkandalov / live-plugin

IntelliJ plugin for writing IntelliJ plugins at runtime ⚡️
https://plugins.jetbrains.com/plugin/7282
Apache License 2.0
837 stars 66 forks source link

Cannot depend on org.jetbrains.kotlin in latest Idea #186

Closed grsky360 closed 1 week ago

grsky360 commented 1 month ago

Idea version: 2024.2

After depends-on org.jetbrains.kotlin, and use some classes, compilation error occurs

class 'kotlin.script.experimental.host.ScriptingHostConfiguration' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 2.0.0, expected version is 1.8.0.

Hope to support use embedded kotlin compiler in Idea

dkandalov commented 1 month ago

Thank you for reporting this! Some of my custom plugins stopped working after IDE upgrade and didn't get a chance to look into that, but now I know :)

I'm getting similar errors in 2024.2:

error: class 'org.jetbrains.kotlin.psi.KtLambdaArgument' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 2.0.0, expected version is 1.8.0.

I guess in 2024.2 Kotlin plugin has been compiled with a later version than IDE stdlib version https://plugins.jetbrains.com/docs/intellij/using-kotlin.html#kotlin-standard-library

I'll need to upgrade liveplugin Kotlin dependency to 1.9.

grsky360 commented 1 month ago

Thanks!

BTW, is it possible to support custom kotlin compiler version? Maybe 3 options, like

  1. Embedded by plugin
  2. Manual version from maven/local path?
  3. Embedded by IDE (Not sure about this one)
grsky360 commented 1 week ago

Cannot compile the kts script

Error compiling script.
/hello-world/plugin.kts:25:22: error: unresolved reference: isIdeStartup
show("isIdeStartup: $isIdeStartup")
                     ^
/hello-world/plugin.kts:26:17: error: unresolved reference: project
show("project: $project")
                ^
/hello-world/plugin.kts:27:26: error: unresolved reference: pluginDisposable
show("pluginDisposable: $pluginDisposable")
                         ^
/hello-world/plugin.kts:28:20: error: unresolved reference: pluginPath
show("pluginPath: $pluginPath")
grsky360 commented 1 week ago
Error while loading plugin class.
com.intellij.diagnostic.PluginException: Cannot load class Plugin (  error: kotlin/script/templates/standard/ScriptTemplateWithArgs,  classLoader=PluginClassLoader_Fork(plugin=Default plugin descriptor for hello-world, packagePrefix=null, instanceId=2, state=active)) [Plugin: hello-world]
    at liveplugin.implementation.pluginrunner.PluginClassLoader_Fork.loadClassInsideSelf(PluginClassLoader_Fork.java:447)
    at liveplugin.implementation.pluginrunner.PluginClassLoader_Fork.tryLoadingClass(PluginClassLoader_Fork.java:257)
    at liveplugin.implementation.pluginrunner.PluginClassLoader_Fork.loadClass(PluginClassLoader_Fork.java:229)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    at liveplugin.implementation.pluginrunner.kotlin.KotlinPluginRunner.setup(KotlinPluginRunner.kt:84)
    at liveplugin.implementation.pluginrunner.PluginRunnerKt$runWith$1.invoke(PluginRunner.kt:161)
    at liveplugin.implementation.pluginrunner.PluginRunnerKt$runInBackground$2.run(PluginRunner.kt:176)
    at com.intellij.openapi.progress.impl.CoreProgressManager.startTask(CoreProgressManager.java:477)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.startTask(ProgressManagerImpl.java:133)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcessWithProgressAsynchronously$6(CoreProgressManager.java:528)
    at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$4(ProgressRunner.java:249)
    at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:98)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:221)
    at com.intellij.platform.diagnostic.telemetry.helpers.TraceKt.use(trace.kt:49)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:220)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$13(CoreProgressManager.java:660)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:735)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:691)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:659)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:79)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:202)
    at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:98)
    at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$5(ProgressRunner.java:249)
    at com.intellij.openapi.progress.impl.ProgressRunner$ProgressRunnable.run(ProgressRunner.java:501)
    at com.intellij.openapi.progress.impl.ProgressRunner.lambda$launchTask$18(ProgressRunner.java:466)
    at com.intellij.util.concurrency.ChildContext$runAsCoroutine$1.invoke(propagation.kt:92)
    at com.intellij.util.concurrency.ChildContext$runAsCoroutine$1.invoke(propagation.kt:92)
    at com.intellij.util.concurrency.ChildContext.runAsCoroutine(propagation.kt:97)
    at com.intellij.util.concurrency.ChildContext.runAsCoroutine(propagation.kt:92)
    at com.intellij.openapi.progress.impl.ProgressRunner.lambda$launchTask$19(ProgressRunner.java:464)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:735)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:732)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:732)
    at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.NoClassDefFoundError: kotlin/script/templates/standard/ScriptTemplateWithArgs
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1027)
    at com.intellij.util.lang.UrlClassLoader.consumeClassData(UrlClassLoader.java:270)
    at com.intellij.util.lang.FileLoader.findClass(FileLoader.java:187)
    at com.intellij.util.lang.ClassPath.findClassInLoader(ClassPath.java:240)
    at com.intellij.util.lang.ClassPath.findClassWithoutCache(ClassPath.java:229)
    at com.intellij.util.lang.ClassPath.findClass(ClassPath.java:212)
    at liveplugin.implementation.pluginrunner.PluginClassLoader_Fork.loadClassInsideSelf(PluginClassLoader_Fork.java:439)   ... 36 more
Caused by: java.lang.ClassNotFoundException: kotlin.script.templates.standard.ScriptTemplateWithArgs PluginClassLoader_Fork(plugin=Default plugin descriptor for hello-world, packagePrefix=null, instanceId=2, state=active)
    at liveplugin.implementation.pluginrunner.PluginClassLoader_Fork.loadClass(PluginClassLoader_Fork.java:232)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)  ... 44 more
dkandalov commented 1 week ago

Yes, sorry the update is not working in the latest IntelliJ. (It worked in 2024.2.0.1 but not in 2024.2.1 even though it's the same major version 🤷)

It all comes down to the changes in the Kotlin scripting support which not easy to debug.

dkandalov commented 1 week ago

BTW, is it possible to support custom kotlin compiler version? Maybe 3 options, like

It is possible, the question is how much effort it can take and what are the benefits :) Btw, it's not only compiler but also Kotlin stdlib.

  1. Embedded by plugin

This is what LivePlugin does now. It's bundled with an embedded Kotlin compiler because IDEs are not guaranteed to have it.

  1. Manual version from maven/local path?

Going to down this route it seems logical to make the whole class path configurable. I prototyped "run configurations" for LivePlugins a while ago. The question was where to store the run configuration settings... maybe in the plugin folder because live plugins have an IDE scope, but then this feels a bit like almost like a proper project. Custom path also implies that it should be the same path used for script highlighting which is also a bit tricky with Kotlin script (at least the way I managed to get it working).

  1. Embedded by IDE (Not sure about this one)

IIRC this is what LivePlugin had initially (similar to Groovy support) but I soon discovered that Kotlin versions bundled with IntelliJ IDEs are changing quite often and are not very compatible without changes in the plugin. And some IDEs don't have embedded Kotlin compiler.

Sorry, it takes a while to fix the plugin :( It's JDK upgrade to 21, Kotlin upgrade to 1.9 (which changed how it handles scripting), Gradle build plugin with completely new version...

grsky360 commented 1 week ago

Thanks for explaining. And looks IntelliJ is doing some breaking change in the major version....

grsky360 commented 1 week ago

@dkandalov It works well after upgrade to 0.9.1, many thanks!