How to create main.kts scratch files?

So, besides “regular” Kotlin scratch files, reading through the Kotlin 1.3.70 announcement made me believe it should also easily be possible to create “main.kts” scratches files that automatically have kotlin-main-kts.jar added to their classpath. However, it does not work for me. This is what I did:

In a Kotlin 1.5.10 project loaded into IDEA 2021.1.2 Ultimate, I chose FileNewScratch FileKotlin. As this creates a file named “scratch.kts” be default, I went ahead, right-clicked in the tab, chose Rename File... and renamed it to “scratch.main.kts”. Then I started typing @file:DependsOn("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.3"), but the IDE tells me Unresolved reference: DependsOn.

So how can I get “main.kts” scratch files / scratch files in which I call pull dependencies to work?

1 Like

“main.kts” scripts are not supported in scratches yet.

But they are supported in InetlliJ as regular source files, including all completion/navigation with the dependencies declared in @DependsOn. (the @Import annotation is not supported yet though).
Just create a .main.kts file outside project source directories, and editing part should work fine.

2 Likes

The “outside” was the important bit, thanks. I first had added my .main.kts file as a resource in an existing Kotlin project, and that did not work.

We have an excuse here - when a source - including script one - is a part of a module sources, its dependencies are processed according to the module settings. But of course we need to make it more obvious.

1 Like

@ilya.chernikov any plans to add completion/navigation support to the @Import annotation? My scripts re-use code of each other, and the lack of proper highlighting/completion/navigation for the imported bits of code makes main.kts scripts much less appealing, than they initially looked like.