Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scalafixEnable: relax -Xfatal-warnings for scalafix invocations #195

Merged
merged 2 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 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,16 @@ 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(_.key == scalafix.key))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also check for scalafixAll?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! just added 9c91d70

}
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/scalafix 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