Skip to content

Commit

Permalink
Skip validation if there are no sources to fix.
Browse files Browse the repository at this point in the history
Previously, the semanticdb-scalac and scalacOptions validation ran for
empty root projects with no sources, failing the build when running
scalafix from the root project.  Since there are no sources to fix we
should not run any validation.
  • Loading branch information
olafurpg committed Sep 21, 2018
1 parent aa44fa7 commit 4a7f4a3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,47 @@ object ScalafixPlugin extends AutoPlugin {
)
}

private def validateProject(
files: Seq[Path],
dependencies: Seq[ModuleID],
args: ScalafixArguments,
ruleNames: Seq[String]
): Seq[String] = {
if (files.isEmpty) Nil
else {
val errors = ListBuffer.empty[String]
val isSemanticdb =
dependencies.exists(_.name.startsWith("semanticdb-scalac"))
if (!isSemanticdb) {
val names = ruleNames.mkString(", ")
errors +=
s"""|The semanticdb-scalac compiler plugin is required to run semantic rules like $names.
|To fix this problem for this sbt shell session, run `scalafixEnable` and try again.
|To fix this problem permanently for your build, add the following settings to build.sbt:
| addCompilerPlugin(scalafixSemanticdb)
| scalacOptions += "-Yrangepos"
|""".stripMargin
}
val validateError = args.validate()
if (validateError.isPresent) {
errors += validateError.get().getMessage
}
errors
}
}

private def scalafixSemantic(
ruleNames: Seq[String],
mainArgs: ScalafixArguments,
shellArgs: ShellArgs,
config: Configuration
): Def.Initialize[Task[Unit]] = Def.taskDyn {
val errors = ListBuffer.empty[String]
val isSemanticdb =
libraryDependencies.value.exists(_.name.startsWith("semanticdb-scalac"))
if (!isSemanticdb) {
val names = ruleNames.mkString(", ")
errors +=
s"""|The semanticdb-scalac compiler plugin is required to run semantic rules like $names.
|To fix this problem for this sbt shell session, run `scalafixEnable` and try again.
|To fix this problem permanently for your build, add the following settings to build.sbt:
| addCompilerPlugin(scalafixSemanticdb)
| scalacOptions += "-Yrangepos"
|""".stripMargin
}
val dependencies = libraryDependencies.value
val files = filesToFix(shellArgs, config).value
val withScalaArgs = mainArgs
.withScalaVersion(scalaVersion.value)
.withScalacOptions(scalacOptions.value.asJava)
val validateError = withScalaArgs.validate()
if (validateError.isPresent()) {
errors += validateError.get().getMessage
}
val files = filesToFix(shellArgs, config).value
val errors = validateProject(files, dependencies, withScalaArgs, ruleNames)
if (errors.isEmpty) {
Def.task {
val args = withScalaArgs.withClasspath(
Expand Down
20 changes: 20 additions & 0 deletions src/sbt-test/sbt-scalafix/root-validation/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
inThisBuild(
List(
scalaVersion := "2.12.6",
)
)

lazy val root = project
.in(file("."))
.aggregate(lib)

lazy val lib = project
.in(file("lib"))
.settings(
addCompilerPlugin(scalafixSemanticdb),
scalacOptions ++= List(
"-Ywarn-unused",
"-Yrangepos"
)
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package lib

import scala.concurrent.Future
import scala.util.Success

object App { println(Success(1)) }
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-scalafix/root-validation/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resolvers += Resolver.sonatypeRepo("releases")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M6")
3 changes: 3 additions & 0 deletions src/sbt-test/sbt-scalafix/root-validation/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-> root/scalafix --check RemoveUnused
> root/scalafix RemoveUnused
> root/scalafix --check RemoveUnused

0 comments on commit 4a7f4a3

Please sign in to comment.