Running kotlin scripts with a custom ScriptDefinition inside Intellij Idea


I’m not sure if this is a right place to ask this question, so if it is an offtopic here please forgive me and point to the right place.


We are playing with a custom DSL based on Kotlin script.

We have successfully created a custom ScriptDefinition class annotated with @KotlinScript, registered a new specific file extension, and defined appropriate methods in the class. Our gradle-based project produces a JAR with the ScriptDefinition class.

We have added a dependency on the produced JAR into a project with the scripts (it’s also gradle-based). Now Intellij Idea (both Community and Ultimate editions) recognizes the file extension, and correctly parses these scripts with proper syntax checking, code auto-completion and so on. We even can run the script using a custom command line runner. So far so good, indeed!


The problem arrives when we try to execute such a script from within Intellij Idea.

For scripts located outside registered srcDirs Idea doesn’t pass -cp <classpath> argument to org.jetbrains.kotlin.cli.jvm.K2JVMCompiler. Entities defined in our ScriptDefinitions are not recognized and the script compilation fails.

For scripts located inside registered srcDirs the argument -cp <classpath> is passed, but Idea complains if two scripts have a same name, even when they are located in different directories: Duplicate JVM class name 'Script_bi' generated from: Script_bi, Script_bi.

Additionally I need to manually specify -Dkotlin.script.classpath=$Classpath$ for each script run configuration which is inconvenient (we have a lot of scripts in the project).


I would like to politely ask your suggestions how to run custom KTS scripts from Intellij Idea.

Thank you in advance!

Sorry, I don’t have an answer, but a question myself: Does “outside” also refer to resources? I.e. does auto-completion work for you if the script is a resource? Because that’s something I’m struggling to get working.