Getting started with scala-cli

Following a discussion in another thread, I’ve decided to give scala-cli a try, and I’m experimenting with moving an existing sbt project to scala-cli. I’ve been struggling with one thing: What’s the equivalent of sbt’s Test/runMain? I currently use it to start a customized Scalatest runner (to grade students’ work), and I’d rather not have to change my workflow too much.

EDIT

Here’s my workaround so far:

java -cp $(scala-cli compile --test . -p) Grade

It kind of works, but shows some downloading messages with every run:

Downloading https://repo1.maven.org/maven2/com/github/charpov/tiny-scala-utils-test_3/
Downloading https://charpov.github.io/TinyScalaUtils/maven/com/github/charpov/tiny-scala-utils-test_3/
Downloaded https://charpov.github.io/TinyScalaUtils/maven/com/github/charpov/tiny-scala-utils-test_3/
Downloaded https://repo1.maven.org/maven2/com/github/charpov/tiny-scala-utils-test_3/
Downloading https://charpov.github.io/TinyScalaUtils/maven/com/github/charpov/tiny-scala-utils_3/
Downloading https://repo1.maven.org/maven2/com/github/charpov/tiny-scala-utils_3/
Downloaded https://charpov.github.io/TinyScalaUtils/maven/com/github/charpov/tiny-scala-utils_3/
Downloaded https://repo1.maven.org/maven2/com/github/charpov/tiny-scala-utils_3/

I’m using two small files for a test:

Run.scala:

//> using repository "https://charpov.github.io/TinyScalaUtils/maven/"
//> using dep com.github.charpov::tiny-scala-utils:1.1.1
//> using test.dep com.github.charpov::tiny-scala-utils-test:1.1.1
//> using scala 3.3

@main def Run(): Unit = tinyscalautils.text.info()

Grade.test.scala:

@main def Grade(): Unit = println("100/100")
1 Like

It doesn’t look like there is a command that works, however, if you are using Metals in VS Code then a main method in a file in the test scope should still get the run | debug buttons above the method, this isn’t something you can do from the command line which is annoying I know.

Edit: I’ve now opened an issue for this There is no command to run a main method in test scope · Issue #2728 · VirtusLab/scala-cli · GitHub

3 Likes

I write grading tests with Scalatest (using a few extensions to assign weights and timeouts to tests) and I run them through a simple runner that adds points and produces a final grade. I obviously rely on scripts to run an entire course assignment (and we’re discussing a tool with CLI in its name :wink: ) .

Thanks for starting the issue. It feels like something that shouldn’t be too hard to implement (there’s already a way to specify a “test” scope for compile, so why not for run?)

Do you know why starting java with the full class path triggers these downloading messages?

Is this relevant? Migrating from the old Scala runner | Scala CLI

I never used the old runner. I usually don’t even bother installing it on my machines and let sbt handle everything.

It’s not directly related to your question, but I’ve migrated some project from sbt to scala-cli and, using vscode + metals, I’ve found that compilation, execution times are much longer.

Is your experience similar?

Thanks.

Not my experience, I also migrated a mid-size project (about 500 source files) from sbt to scala-cli and compilation times are equal or slightly faster. I guess it depends on the size and libraries of the project; maybe sbt has better parallel compilation or something?

Thanks !! I’ll test it more thoroughly.