9

I just switched to SBT 0.13.1 and either arg handling changed or there's a bug. Here's what I'm testing with (named sbt-test.scala):

#!/bin/sh
SBT_0_13_0="/path/to/sbt-launch-0.13.0.jar"
SBT_0_13_1="/path/to/sbt-launch-0.13.1.jar"
SBT="$SBT_0_13_1"
exec java $JAVA_OPTS -Dsbt.main.class=sbt.ScriptMain -jar "$SBT" $0 "$@"
!#

/***
scalaVersion := "2.10.3"

libraryDependencies ++= Seq(
  "com.typesafe" % "config" % "1.0.0"
)
*/

import com.typesafe.config.{ConfigFactory, Config}

println(s"Args: ${args mkString ", "}")

val cfg = ConfigFactory.parseString(
  """
    |credentials {
    |  user = someone
    |  pass = s3cr3t
    |}
  """.stripMargin)

println(cfg getString "credentials.user")

When I run ./sbt-test.scala --arg=val I get the following error:

[error] Expected ID character
[error] Not a valid command: arg
[error] arg=val

Running the same script with the same argument with SBT 0.13.0 yields the expected:

Args: --arg=val
someone

Similarly, if I turn that executable script into an actual SBT project and attempt to run sbt run --flag I get

[error] Not a valid command: flag (similar: iflast, last, alias)
[error] flag
Andrey
  • 8,882
  • 10
  • 58
  • 82

1 Answers1

18

See the documentation on Batch mode:

Batch mode

You can also run sbt in batch mode, specifying a space-separated list of sbt commands as arguments. For sbt commands that take arguments, pass the command and arguments as one argument to sbt by enclosing them in quotes. For example,

$ sbt clean compile "testOnly TestA TestB"

In this example, testOnly has arguments, TestA and TestB. The commands will be run in sequence (clean, compile, then testOnly).

So if you don't want --flag to be interpreted as a command, you have to quote it with run:

$ sbt "run --flag"

sbt script runner

The scripting is described in Scripts, REPL, and Dependencies, which seems to say you don't need extra quotes other than the bash quotes:

java -Dsbt.main.class=sbt.ScriptMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"

In reality, to make your script work, you'd need another sets of escaped quotes around $@ like this:

exec java $JAVA_OPTS -Dsbt.main.class=sbt.ScriptMain -jar "$SBT" $0 "\"$@\""

If this is a regression that started in 0.13.1, you should report to sbt.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • what about getting it to run as a script? this is the main focus of the question. – Andrey Dec 15 '13 at 06:28
  • double quoting only makes it work for a single arg. but if i have something like ./sbt-test.scala --flag --arg=val i get an error on the second arg. i'm going to report it as a bug – Andrey Dec 15 '13 at 16:50