简体   繁体   English

在sbt中,如何在所有配置中覆盖控制台的scalacOptions?

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

I like defining scalacOptions at the top level like so (as an example, ignoring project axis for now): 我喜欢在顶层定义scalacOptions ,如此(例如, scalacOptions忽略项目轴):

scalacOptions += "-Ywarn-unused-import"

But then I realised that's too strict for console . 但后来我意识到这对console来说太严格了。 So I tried setting: 所以我试着设置:

scalacOptions in console ~= (_ filterNot (_ == "-Ywarn-unused-import"))

But that didn't work (still got (fatal) warnings in the REPL). 但这不起作用(在REPL中仍然有致命的警告)。

I used inspect to try and understand why: 我用inspect来试着理解为什么:

> 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 注意: console

  • provided by compile:console compile:console提供compile:console
  • depends on compile:console::scalacOptions 取决于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:console::scalacOptions

  • provided by compile:scalacOptions compile:scalacOptions提供compile:scalacOptions
  • doesn't reach *:console::scalacOptions (which is what I defined) in the delegation chain 在委托链中没有达到*:console::scalacOptions (这是我定义的)

My question is how do I override scalacOptions for all configurations for console? 我的问题是如何为控制台的所有配置覆盖scalacOptions 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. 我想避免scalacOptions in (Compile, console)设置scalacOptions in (Compile, console) (因为它会被复制(Test, console) )或定义一个scalac选项的val。

My question is how do I override scalacOptions for all configurations for console? 我的问题是如何为控制台的所有配置覆盖scalacOptions

I don't think we can given the presence of compile:scalacOptions provided by sbt's Defaults . 我不认为我们可以提供compile:scalacOptions由sbt的Defaults The only scope that has higher precedence is compile:console::scalacOptions . 唯一具有更高优先级的范围是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. 在大多数情况下,人们不希望CompileTest设置跨线,因此配置范围更高的优先级我不认为是一个错误的默认值。

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. BuildStructure中有一个delegates函数的单个实例,它在加载时初始化并用于所有任务。 The ordering is done in Scope.delegates . 订购在Scope.delegates中完成。

I fix the bad scalac options in an autoplugin: 我在autoplugin中修复了坏的scalac选项:

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"))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM