Skip to content

Commit

Permalink
fix usage of scalafix via another task
Browse files Browse the repository at this point in the history
Regression of 0cf5501: initial (cold) invocations to a task delegating
to scalafix would result in scalafix being called before compilation
(because scalafixRunExplicitly == false) causing false negatives.
  • Loading branch information
github-brice-jaglin committed Jul 15, 2020
1 parent 12c6475 commit 68b7e82
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ object ScalafixPlugin extends AutoPlugin {
compile := Def.taskDyn {
val oldCompile =
compile.value // evaluated first, before the potential scalafix evaluation
val runScalafixAfterCompile =
scalafixOnCompile.value && !scalafixRunExplicitly.value
if (runScalafixAfterCompile)
if (scalafixOnCompile.value)
scalafix
.toTask("")
.map(_ => oldCompile)
Expand Down Expand Up @@ -415,7 +413,7 @@ object ScalafixPlugin extends AutoPlugin {
).findErrors(files, dependencies, withScalaInterface)
if (errors.isEmpty) {
val task = Def.task {
// passively consume compilation output without triggering compile as it can result in a cyclic dependency
// don't use fullClasspath as it results in a cyclic dependency via compile when scalafixOnCompile := true
val classpath =
dependencyClasspath.in(config).value.map(_.data.toPath) :+
classDirectory.in(config).value.toPath
Expand All @@ -428,8 +426,8 @@ object ScalafixPlugin extends AutoPlugin {
streams.in(config, scalafix).value
)
}
if (scalafixRunExplicitly.value) task.dependsOn(compile.in(config))
else task
// sub-task of compile after which bytecode should not be modified
task.dependsOn(manipulateBytecode.in(config))
} else {
Def.task {
if (errors.length == 1) {
Expand Down Expand Up @@ -563,16 +561,6 @@ object ScalafixPlugin extends AutoPlugin {
}
}

// Controls whether scalafix should depend on compile (true) & whether compile may depend on
// scalafix (false), to avoid cyclic dependencies causing deadlocks during executions (as
// dependencies come from dynamic tasks).
private val scalafixRunExplicitly: Def.Initialize[Task[Boolean]] =
Def.task {
executionRoots.value.exists { root =>
Seq(scalafix.key, scalafixAll.key).contains(root.key)
}
}

private def isScalaFile(file: File): Boolean = {
val path = file.getPath
path.endsWith(".scala") ||
Expand Down
1 change: 1 addition & 0 deletions src/sbt-test/sbt-scalafix/wrapper/.scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules = [RemoveUnused]
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-scalafix/wrapper/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
val V = _root_.scalafix.sbt.BuildInfo

scalaVersion := V.scala212
addCompilerPlugin(scalafixSemanticdb)
scalacOptions ++= Seq("-Yrangepos", "-Ywarn-unused")
24 changes: 24 additions & 0 deletions src/sbt-test/sbt-scalafix/wrapper/project/LintAllPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sbt._
import scalafix.sbt.ScalafixPlugin
import scalafix.sbt.ScalafixPlugin.autoImport._

object LintAllPlugin extends AutoPlugin {

override def trigger: PluginTrigger = allRequirements

override def requires: Plugins = ScalafixPlugin

object autoImport {
val lintAll = taskKey[Unit]("run all linters")
}

import autoImport._

override def projectSettings: Seq[Def.Setting[_]] =
Seq(
lintAll := {
scalafixAll.toTask(" --check").value
// & other linters...
}
)
}
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-scalafix/wrapper/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resolvers += Resolver.sonatypeRepo("public")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example

import imported.Imported

object Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package imported

object Imported
14 changes: 14 additions & 0 deletions src/sbt-test/sbt-scalafix/wrapper/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# check that missing rewrites are detected, with or without a warm compilation cache
> set scalafixOnCompile := false
-> lintAll
> compile
-> lintAll

# apply the rewrite via an explicit call
> scalafix

# confirms that `lintAll` sees that rewrite, with or without a warm compilation cache
> clean
> lintAll
> compile
> lintAll

0 comments on commit 68b7e82

Please sign in to comment.