Customised Kotlin scripts support in IntelliJ
Answered
Hi IntelliJ,
Currently the IDE support is only for .main.kts Kotlin scripts? Is there any way to enable the support for customised Kotlin scripts such as .simple.kts or whatever? I created a template and an engine for my scrips but unfortunately those scripts has plenty of uresolve reference errors.
Please sign in to leave a comment.
You can take a look at this guide: https://kotlinlang.org/docs/custom-script-deps-tutorial.html
It explains how to set up support for custom Kotlin script types like `.simple.kts`.
If you follow that and still keep seeing unresolved reference errors, it would be great if you could report it to us here: https://youtrack.jetbrains.com/newissue?project=KT so the team can take a closer look.
Thanks!
You're hitting a common challenge when working with custom Kotlin scripts in IntelliJ IDEA beyond the standard
.main.ktsorbuild.gradle.ktsfiles. The "unresolved reference errors" indicate that IntelliJ's IDE is not fully aware of the classpath and script definition for your.simple.ktsfiles.Here's a breakdown of why this happens and how you can approach enabling proper support:
Why you're seeing unresolved reference errors:
.ktsfiles, particularly for Gradle Kotlin DSL (build.gradle.kts) and standalonemain.ktsscripts. However, for custom extensions like.simple.kts, it doesn't automatically know how to compile them or what dependencies they might have.How to enable support for customized Kotlin scripts in IntelliJ IDEA:
The core idea is to teach IntelliJ about your custom script type by providing a proper Kotlin Script Definition. This often involves creating a separate module in your project.
Here's a general approach, drawing from the Kotlin documentation and community discussions:
Add the necessary Kotlin scripting dependencies to this module's
build.gradle.kts(orpom.xml):Kotlin
@KotlinScript.Example:
Kotlin
META-INF:src/main/resources/META-INF/kotlin/script/templates/templatesdirectory, create an empty file with the fully qualified name of your script template class. For the example above, the file name would becom.example.SimpleScriptTemplate. Ensure there's no.classor.ktextension in the file name itself..simple.ktsfiles has a dependency on your script definition module.File | Settings/Preferences | Languages & Frameworks | Kotlin | Kotlin Scripting..ktsscripts. This ensures IntelliJ uses your specific definition for your.simple.ktsfiles. This is a common pitfall.File | Invalidate Caches / Restart...and select "Invalidate and Restart.".simple.ktsfiles, you can usually create a "Kotlin Script" run/debug configuration in IntelliJ IDEA. In this configuration, specify the path to your.simple.ktsfile. IntelliJ should then use your registered script definition to execute it.Common causes of unresolved reference errors and troubleshooting:
META-INFfile: Double-check the path and name of the empty file inMETA-INF/kotlin/script/templates/. It must be the exact fully qualified name of your@KotlinScriptannotated class.ScriptCompilationConfiguration: ThedependenciesFromClassloaderin yourScriptCompilationConfigurationis crucial. If your scripts use classes from your project or external libraries, those need to be explicitly available to the script's classloader. This is often the hardest part..ktsfiles inside a source root (e.g.,src/main/kotlin). This often helps IntelliJ resolve project-internal references more easily. However, be aware that IntelliJ might warn you that "This script is not supposed to be inside source root" in newer Kotlin versions, as they might be ignored during module compilation in Kotlin 1.9+.dependenciesFromClassloadercan help here, but you might need to be PrepaidCardStatus Login explicit about the names.build.gradle.kts. Mismatches can lead to various IDE issues, including unresolved references.It's a bit of a setup process, but once you have your custom script definition properly configured, IntelliJ IDEA's support for your
.simple.ktsfiles should dramatically improve, giving you the coding assistance you need.Thanks both, all was very useful.