From 7002cd940434120f73385d322a28d54c994d0955 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Fri, 21 Sep 2018 11:50:52 -0700 Subject: Mark fatal warnings as such --- src/main/scala/xyz.driver.sbt/SbtSettings.scala | 30 +++++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/scala/xyz.driver.sbt/SbtSettings.scala b/src/main/scala/xyz.driver.sbt/SbtSettings.scala index dc336b6..311e976 100644 --- a/src/main/scala/xyz.driver.sbt/SbtSettings.scala +++ b/src/main/scala/xyz.driver.sbt/SbtSettings.scala @@ -91,18 +91,34 @@ object SbtSettings extends AutoPlugin { "-Ywarn-unused:_,-explicits,-implicits" ) }, + // Currently, scalac does not provide a way to fine-tune the treating of + // warnings as errors. Either all are considered errors + // (with -Xfatal-warnings), or none are. This hack analyzes the compiler's + // output and treats all warnings as errors, except for deprecations. compile in Compile := { + val log = streams.value.log val compiled = (compile in Compile).value val problems = compiled.readSourceInfos().getAllSourceInfos.asScala.flatMap { - case (_, info) => - info.getReportedProblems + case (_, info) => info.getReportedProblems } - - val deprecationsOnly = problems.forall { problem => - problem.message().contains("is deprecated") + var deprecationsOnly = true + problems.foreach { + problem => + if (!problem.message().contains("is deprecated")) { + deprecationsOnly = false + val pos = problem.position + val file = pos.sourcePath.asScala.getOrElse("?") + val line = pos.line.asScala.map(_.toString).getOrElse("?") + val col = pos.pointer.asScala.map(_.toString).getOrElse("?") + val msg = problem.message + val desc = pos.lineContent() + "\n" + pos.pointerSpace.asScala + .getOrElse("") + "^" + log.error(s"[fatal warning] $file:$line:$col $msg\n$desc") + } + } + if (!deprecationsOnly) { + throw new MessageOnlyException("Fatal warnings: some warnings other than deprecations were found.") } - - if (!deprecationsOnly) sys.error("Fatal warnings: some warnings other than deprecations were found.") compiled } ) -- cgit v1.2.3