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

Support several scalafixScalaBinaryVersion within a build #380

Merged
merged 11 commits into from
Dec 19, 2023
81 changes: 47 additions & 34 deletions src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,60 @@ object ScalafixInterface {
private lazy val _value = scala.util.Try(thunk())
override def apply(): T = _value.get
}
private val fromToolClasspathMemo: BlockingCache[
(String, Seq[ModuleID], Seq[Repository]),
ScalafixInterface
] = new BlockingCache

def fromToolClasspath(
scalafixBinaryScalaVersion: String,
scalafixDependencies: Seq[ModuleID],
scalafixCustomResolvers: Seq[Repository],
logger: Logger = ConsoleLogger(System.out)
): () => ScalafixInterface =
new LazyValue({ () =>
if (scalafixBinaryScalaVersion.startsWith("3"))
logger.error(
"To use Scalafix on Scala 3 projects, you must unset `scalafixBinaryScalaVersion`. " +
"Rules such as ExplicitResultTypes requiring the project version to match the Scalafix " +
"version are unsupported for the moment."
)
else if (scalafixBinaryScalaVersion == "2.11")
logger.error(
"Scala 2.11 is no longer supported. Please downgrade to the final version supporting " +
"it: sbt-scalafix 0.10.4."
)
val callback = new ScalafixLogger(logger)
val scalafixArguments = ScalafixAPI
.fetchAndClassloadInstance(
fromToolClasspathMemo.getOrElseUpdate(
bjaglin marked this conversation as resolved.
Show resolved Hide resolved
(
scalafixBinaryScalaVersion,
scalafixCustomResolvers.asJava
)
.newArguments()
.withMainCallback(callback)
val printStream =
new PrintStream(
LoggingOutputStream(
logger,
Level.Info
)
)
new ScalafixInterface(scalafixArguments, Nil)
.withArgs(
Arg.PrintStream(printStream),
Arg.ToolClasspath(
Nil,
scalafixDependencies,
scalafixCustomResolvers
)
)
scalafixDependencies,
scalafixCustomResolvers
), {
if (scalafixBinaryScalaVersion.startsWith("3"))
logger.error(
"To use Scalafix on Scala 3 projects, you must unset `scalafixBinaryScalaVersion`. " +
"Rules such as ExplicitResultTypes requiring the project version to match the Scalafix " +
"version are unsupported for the moment."
)
else if (scalafixBinaryScalaVersion == "2.11")
logger.error(
"Scala 2.11 is no longer supported. Please downgrade to the final version supporting " +
"it: sbt-scalafix 0.10.4."
)
val callback = new ScalafixLogger(logger)
val scalafixArguments = ScalafixAPI
.fetchAndClassloadInstance(
scalafixBinaryScalaVersion,
scalafixCustomResolvers.asJava
)
.newArguments()
.withMainCallback(callback)
val printStream =
new PrintStream(
LoggingOutputStream(
logger,
Level.Info
)
)
new ScalafixInterface(scalafixArguments, Nil)
.withArgs(
Arg.PrintStream(printStream),
Arg.ToolClasspath(
Nil,
scalafixDependencies,
scalafixCustomResolvers
)
)
}
)
})
}
10 changes: 5 additions & 5 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ object ScalafixPlugin extends AutoPlugin {
SettingKey[Boolean]("bspEnabled") := false
)
),
scalafixInterfaceProvider := ScalafixInterface.fromToolClasspath(
bjaglin marked this conversation as resolved.
Show resolved Hide resolved
scalafixScalaBinaryVersion.value,
scalafixDependencies = scalafixDependencies.value,
scalafixCustomResolvers = scalafixResolvers.value
),
update := {
object SemanticdbScalac {
def unapply(id: ModuleID): Option[String] =
Expand Down Expand Up @@ -211,11 +216,6 @@ object ScalafixPlugin extends AutoPlugin {
),
scalafixDependencies := Nil,
commands += ScalafixEnable.command,
scalafixInterfaceProvider := ScalafixInterface.fromToolClasspath(
(ThisBuild / scalafixScalaBinaryVersion).value,
scalafixDependencies = (ThisBuild / scalafixDependencies).value,
scalafixCustomResolvers = (ThisBuild / scalafixResolvers).value
),
scalafixInterfaceCache := new BlockingCache[
ToolClasspath,
ScalafixInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rules = [DisableSyntax, OrganizeImports, ExplicitResultTypes]
Copy link
Collaborator

Choose a reason for hiding this comment

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

DisableSyntax seems unexercised, but it doesn't hurt to keep a syntactic rule here I guess

OrganizeImports.removeUnused = true
ExplicitResultTypes.skipSimpleDefinitions = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules = [DisableSyntax, OrganizeImports]
OrganizeImports.removeUnused = false
24 changes: 24 additions & 0 deletions src/sbt-test/sbt-scalafix/cross-build-advanced/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import _root_.scalafix.sbt.{BuildInfo => Versions}

val scala3Version = "3.3.0"
ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision

lazy val root = project
.in(file("."))
.settings(
scalaVersion := Versions.scala212,
crossScalaVersions := Seq(Versions.scala212, Versions.scala213, scala3Version),
scalacOptions ++= (if (scalaVersion.value.startsWith("2")) Seq("-Ywarn-unused") else Seq()),
scalafixScalaBinaryVersion := {
if ((scalaBinaryVersion).value == "3") {
(scalafixScalaBinaryVersion).value
} else { (scalaBinaryVersion).value }
bjaglin marked this conversation as resolved.
Show resolved Hide resolved
},
scalafixConfig := {
if (scalaBinaryVersion.value == "3")
Some(file(".scalafix-3.conf"))
else
Some(file(".scalafix-2.conf"))
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.7
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,7 @@
import scala.Int
import scala.collection.Seq
import scala.collection.AbstractSeq

object Main {
def foo(a: Int) = a + 2.0f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.Int

object Main {
def foo(a: Int): Float = a + 2.0f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.Int
import scala.collection.Seq
import scala.collection.AbstractSeq

object Main {
def foo(a: Int) = a + 2.0f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.Int
import scala.collection.AbstractSeq
import scala.collection.Seq

object Main {
def foo(a: Int) = a + 2.0f
}
6 changes: 6 additions & 0 deletions src/sbt-test/sbt-scalafix/cross-build-advanced/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
> +compile

> +scalafix

$ must-mirror src/main/scala-2/Main.scala src/main/scala-2/Main.scala.expected
$ must-mirror src/main/scala-3/Main.scala src/main/scala-3/Main.scala.expected