diff --git a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 7ff1b462..e2ace9a0 100644 --- a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -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( diff --git a/src/sbt-test/sbt-scalafix/root-validation/build.sbt b/src/sbt-test/sbt-scalafix/root-validation/build.sbt new file mode 100644 index 00000000..52f9165e --- /dev/null +++ b/src/sbt-test/sbt-scalafix/root-validation/build.sbt @@ -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" + ) + ) + diff --git a/src/sbt-test/sbt-scalafix/root-validation/lib/src/main/scala/lib/App.scala b/src/sbt-test/sbt-scalafix/root-validation/lib/src/main/scala/lib/App.scala new file mode 100644 index 00000000..be4afa42 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/root-validation/lib/src/main/scala/lib/App.scala @@ -0,0 +1,6 @@ +package lib + +import scala.concurrent.Future +import scala.util.Success + +object App { println(Success(1)) } diff --git a/src/sbt-test/sbt-scalafix/root-validation/project/plugins.sbt b/src/sbt-test/sbt-scalafix/root-validation/project/plugins.sbt new file mode 100644 index 00000000..f6f697b2 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/root-validation/project/plugins.sbt @@ -0,0 +1,2 @@ +resolvers += Resolver.sonatypeRepo("releases") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version")) diff --git a/src/sbt-test/sbt-scalafix/root-validation/project/project/plugins.sbt b/src/sbt-test/sbt-scalafix/root-validation/project/project/plugins.sbt new file mode 100644 index 00000000..475375b5 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/root-validation/project/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M6") diff --git a/src/sbt-test/sbt-scalafix/root-validation/test b/src/sbt-test/sbt-scalafix/root-validation/test new file mode 100644 index 00000000..55d8f8c9 --- /dev/null +++ b/src/sbt-test/sbt-scalafix/root-validation/test @@ -0,0 +1,3 @@ +-> root/scalafix --check RemoveUnused +> root/scalafix RemoveUnused +> root/scalafix --check RemoveUnused