I like defining scalacOptions at the top level like so (as an example, ignoring project axis for now):
scalacOptions += "-Ywarn-unused-import" But then I realised that's too strict for console. So I tried setting:
scalacOptions in console ~= (_ filterNot (_ == "-Ywarn-unused-import")) But that didn't work (still got (fatal) warnings in the REPL).
I used inspect to try and understand why:
> inspect console [info] Task: Unit [info] Description: [info] Starts the Scala interpreter with the project classes on the classpath. [info] Provided by: [info] {file:/a/}b/compile:console [info] Defined at: [info] (sbt.Defaults) Defaults.scala:261 [info] Dependencies: [info] compile:console::compilers [info] compile:console::initialCommands [info] compile:console::fullClasspath [info] compile:console::taskTemporaryDirectory [info] compile:console::scalaInstance [info] compile:console::streams [info] compile:console::cleanupCommands [info] compile:console::scalacOptions [info] Delegates: [info] compile:console [info] *:console [info] {.}/compile:console [info] {.}/*:console [info] */compile:console [info] */*:console [info] Related: [info] test:console Note: console is
compile:console compile:console::scalacOptions then:
> inspect compile:console::scalacOptions [info] Task: scala.collection.Seq[java.lang.String] [info] Description: [info] Options for the Scala compiler. [info] Provided by: [info] {file:/a/}b/compile:scalacOptions [info] Defined at: [info] (sbt.Classpaths) Defaults.scala:1593 [info] Reverse dependencies: [info] compile:console [info] Delegates: [info] compile:console::scalacOptions [info] compile:scalacOptions [info] *:console::scalacOptions [info] *:scalacOptions [info] {.}/compile:console::scalacOptions [info] {.}/compile:scalacOptions [info] {.}/*:console::scalacOptions [info] {.}/*:scalacOptions [info] */compile:console::scalacOptions [info] */compile:scalacOptions [info] */*:console::scalacOptions [info] */*:scalacOptions [info] Related: [info] *:console::scalacOptions [info] compile:scalacOptions [info] *:scalacOptions [info] */*:scalacOptions [info] test:scalacOptions Note: compile:console::scalacOptions is
compile:scalacOptions *:console::scalacOptions (which is what I defined) in the delegation chainMy question is how do I override scalacOptions for all configurations for console? Is it possible to change the delegation chain?
I'd like to avoid having to set scalacOptions in (Compile, console) (as it would be duplicated for (Test, console)) or define a val of scalac options.
My question is how do I override
scalacOptionsfor all configurations for console?
I don't think we can given the presence of compile:scalacOptions provided by sbt's Defaults. The only scope that has higher precedence is compile:console::scalacOptions. In most cases one would not want Compile and Test settings to cross wire, so configuration scoping higher precedence I don't think is a bad default.
lazy val commonSettings = Seq( scalaVersion := "2.11.4", scalacOptions += "-Ywarn-unused-import", scalacOptions in (Compile, console) ~= (_ filterNot (_ == "-Ywarn-unused-import")), scalacOptions in (Test, console) := (scalacOptions in (Compile, console)).value ) Is it possible to change the delegation chain?
No, this is not possible. There's a single instance of delegates function in BuildStructure, and it's initialized at the loading time and used for all tasks. The ordering is done in Scope.delegates.
I fix the bad scalac options in an autoplugin:
package console import sbt._ /** [[FixScalacOptionsInConsole]] is an [[AutoPlugin]] that removes * noisy or unnecessary scalac options when running an sbt console. */ object FixScalacOptionsInConsole extends AutoPlugin { import Keys._ override def requires = plugins.JvmPlugin override def trigger = allRequirements override lazy val projectSettings = Seq( Compile / console / scalacOptions ~= filter, Test / console / scalacOptions ~= filter ) def filter: Seq[String] => Seq[String] = _ .filterNot(_ == "-feature") .filterNot(_.startsWith("-opt:")) .filterNot(_ == "-unchecked") .filterNot(_.startsWith("-Xlint:")) .filterNot(_ == "-Xfatal-warnings") .filterNot(_.startsWith("-Ywarn")) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With