kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.07k stars 126 forks source link

Does not work with Kotlin 2.0.0 #421

Open MasWag opened 4 months ago

MasWag commented 4 months ago

It seems kscript does not work with Kotlin 2.0.0. What I observe is the same as #408:

$ kscript 'println("hello world")' 

Exception in thread "main" java.lang.ClassNotFoundException: kscript.scriplet.Scriplet
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at Main_Scriplet$Companion.main(Main_Scriplet.kt:5)
    at Main_Scriplet.main(Main_Scriplet.kt)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:70)
    at org.jetbrains.kotlin.runner.Main.run(Main.kt:183)
    at org.jetbrains.kotlin.runner.Main.main(Main.kt:193)
[kscript] [ERROR] Execution of scriplet failed:
[kscript] [ERROR] Command     : 'bash -c /opt/homebrew/Cellar/kotlin/2.0.0/libexec/bin/kotlin  -classpath '/Users/mwaga/Library/Caches/kscript/jar_7a6cdcc8b547b2bb2a0ad2ab4f24c291/scriplet.jar:/opt/homebrew/Cellar/kotlin/2.0.0/libexec/lib/kotlin-script-runtime.jar' Main_Scriplet '
[kscript] [ERROR] Exit Code   : 1   
[kscript] [ERROR] 

It works well with Kotlin 1.9.24 (after cache removal).

$ brew unlink kotlin && brew link kotlin@1.9.24  && rm -rf ~/Library/Caches/kscript/ && kscript 'println("hello world")'
Unlinking /opt/homebrew/Cellar/kotlin/2.0.0... 6 symlinks removed.
Linking /opt/homebrew/Cellar/kotlin@1.9.24/1.9.24... 6 symlinks created.
hello world
NoUsernameProvided commented 4 months ago

Same issue here.

zorenkonte commented 4 months ago

Got the same error

aartiPl commented 4 months ago

I will try to solve it, although I don't have much time to invest. Contributions from anyone are more than welcome.

aartiPl commented 1 month ago

I have investigated the issue. The problem with version 2.0 is that it completely changes how the scripts are executed. The current way of executing the script is by wrapping it with the following code:

https://github.com/kscripting/kscript/blob/6acd4e1907478c49cc4e3db640200f926933b15d/src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt#L48

But now, this method does not work anymore.

I was also thinking about a more sophisticated way of rewriting the script so that it is put inside fun main() {} block. But it will only work for some cases - for example, enum class {} can not be defined in the function's local scope, so it will fail if you try to define an enum in your script.

The only possible alternative I can see is to use the Kotlin script engine to implement the KScript engine. I have started the branch with those changes, but I don't have time to accomplish that work: https://github.com/kscripting/kscript/blob/82af53b691f967c6cdad5efadc79965dc1edb932/src/main/kotlin/io/github/kscripting/kscript/KscriptHandler.kt#L133

If anyone wants to take on this task, feel free to do so. I will accept the MRs.

Dogacel commented 4 days ago

@aartiPl if it doesn't work with kotlin 2.0+, is it possible to select which version of kotlin compiler to use? I tried using this for the first time and having the same error.

Maybe we need an environment manager like jenv for kotlin.