ziggystar
ziggystar

Reputation: 28690

Different compile options for tests and release in SBT?

I have a project where I need to disable assertions when creating the binaries. Now I could just do:

scalacOptions += "-Xdisable-assertions"

But then also the unit tests would be run without assertions. Is there a (hopefully) simple way to achieve what I need?

Upvotes: 2

Views: 754

Answers (1)

Jacek Laskowski
Jacek Laskowski

Reputation: 74669

How do you create the binaries? What task/command do you use?

Use the task as the scope for scalacOptions to have different values for them. See Scoping by configuration axis:

By default, all the keys associated with compiling, packaging, and running are scoped to a configuration and therefore may work differently in each configuration. The most obvious examples are the task keys compile, package, and run; but all the keys which affect those keys (such as sourceDirectories or scalacOptions or fullClasspath) are also scoped to the configuration.

Use inspect when in doubt.

> inspect scalacOptions
[info] Task: scala.collection.Seq[java.lang.String]
[info] Description:
[info]  Options for the Scala compiler.
[info] Provided by:
[info]  {file:/C:/dev/sandbox/task-dependsOn/}task-dependson/compile:scalacOptions
[info] Defined at:
[info]  (sbt.Classpaths) Defaults.scala:1424
[info] Dependencies:
[info]  task-dependson/compile:autoCompilerPlugins
[info]  task-dependson/compile:settingsData
[info]  task-dependson/compile:update
[info]  task-dependson/compile:buildDependencies
[info]  task-dependson/compile:thisProjectRef
[info] Delegates:
[info]  task-dependson/compile:scalacOptions
[info]  task-dependson/*:scalacOptions
[info]  {.}/compile:scalacOptions
[info]  {.}/*:scalacOptions
[info]  */compile:scalacOptions
[info]  */*:scalacOptions
[info] Related:
[info]  b/compile:scalacOptions
[info]  b/test:scalacOptions
[info]  task-dependson/test:scalacOptions
[info]  task-dependson/jacoco:scalacOptions
[info]  a/jacoco:scalacOptions
[info]  */*:scalacOptions
[info]  a/test:scalacOptions
[info]  a/compile:scalacOptions
[info]  b/jacoco:scalacOptions

The Compile configuration scope is the default one (see show defaultConfiguration for a project) so scalacOptions += "-Xdisable-assertions" is in fact scalacOptions in Compile += "-Xdisable-assertions". Use different configuration, say Test, and you'll get different results.

There's however a hitch in SBT (I missed the very first time I responded) - settings are chained and when a setting is not defined in a scope, it gets its value from a more general scope. When I said, scalacOptions +=... is in fact scalacOptions in Compile I missed the important feature of settings - scalacOptions is global while scalacOptions in Compile is Compile-scoped.

Upvotes: 1

Related Questions