Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sbt: set specific scalacOptions options when compiling tests

Tags:

scala

sbt

Normally I use this set of options for compiling Scala code:

scalacOptions ++= Seq(
    "-deprecation",
    "-encoding", "UTF-8",
    "-feature",
    "-unchecked",
    "-language:higherKinds",
    "-language:implicitConversions",
    "-Xfatal-warnings",
    "-Xlint",
    "-Yinline-warnings",
    "-Yno-adapted-args",
    "-Ywarn-dead-code",
    "-Ywarn-numeric-widen",
    "-Ywarn-value-discard",
    "-Xfuture",
    "-Ywarn-unused-import"
)

But some of them don't play well with ScalaTest, so I would like to disable -Ywarn-dead-code and -Ywarn-value-discard when compiling tests.

I tried adding scope like this

scalacOptions in Compile ++= Seq(...)

or

scalacOptions in (Compile, compile) ++= Seq(...)

or even

val ignoredInTestScalacOptions = Set(
    "-Ywarn-dead-code",
    "-Ywarn-value-discard"
)

scalacOptions in Test ~= { defaultOptions =>
  defaultOptions filterNot ignoredInTestScalacOptions
}

but the first two disable options for normal compile scope as well while the latter doesn't affect tests compilation options.

How could I have a separate list of options when compiling tests?

like image 730
Tvaroh Avatar asked Jun 04 '16 12:06

Tvaroh


2 Answers

Had the same issue, @Mike Slinn answer didn't work for me. I believe the test options extend the compile options? What eventually did the trick was explicitly removing ignored options from test

scalacOptions in Test --= Seq( "-Ywarn-dead-code", "-Ywarn-value-discard")

like image 194
LiorH Avatar answered Oct 02 '22 09:10

LiorH


Why not define common options in a variable (which I called sopts), and other options in another variable (which I called soptsNoTest)?

val sopts = Seq(
  "-deprecation",
  "-encoding", "UTF-8",
  "-feature",
  "-target:jvm-1.8",
  "-unchecked",
  "-Ywarn-adapted-args",
  "-Ywarn-numeric-widen",
  "-Ywarn-unused",
  "-Xfuture",
  "-Xlint"
)
val soptsNoTest = Seq(
  "-Ywarn-dead-code",
  "-Ywarn-value-discard"
)

scalacOptions in (Compile, doc) ++= sopts ++ soptsNoTest
scalacOptions in Test ++= sopts

Tested with SBT 0.13.13.

Because this question has been unanswered for so long, and Scala 2.12 and 2.12.1 were released in the interim, I modified the common options to suit.

BTW, I do not experience any problem with ScalaTest using the switches you mention. I only answered this question because it was interesting.

like image 21
Mike Slinn Avatar answered Oct 02 '22 10:10

Mike Slinn