https://kotlinlang.org logo
#scripting
Title
# scripting
v

Vampire

06/26/2022, 8:57 PM
Can you somehow define which Kotlin version a Kotlin Script is written in? In my case especially a
.main.kts
script. I guess it uses whatever Kotlin version you are running it with. But how to make sure it is at least version X? Do you have to manually check that if necessary? Actually more important, how does IntelliJ know which stdlib to use for navigation and completion? I have a script that I intend to run only with Kotlin 1.7.0, so I could use Kotlin 1.7.0 API. But even if I add a
@file:DependsOn
on 1.7.0 stdlib-jdk8, it still navigates to 1.3.72 Kotlin stdlib. The installed plugin is 1.7.0 and the complier settings have "latest stable (1.7)" for language and api version.
👀 1
i

ilya.chernikov

06/27/2022, 9:54 AM
cc: @Andrei Klunnyi
a

Andrei Klunnyi

06/27/2022, 9:57 AM
@Vladimir Dolzhenko , can I ask for you help, please?
v

Vladimir Dolzhenko

06/27/2022, 2:17 PM
well, you can check what stdlib version is used from a classpath if you run it - it would be the same version as plugin uses (1.7.0 in your and mine cases) navigation uses stdlib from script dependencies of gradle - looks like a bug
will you file an issue or I can do it for you ?
v

Vampire

06/27/2022, 2:44 PM
"can I do it" sounds like you prefer doing it yourself. Be my guest. 🙂 Also post the link here please, so I can watch'n'vote. 🙂
1
v

Vladimir Dolzhenko

06/27/2022, 2:50 PM
m

mbonnin

06/27/2022, 2:54 PM
How is it supposed to work actually? 1. Use the same stdlib version as the
kotlin
binary used to run the script? 2. Use the stdlib resolved from the
@DependsOn
annotations?
v

Vampire

06/27/2022, 2:54 PM
Ah, thanks for the link
v

Vladimir Dolzhenko

06/27/2022, 2:56 PM
@mbonnin at least it has to support p1. p2 looks like extension to me of p1.
m

mbonnin

06/27/2022, 2:57 PM
That's the ideal case indeed, just curious because Gradle forces the stdlib version. I thought maybe the same would apply to Kotlin scripts. I'll make a quick test
v

Vladimir Dolzhenko

06/27/2022, 2:58 PM
Indeed, Gradle forces the stdlib version but it has to be applicable only for
.gradle.kts
files
m

mbonnin

06/27/2022, 3:00 PM
Looks like the same is happening with
*.main.kts
:
Copy code
#!/usr/bin/env kotlin

@file:DependsOn("org.jetbrains.kotlin:kotlin-stdlib:1.7.0")

println(KotlinVersion.CURRENT.toString())
And then running it:
Copy code
$ kotlin -version 
Kotlin version 1.6.20-release-275 (JRE 17.0.2+8)
$ ./scripts/test.main.kts
1.6.20
v

Vladimir Dolzhenko

06/27/2022, 3:01 PM
wdym ?
v

Vampire

06/27/2022, 3:02 PM
He means it uses the stdlib of the Kotlin version running the script, not the one from
DependsOn
😕
☝️ 1
v

Vladimir Dolzhenko

06/27/2022, 3:03 PM
got it, as to me
@DependsOn
support is not implemented cc: @ilya.chernikov
i

ilya.chernikov

06/28/2022, 9:12 AM
@Vladimir Dolzhenko,
@DependsOn
used to work just fine, but appears to be broken now (in IntelliJ, I mean). Let's discuss it.
@mbonnin, on the runtime the stdlib is added to the classpath by default, so the second one added via
@DependsOn
doesn't have any effect. If you want to supply specific version, you need to pass
-no-stdlib
to compiler somehow.
(The behavior here is similar to the regular cli compiler.)
m

mbonnin

06/28/2022, 9:21 AM
Gotcha, makes sense 👍
Kscript has support for things like
@file:KotlinOpts("-J-Xmx5g") @file:KotlinOpts("-J-server") @file:CompilerOpts("-jvm-target 1.8")
but I can see how that's a 🐔 -🥚 problem
2 Views