Issues about IDEA support of Kotlin script (.kts)

By creating a .kts file and copying the code snippet at https://kotlinlang.org/docs/tutorials/command-line.html#using-the-command-line-to-run-scripts into it, the editor marks “args” as an error but the code can be run correctly.

Did I miss some settings or it’s a small bug of IDEA?

Also, the debug icon is de-activated. Will it be enabled in the future?

Thanks.

This is the version I used:
IntelliJ IDEA 2017.3.3 (Ultimate Edition)
Build #IU-173.4301.25, built on January 16, 2018
JRE: 1.8.0_152-release-1024-b11 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.13.2

2 Likes

I’m able to run (though not debug) *.kts files through IntelliJ.

I installed the Kotlin compiler via homebrew. I also had to specify a JDK in the project configuration as one was not configured by default. Here’s the version info:

$ kotlinc -version
info: kotlinc-jvm 1.2.21 (JRE 1.8.0_152-b16)
IntelliJ IDEA 2017.3.4 (Ultimate Edition)
Build #IU-173.4548.28, built on January 29, 2018
JRE: 1.8.0_152-release-1024-b11 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.13.3

Also, the sample code snippet takes a directory as an argument, so you’ll want to provide that as program argument in the run configuration.

I experience the same as ckchen with these versions

➜  tmp kotlinc -version
info: kotlinc-jvm 1.2.21 (JRE 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
IntelliJ IDEA 2017.3.4 (Ultimate Edition)
Build #IU-173.4548.28, built on January 29, 2018

JRE: 1.8.0_152-release-1024-b11 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.0-32-generic

The script does run fine, the problem is that IDEA does not validate the code correctly and falsely reports 4 errors in 3 lines of code:

Is this a configuration problem? Do we miss some settings?

Also, in scripting I want to pass exit codes to the calling shell to indicate success or failure of the script run. I tried to do that with exitProcess (exitProcess - Kotlin Programming Language), but that reults in error: unresolved reference: exitProcess. What is the recommended way to return an exit code from a kotlin script?

Thanks,
Thilo

Does System.exit(n) not work?

Thanks, @reitzig! Your reply solved both of my problems:

  1. As of this morning I cannot reproduce any more the IDE problem. The errors in IDEA have disappeared magically (on my Linux installation as well as on Windows. :mage:)

  2. yes, System.exit(n) works, as well as exitProcess(n), where now IDEA kindly offers to add

    import kotlin.system.exitProcess
    
1 Like

Sometimes just restarting helps :wink:

In my case this appears in the errors too:

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

Running the script from the command line is fine.

Also tried restarting IntelliJ (with invalidate caches)

exitProcess is indeed the better choice since it has return type Nothing (instead of void).

I was able to fix kts problems in Intellij 2017.3 by moving my kts files into the main directory. That is into a directory that belongs to some module. When they were outside of modules (in root directory), the syntax errors were appearing.