Skip to content

Commit

Permalink
add --triggered support to scalafixOnCompile
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukeoe committed Apr 18, 2021
1 parent d805fd6 commit 1407d55
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Expand Up @@ -45,7 +45,8 @@ object ScalafixPlugin extends AutoPlugin {

val scalafixOnCompile: SettingKey[Boolean] =
settingKey[Boolean](
"Run Scalafix rule(s) declared in .scalafix.conf on compilation and fail on lint errors. " +
"Run Scalafix rule(s) declared in .scalafix.conf on compilation and fail on lint errors," +
"with overlaying configuration by `triggered` sections if defined in .scalafix.conf file." +
"Off by default. When enabled, caching will be automatically activated, " +
"but can be disabled with `scalafixCaching := false`."
)
Expand Down Expand Up @@ -107,7 +108,7 @@ object ScalafixPlugin extends AutoPlugin {
compile.value // evaluated first, before the potential scalafix evaluation
if (scalafixOnCompile.value)
scalafix
.toTask("")
.toTask(" --triggered")
.map(_ => oldCompile)
else Def.task(oldCompile)
}.value,
Expand Down Expand Up @@ -529,13 +530,29 @@ object ScalafixPlugin extends AutoPlugin {
// custom tool classpaths might contain directories for which we would need to stamp all files, so
// just disable caching for now to keep it simple and to be safe
throw StampingImpossible
case "--triggered" =>
// If there is a triggered section in a default config file, --triggered flag should be accounted.
// If not, --triggered can share the same cache.
checkIfTriggeredSectionExists
case _ => true
}
write(cacheKeys)
case Arg.NoCache =>
throw StampingImpossible
}

private lazy val checkIfTriggeredSectionExists: Boolean = {
val confInArgs = interface.args
.collect { case Arg.Config(conf) => conf }
.flatten
.lastOption
val configFile = confInArgs.fold(file(".scalafix.conf"))(_.toFile())
if (configFile.exists()) {
val lines = IO.readLines(configFile)
lines.exists(_.startsWith("triggered"))
} else false
}

def stampFile(file: File): String = {
// ensure the file exists and is not a directory
if (file.isFile)
Expand Down
9 changes: 8 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/.scalafix.conf
@@ -1,2 +1,9 @@
rules = [DisableSyntax, RemoveUnused]
DisableSyntax.noNulls = true
DisableSyntax.noNulls = true

DisableSyntax.noVars = true
RemoveUnused.privates = false
triggered = {
DisableSyntax.noVars = false
RemoveUnused.privates = true
}
@@ -0,0 +1,4 @@
object Variables {
private var a = 1
def b: Int = return 2
}
7 changes: 6 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/build.sbt
Expand Up @@ -5,7 +5,7 @@ inThisBuild(
scalaVersion := Versions.scala212,
scalacOptions ++= List(
"-Yrangepos",
"-Ywarn-unused-import"
"-Ywarn-unused"
)
)
)
Expand All @@ -21,3 +21,8 @@ lazy val rewrite = project
addCompilerPlugin(scalafixSemanticdb)
)
.settings(scalafixConfigSettings(IntegrationTest): _*)

lazy val args = project
.settings(
addCompilerPlugin(scalafixSemanticdb)
)
9 changes: 8 additions & 1 deletion src/sbt-test/sbt-scalafix/scalafixOnCompile/test
Expand Up @@ -23,4 +23,11 @@

# check configuration granularity for scalafixOnCompile
> set scalafixOnCompile.in(lint, Test) := false
> lint/test:compile
> lint/test:compile

# check that triggered rules are respected on compilation, and ignored on explicit invocation.
# `private var` is detected by `DisableSyntax.noVars = true` on explicit invocation,
# and removed by `triggered.RemoveUnused.privates = true` on compilation.
-> args/test:scalafix
> args/test:compile
> args/test:scalafix
@@ -0,0 +1,4 @@
rules = [DisableSyntax]
DisableSyntax.noVars = true

triggered.rules = [ProcedureSyntax]
48 changes: 48 additions & 0 deletions src/sbt-test/skip-windows/caching/test
Expand Up @@ -106,6 +106,54 @@ $ exec chmod 000 src/main/scala/InitiallyValid.scala
-> scalafix --check ProcedureSyntax
$ delete src/main/scala

# files should be re-checked in scalafix run without --triggered even after scalafix runs onCompile, if a triggered configuration is defined
> set scalafixConfig := Some(file("files/Triggered.scalafix.conf"))
> set scalafixOnCompile := true
$ mkdir src/main/scala
$ copy-file files/Valid.scala src/main/scala/Valid.scala
> compile
$ exec chmod 000 src/main/scala/Valid.scala
> scalafix --triggered --check
-> scalafix --check
> set scalafixOnCompile := false
$ delete src/main/scala

# files should be re-checked in scalafix --triggered run even after scalafix run without --triggered, if a triggered configuration is defined
> set scalafixConfig := Some(file("files/Triggered.scalafix.conf"))
$ mkdir src/main/scala
$ copy-file files/Valid.scala src/main/scala/Valid.scala
> scalafix
$ exec chmod 000 src/main/scala/Valid.scala
> scalafix --check
-> scalafix --triggered --check
$ delete src/main/scala

# files should not be re-checked in scalafix explicit invocation after scalafix runs onCompile if any triggered configuration is not defined
> set scalafixConfig := Some(file("files/DisableSyntaxVar.scalafix.conf"))
> set scalafixOnCompile := true
$ mkdir src/main/scala
$ copy-file files/Valid.scala src/main/scala/Valid.scala
> compile
$ exec chmod 000 src/main/scala/Valid.scala
> scalafix --triggered --check
> scalafix --check
> set scalafixOnCompile := false
$ delete src/main/scala

# a file patched during the last run with --triggered should not also be cached.
> set scalafixConfig := Some(file("files/Triggered.scalafix.conf"))
> set scalafixOnCompile := true
$ mkdir src/main/scala
$ copy-file files/ProcedureSyntax.scala src/main/scala/ToPatch.scala
-> scalafix --triggered --check
> compile
-$ must-mirror files/ProcedureSyntax.scala src/main/scala/ToPatch.scala
$ exec chmod 000 src/main/scala/ToPatch.scala
-> scalafix --triggered --check
-> scalafix --check
> set scalafixOnCompile := false
$ delete src/main/scala

# files should not be re-checked if all requested rules have previously been run across invocations
> set scalafixConfig := Some(file("files/DisableSyntaxVar.scalafix.conf"))
$ mkdir src/test/scala
Expand Down

0 comments on commit 1407d55

Please sign in to comment.