23

So for example you couldn't write:

a := 3
b := 4

You had to separate them, like this:

a := 3

b := 4

I can't think of another file format with a similar requirement. It was definitely surprising to newcomers. Even some longtime sbt users considered it objectionable.

In my experience, this was the single frequently-most-asked question about sbt, before it was changed.

4

1 Answer 1

26

As of sbt 0.13.7, the blank lines are no longer required. So the following is now historical information.

The short answer is the blank lines make the file machine-readable and machine-writable.

Let's unpack this.

First, note that it's common for settings to use multiple lines, for example a long Seq, or a task body containing code.

Second, note that settings are expressions, not statements. So sbt can't infer semicolons between them. In Scala, semicolons are only inferred between statements.

So the blank lines are needed so that sbt knows where one setting ends and the next begins.

In theory, could it determine that anyway? Perhaps. In 2011 Mark Harrah (the creator of sbt) wrote:

using the compiler to separate expressions [...] may happen at some point, but I try to avoid introducing the overhead of starting up a compiler wherever possible

So it turns out that a key concern here is performance. The Scala compiler is notoriously slow. You might next wonder, well, the settings are going to need to be compiled anyway, aren't they? Yes, but few people realize that to improve performance, each setting in an .sbt file is separately compiled and the result of compilation cached, even between invocations of sbt. So if you edit your build and change one setting, only that one setting is recompiled.

Above, I said that .sbt files are machine-writable, not just machine-readable. You can change settings on the fly and then save your changes with session save. In 2014 Jason Zaugg (of Typesafe) wrote:

The decision [on blank lines] originally came from the constraint that build.sbt should be machine editable. In an SBT session, you can run set foo := bar, and that will be persisted into the file.

In practise, people don't tend to use this feature that much, and we might revisit this decision [...]

For further details on the design considerations behind the blank lines and how this might change in the future, see this writeup by Josh Suereth (also of Typesafe).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.