Skip to content

Commit

Permalink
Merge pull request #195 from bjaglin/fatal-warns
Browse files Browse the repository at this point in the history
scalafixEnable: relax -Xfatal-warnings for scalafix invocations
  • Loading branch information
github-brice-jaglin authored Mar 4, 2021
2 parents e311d0b + 9c91d70 commit 1eeafe2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
39 changes: 32 additions & 7 deletions src/main/scala/scalafix/sbt/ScalafixEnable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalafix.sbt
import sbt._
import sbt.Keys._
import sbt.internal.sbtscalafix.Compat
import ScalafixPlugin.autoImport._

/** Command to automatically enable semanticdb-scalac for shell session */
object ScalafixEnable {
Expand Down Expand Up @@ -36,18 +37,29 @@ object ScalafixEnable {
"Configure libraryDependencies, scalaVersion and scalacOptions for scalafix.",
detail = """1. enables the semanticdb-scalac compiler plugin
|2. sets scalaVersion to latest Scala version supported by scalafix
|3. add -Yrangepos to scalacOptions""".stripMargin
|3. add -Yrangepos to scalacOptions
|4. relax -Xfatal-warnings, -Werror & -Wconf* (if set in scalacOptions) for upcoming scalafix invocations""".stripMargin
) { s =>
val extracted = Project.extract(s)
val settings: Seq[Setting[_]] = for {
(p, fullVersion) <- projectsWithMatchingScalaVersion(s)
isEnabled =
relaxScalacForScalafix <- List(
scalacOptions.in(p) := {
val options = scalacOptions.in(p).value
if (!scalafixInvoked.value) options
else
options.filterNot { option =>
List("-Xfatal-warnings", "-Werror").contains(option) ||
option.startsWith("-Wconf")
}
}
)
isSemanticdbEnabled =
libraryDependencies
.in(p)
.get(extracted.structure.data)
.exists(_.exists(_.name == "semanticdb-scalac"))
if !isEnabled
setting <- List(
addSemanticdb <- List(
scalaVersion.in(p) := fullVersion,
scalacOptions.in(p) ++= List(
"-Yrangepos",
Expand All @@ -57,10 +69,23 @@ object ScalafixEnable {
ScalafixPlugin.autoImport.scalafixSemanticdb
)
)
} yield setting
settings <-
relaxScalacForScalafix ++
(if (!isSemanticdbEnabled) addSemanticdb else List())
} yield settings

val semanticdbInstalled = Compat.append(extracted, settings, s)
val scalafixReady = Compat.append(extracted, settings, s)

semanticdbInstalled
scalafixReady
}

private def scalafixInvoked: Def.Initialize[Task[Boolean]] =
Def.task {
executionRoots.value.exists { root =>
Seq(
scalafix.key,
scalafixAll.key
).contains(root.key)
}
}
}
11 changes: 10 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixEnable/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ lazy val scala211 = project.settings(

// 2.12.x is supported
lazy val scala212 = project.settings(
scalaVersion := V.scala212
scalaVersion := V.scala212,
scalacOptions ++= Seq(
// generate errors on unused imports
"-Xfatal-warnings",
"-Ywarn-unused",
// generate errors on procedure syntax
"-Wconf:cat=deprecation:e",
"-Xfuture",
"-deprecation"
)
)

// 2.13.x is supported
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object UnusedImportAndProcedureSyntax {
import java.util.Calendar
def main {}
}
13 changes: 13 additions & 0 deletions src/sbt-test/sbt-scalafix/scalafixEnable/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
-> check
> scalafixEnable
> check

# we have 2 fatal warnings preventing compilation
-> scala212/compile

# ensure that a semantic rule can be ran despite warnings, to fix one of the warning
> scala212/scalafixAll RemoveUnused

# but that -Xfatal-warnings remains honored for regular compilation
-> scala212/compile

# confirm that compilation succeeds after fixing the last warning
> scala212/scalafix ProcedureSyntax
> scala212/compile

0 comments on commit 1eeafe2

Please sign in to comment.