How do you make IntelliJ recognize a `.main.kts` f...
# scripting
r
How do you make IntelliJ recognize a
.main.kts
file as a Kotlin Script and provide highlighting, completion and everything else? I just have a folder containing my scripts for now and it just treats them as plain text
p
it general it should work right away, sometimes after refreshing Gradle or reopening the IDE. Do you use any
@file:Import
in your script? If yes, you may be impacted by [KTIJ-14580] Imported script are not supported for scripts outside of a source root, although I don't remember if it manifests exactly as plaintext-like syntax highlighting - for sure there's something off
m
It didn't work for a while with K2 but I think it is fixed now
r
After navigating between different scripts it started parsing some of them, but still not the first one I opened. I also still have this issue where the IDE freezes for seconds every time I change a single character in a script. https://youtrack.jetbrains.com/issue/KTIJ-30183
p
regarding the freezes, does it happen in a dead-simple script with just
println("Hello")
? if no, could you provide a reproducer?
r
No, it doesn't. Then I started expanding my simple script until it froze and it now just did once, but not every time I change it. I'll continue adding stuff.
👍 1
m
FWIW, working fine here with
Build #IU-251.20015.29, built on February 6, 2025
You might have to click the small icon to load the script definitions?
This one
For some of them I need this, not 100% sure why
r
Oh I think it actually freezes every time I touch or just indirectly glance in the general direction of my other, "problematic" scripts, nothing to do with the hello world one. I'll try with a folder containing only a new simple script and expand from that instead
@mbonnin would be nice if that icon appeared at all 😄
😅 1
p
now I recall I had something similar: IDE freezes when editing scripts with @file:Import, and someone in the comment pointed to networking issues. Indeed it might be like: the IDE tried to fetch some packages in a blocking way, but there was a problem, so it timed out after ~15 s and it unfroze. Could you check against some shady
@file:Import
and/or
@file:Repository
?
r
I highly suspected it came from my dependencies because that's the only thing "special" I do, I guess. The moment I added a
@file:DependsOn
referencing a dependency in a custom
@file:Repository
, it froze
The dependency is on an Artifactory server with username and password options
p
great - it narrows down the issue greatly!
r
Well, reaching out to a repository on every file change is not great news 😄
p
I doubt this is the case in general - Kotlin Scripting uses Maven Local for caching the dependencies. What I think happens is that Scripting cannot actually get the artifact, so there's nothing to cache. That's why it tries to fetch stuff from the repo pretty naively
is the dependency from that repo ultimately resolved (can you access classes/functions from it)?
r
Yeah, and now it just stopped freezing. I guess it finally reached it or something. The issue is that I know the repository has no problem, a bunch of Gradle project use it every day from everywhere and there is absolutely 0 timeout anywhere
It also doesn't freeze anymore in my real scripts which are in a different "project". 😕
Maybe having a script with only one dependency from that repository can help reaching the point where it starts working (most of my real scripts have 5+, maybe anyone of them failing would cause the freezing to continue)
v
To rule out the resolution being the problem, create a Maven build, add those dependencies as dependencies, build it. This makes the dependencies downloaded to Maven Local. Once they are in Maven Local, the remote repository will not be touched unless maybe you used a version range.
r
It's already ruled out. As I said I use these dependencies in a lot of projects I'm working on locally, with the same versions. The IDE also kinda works with autocompletion between the freezes, so it definitely found the dependencies. I can't provide a reproducer as I can't provide access to my repository so I guess kotlin scripting will keep not working in the IDE for me (I used it for years, it never worked)
v
For the "you need to press the button and sometimes it is missing", let me quote one of the JB devs working on this:
I'm gonna add something like force load dependencies action to the following release. as well as a shortcut for it.
to be mentioned, it's not the final version of main.kts dependency management — we are still discussing and experimenting around it
But besides that, I also use
.main.kts
since years and it worked fine in the IDE except for some bugs that are all reported. 🤷‍♂️
Are those files within or outside of a source root? I vaguely remember that there were some problems if they were within some source root, but for example work for
.github/workflows/foo.main.kts
r
I usually edit them as plain text or just wait for the freezes (you can queue inputs during a freeze and they're played after the freeze ends… triggering the next freeze)
I really have a folder containing
.main.kts
files. That's it. I also tried setting up an empty Gradle project next to it, to force Fleet to parse the scripts as Kotlin at the time, it kinda worked.
v
If it is a full UI freeze, there are usually thread dumps in the IntelliJ log directory. Those you could send over to JetBrains for analysis to maybe find the culprit.
r
I sent multiple of those in the issue above but they won't do anything without a reproducer I can't provide, I guess the dumps don't contain anything meaningful so there's no way to know without reproducing and debugging the IDE I guess
🤷‍♂️ 1
😞 1
There's a lot to do with dependencies management in scripts. For example, I would like to be able to use variables in dependency definitions, for the version. It would also be nice to configure minimum java and kotlin versions for a script, or to be able to configure repositories without having to specify credentials in plain text in the script, and probably other things
Also. Kotlin/Native scripts.
p
yeah, we're all waiting for some real traction with Scripting
m
FWIW I use a relatively complex scripting setup, often use dependencies from Maven repos, and haven't encountered freezing issues. Performance isn't great but it's not that bad. What I do see, but they're fixing it in the latest EAP, is that scripts are often showing all code as red for a while until the IDE catches up.
However I haven't used repos that require authentication, so that might well be the issue.
r
Maybe I could make my scripts rely only on the local maven repository and have juste one script actually referencing the remote dependencies, I wonder if that would work. I'm not even sure there's a way to reference the local maven repository with these annotations that works Of course they wouldn't be really scripts anymore as they would now depend on the local environment but…