简体   繁体   English

如何覆盖控制台的scalacOptions?

[英]How do I override scalacOptions for console?

Overriding scalacOptions for the SBT console does not work for me, based on the discussion at: In sbt, how do you override scalacOptions for console in all configurations? 根据以下讨论,覆盖SBT控制台的scalacOptions对我不起作用: 在sbt中,如何在所有配置中覆盖控制台的scalacOptions?

The contents of my build.sbt are as follows: 我的build.sbt的内容如下:

    lazy val commonScalacOptions = Seq(
      "-deprecation",
      "-encoding", "UTF-8",
      "-feature",
      "-unchecked",
      "-Xfatal-warnings",
      "-Xlint:-missing-interpolator,_",
      "-Yinline-warnings",
      "-Yno-adapted-args",
      "-Ywarn-dead-code",
      "-Ywarn-numeric-widen",
      "-Ywarn-value-discard",
      "-Ywarn-unused-import",
      "-language:existentials",
      "-language:experimental.macros",
      "-language:implicitConversions",
      "-language:higherKinds",
      "-Xfuture")

    lazy val commonSettings = Seq(
      scalaVersion := "2.11.11",
      scalacOptions ++= commonScalacOptions,
      scalacOptions in (Compile, console)  := (scalacOptions in (Compile, console)).value filterNot ("-Ywarn-unused-import" == _),
      scalacOptions in (Test, console)     := (scalacOptions in (Compile, console)).value)

    lazy val root = Project("brontosaurus-rex", file("." + "brontosaurus-rex")).in(file("."))
      .settings(commonSettings: _*)

and when I run an SBT session, I can see that the -Ywarn-unused-imports option is not part of compile:console::scalacOptions 当我运行SBT会话时,我可以看到-Ywarn-unused-imports选项不是compile:console::scalacOptions一部分compile:console::scalacOptions

    > show compile:console::scalacOptions
    [info] * -deprecation
    [info] * -encoding
    [info] * UTF-8
    [info] * -feature
    [info] * -unchecked
    [info] * -Xfatal-warnings
    [info] * -Xlint:-missing-interpolator,_
    [info] * -Yinline-warnings
    [info] * -Yno-adapted-args
    [info] * -Ywarn-dead-code
    [info] * -Ywarn-numeric-widen
    [info] * -Ywarn-value-discard
    [info] * -language:existentials
    [info] * -language:experimental.macros
    [info] * -language:implicitConversions
    [info] * -language:higherKinds
    [info] * -Xfuture

However, attempting to run console with an unused import in one of my source code files, results in the unexpected error: 但是,尝试在我的一个源代码文件中运行带有未使用导入的console导致意外错误:

    > console
    [info] Compiling 1 Scala source to [PROJECT_DIR]/brontosaurus-rex/target/scala-2.11/classes...
    [error] [PROJECT_DIR]/brontosaurus-rex/src/main/scala/com/dinosaurs/BrontosaurusRex.scala:3: Unused import
    [error] import scala.util.matching.Regex
    [error]                            ^
    [error] one error found
    [error] (compile:compileIncremental) Compilation failed
    [error] Total time: 0 s, completed May 21, 2017 7:45:53 PM

It seems that the error is coming from compile:compileIncremental . 似乎错误来自compile:compileIncremental When I show the contents of compile:compileIncremental::scalacOptions I see the option that I'm trying to filter out: 当我显示compile:compileIncremental::scalacOptions的内容时compile:compileIncremental::scalacOptions我看到我试图过滤掉的选项:

    > show compile:compileIncremental::scalacOptions
    [info] * -deprecation
    [info] * -encoding
    [info] * UTF-8
    [info] * -feature
    [info] * -unchecked
    [info] * -Xfatal-warnings
    [info] * -Xlint:-missing-interpolator,_
    [info] * -Yinline-warnings
    [info] * -Yno-adapted-args
    [info] * -Ywarn-dead-code
    [info] * -Ywarn-numeric-widen
    [info] * -Ywarn-value-discard
    [info] * -Ywarn-unused-import
    [info] * -language:existentials
    [info] * -language:experimental.macros
    [info] * -language:implicitConversions
    [info] * -language:higherKinds
    [info] * -Xfuture

Now, if I inspect the above, it appears to be provided by compile:scalacOptions . 现在,如果我检查上面的内容,它似乎是由compile:scalacOptions提供的。 What I'm confused about is why the compiler options in compile:console::scalacOptions don't appear to be used when running the console command. 我很困惑的是为什么在运行console命令时似乎没有使用compile:console::scalacOptions中的compile:console::scalacOptions器选项。

I'd like to be able to run a less strict set of compiler options for console and this way of doing it seems fairly straightforward. 我希望能够为console运行一组不太严格的编译器选项,这种做法似乎相当简单。 However, I'm stumped as to why the existing solution no longer works. 但是,我很难过为什么现有的解决方案不再适用。 Can anyone help set me straight here? 任何人都可以帮助我直接在这里?

First, scalacOptions in (Compile, console) it means setting scalacOptions to compile scala that you input in the Console . 首先, scalacOptions in (Compile, console) scalacOptions意味着设置scalacOptions来编译您在控制台中输入的scala

As the your compile error , this is caused by before you go to the Scala console , the compiler will firstly try to compile current project and since you have setted scalacOptions for compile , so this error has throwed. 作为您的编译错误 ,这是由于在您转到Scala控制台之前 ,编译器将首先尝试编译当前项目,并且因为您已经为编译设置了scalacOptions ,所以此错误已经抛出。

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

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