IntelliJ Console Interleaving Output on Kotlin?

Ran a Kotlin script for Advent of Code in IntelliJ, and got output like:

Starting position: Position(row=0, column=5)
java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
At position Position(row=0, column=5)
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at Day19.walkMap(day19.kts:39)
At position Position(row=1, column=5)
	at Day19.<init>(day19.kts:19)
At position Position(row=2, column=5)
Passed through A
At position Position(row=3, column=5)
At position Position(row=4, column=5)
At position Position(row=5, column=5)

Process finished with exit code 3

If I run the same thing at the command-line, the stack trace and output don’t get interleaved:

    Starting position: Position(row=0, column=5)
At position Position(row=0, column=5)
At position Position(row=1, column=5)
At position Position(row=2, column=5)
Passed through A
At position Position(row=3, column=5)
At position Position(row=4, column=5)
At position Position(row=5, column=5)
java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at Day19.walkMap(day19.kts:39)
	at Day19.<init>(day19.kts:19)

Probably this is to do with the fact that standard output and standard error are different streams. These streams are demultiplexed onto a single console, but the way it happens (and especially the timing and buffering) is dependent on the terminal (actually the application that created the streams affected, this can be the terminal but could also be the shell or any other intermediary). In this case Intellij is correct in that it is expected that standard output is buffered (only actually processed after a while) where standard error is not (you want to know about errors immediately). Your command line probably is not buffered at all. If you want to “fix” things, you can flush your stream at key points to make sure that outputs are not still buffered inside.

Perhaps – although I’ve used IntelliJ tools with lots of languages, and this isn’t something I’m used to, although I’d have to conduct empirical testing to confirm that it’s different with Kotlin than other languages.

Hm. On a quick test, looks like IntelliJ behaves similarly in Java despite my recollection. It might not be identical, but if I interleave printing to standard out and standard err, the stderr comes out first. If I print a bunch of lines to stdout and then print an exception, it gets interleaved.

I found a similar complaint about RubyMine:

Guess my memory doesn’t line up with reality, it’s not Kotlin.

https://youtrack.jetbrains.com/issue/IDEA-70016