如何覆盖控制台的scalacOptions?

时间:2017-05-22 03:01:00

标签: scala sbt

根据以下讨论,覆盖scalacOptions SBT控制台对我不起作用:In sbt, how do you override scalacOptions for console in all configurations?

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: _*)

当我运行SBT会话时,我发现-Ywarn-unused-imports选项不属于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

但是,尝试在我的某个源代码文件中使用未使用的导入运行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

似乎错误来自compile:compileIncremental。当我显示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

现在,如果我检查上述内容,它似乎由compile:scalacOptions提供。我感到困惑的是为什么compile:console::scalacOptions中的编译器选项在运行console命令时似乎没有使用。

我希望能够为console运行一套不太严格的编译器选项,这种做法似乎相当简单。但是,我很难理解为什么现有的解决方案不再有效。任何人都可以帮助我直接在这里吗?

2 个答案:

答案 0 :(得分:0)

首先,scalacOptions in (Compile, console)表示设置scalacOptions以编译您在控制台中输入的 scala

由于您的编译错误,这是由于您转到 Scala控制台之前,编译器将首先尝试编译当前项目并且由于您为编译设置了scalacOptions,因此错误已经抛出。

答案 1 :(得分:0)

以下在SBT 1.3.2中对我有利:

unsigned Vision::calc_disp_map(
    const Flattened2DArray<int>& left_block_sums, 
    const Flattened2DArray<int>& right_block_sums, 
    Flattened2DArray<unsigned char>& disp_map, //out
    int block_size, 
    int search_length)
{
    for (int x = 0; x < disp_map.width(); x++) {
        for (int y = 0; y < disp_map.height(); y++) {
            int min_distance = 0;
            int lowest_diff = -1;
            for (int i = 0; i < search_length; i++) {
                int target_x = x + (i - search_length / 2);
                if (target_x < 0) {
                    target_x = 0;
                }
                else if (target_x >= right_block_sums.width()) {
                    target_x = right_block_sums.width() - 1;
                }
                int diff = abs(left_block_sums(x, y) - right_block_sums(target_x, y));
                if (lowest_diff == -1 || diff < lowest_diff) {
                    lowest_diff = diff;
                    min_distance = i;
                }
            }
            unsigned char disparity = ((float)min_distance / search_length) * 255;
            disp_map.set(x, y, disparity);
        }
    }
    return 0;
}