Can you somehow define which Kotlin version a Kotl...
# scripting
v
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
cc: @Andrei Klunnyi
a
@Vladimir Dolzhenko , can I ask for you help, please?
v
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
"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
โญ 1
m
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
Ah, thanks for the link
v
@mbonnin at least it has to support p1. p2 looks like extension to me of p1.
m
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
Indeed, Gradle forces the stdlib version but it has to be applicable only for
.gradle.kts
files
m
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
wdym ?
v
He means it uses the stdlib of the Kotlin version running the script, not the one from
DependsOn
๐Ÿ˜•
โ˜๏ธ 1
v
got it, as to me
@DependsOn
support is not implemented cc: @ilya.chernikov
i
@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
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