Possible kts bug

I was in a project, and I was trying to create a kts file to run some simple commands.

I eventually tested the following file Main.kts
with the following code:


I got the following error:

Error:(3, 1) Kotlin: Cannot access script base class 'kotlin.script.templates.standard.ScriptTemplateWithArgs'. Check your module classpath for missing or conflicting dependencies

I then tried a whole new Kotlin project (from Intellij) and made a kts file inside the src directory. It had the same error.

Then I moved the kts file outside of the src directory. It worked. I then moved it back into the src directory, unmarked src as a sources directory, and it worked.

Without it marked as a src directory, I get no syntax highlighting, but it works.

Is there anyway to create a script file in Intellij, get syntax highlighting, and have it run? Preferably in the directory marked as sources?

1 Like

Thanks, please see workarounds here: https://youtrack.jetbrains.com/issue/KT-26271

When a script is a part of a module, it takes dependencies from that module, so you need to include kotlin-script-runtime to the module dependencies explicitly.
If it is outside of a module, the default dependencies are applied, that’s why it works outside of the src dir.
Unfortunately, it is not very obvious. We’re thinking about possible solutions.