Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In sbt, how do you override scalacOptions for console in all configurations?

Tags:

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

  • provided by compile:console
  • depends on 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

  • provided by compile:scalacOptions
  • doesn't reach *:console::scalacOptions (which is what I defined) in the delegation chain

My 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.

like image 817
Dale Wijnand Avatar asked Nov 14 '14 22:11

Dale Wijnand


2 Answers

My question is how do I override scalacOptions for 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.

like image 53
Eugene Yokota Avatar answered Oct 14 '22 03:10

Eugene Yokota


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")) } 
like image 39
Jeffrey Aguilera Avatar answered Oct 14 '22 05:10

Jeffrey Aguilera