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

scalafix-cli_3 & test dependencies (GSoC 2022) #1650

Merged
merged 11 commits into from
Oct 27, 2022
79 changes: 56 additions & 23 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ lazy val core = projectMatrix
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(buildScalaVersions :+ scala3, Seq(), p => p)
.jvmPlatform(buildScalaVersions)
.enablePlugins(BuildInfoPlugin)

lazy val rules = projectMatrix
Expand All @@ -105,7 +105,7 @@ lazy val rules = projectMatrix
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(buildScalaVersions :+ scala3, Seq(), p => p)
.jvmPlatform(buildScalaVersions)
.dependsOn(core)
.enablePlugins(BuildInfoPlugin)

Expand All @@ -114,10 +114,18 @@ lazy val reflect = projectMatrix
.settings(
moduleName := "scalafix-reflect",
isFullCrossVersion,
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"org.scala-lang" % "scala-reflect" % scalaVersion.value
)
libraryDependencies ++= {
if (!isScala3.value)
List(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"org.scala-lang" % "scala-reflect" % scalaVersion.value
)
else
List(
"org.scala-lang" %% "scala3-compiler" % scalaVersion.value,
"org.scala-lang" %% "scala3-library" % scalaVersion.value
)
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(buildScalaVersions)
Expand All @@ -129,13 +137,18 @@ lazy val cli = projectMatrix
moduleName := "scalafix-cli",
isFullCrossVersion,
libraryDependencies ++= Seq(
java8Compat,
rvacaru marked this conversation as resolved.
Show resolved Hide resolved
nailgunServer,
jgit,
commonText
),
libraryDependencies ++= {
if (isScala211.value) Seq()
if (!isScala3.value)
Seq(java8Compat)
else
Seq()
},
libraryDependencies ++= {
if (isScala211.value || isScala3.value) Seq()
rvacaru marked this conversation as resolved.
Show resolved Hide resolved
else
Seq(
// metaconfig 0.10.0 shaded pprint but rules built with an old
Expand All @@ -160,7 +173,7 @@ lazy val testsShared = projectMatrix
coverageEnabled := false
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(testTargetScalaVersions)
.jvmPlatform(buildScalaVersions)
.disablePlugins(ScalafixPlugin)

lazy val testsInput = projectMatrix
Expand All @@ -176,7 +189,7 @@ lazy val testsInput = projectMatrix
coverageEnabled := false
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(testTargetScalaVersions)
.jvmPlatform(buildScalaVersions)
.disablePlugins(ScalafixPlugin)

lazy val testsOutput = projectMatrix
Expand All @@ -188,18 +201,16 @@ lazy val testsOutput = projectMatrix
coverageEnabled := false
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(testTargetScalaVersions)
.jvmPlatform(buildScalaVersions)
.disablePlugins(ScalafixPlugin)

lazy val testkit = projectMatrix
.in(file("scalafix-testkit"))
.settings(
moduleName := "scalafix-testkit",
isFullCrossVersion,
libraryDependencies ++= Seq(
googleDiff,
scalatest
)
libraryDependencies += googleDiff,
libraryDependencies += scalatestDep.value
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(buildScalaVersions)
Expand All @@ -211,18 +222,32 @@ lazy val unit = projectMatrix
noPublishAndNoMima,
// Change working directory to match when `fork := false`.
Test / baseDirectory := (ThisBuild / baseDirectory).value,
// Prevent issues with scalatest serialization
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat,
javaOptions := Nil,
testFrameworks += new TestFramework("munit.Framework"),
buildInfoPackage := "scalafix.tests",
buildInfoObject := "BuildInfo",
libraryDependencies ++= List(
jgit,
coursier,
scalatest.withRevision(
"3.2.13"
), // make sure testkit clients can use recent 3.x versions
scalametaTeskit
munit,
scalatest.withRevision(scalatestLatestV)
),
libraryDependencies ++= {
if (!isScala3.value) {
List(
coursier,
scalametaTeskit
)
} else {
// exclude _2.13 artifacts that have their _3 counterpart in the classpath
List(
coursier
.exclude("org.scala-lang.modules", "scala-xml_2.13"),
scalametaTeskit
.exclude("com.lihaoyi", "sourcecode_2.13")
.exclude("org.scala-lang.modules", "scala-collection-compat_2.13")
.exclude("org.scalameta", "munit_2.13")
)
}
},
Compile / compile / compileInputs := {
(Compile / compile / compileInputs)
.dependsOn(
Expand Down Expand Up @@ -277,8 +302,11 @@ lazy val unit = projectMatrix
IO.write(props, "Input data for scalafix testkit", out)
List(out)
},
buildInfoPackage := "scalafix.tests",
buildInfoObject := "BuildInfo",
buildInfoKeys := Seq[BuildInfoKey](
"scalametaVersion" -> scalametaV,
"scalaVersion" -> scalaVersion.value,
"baseDirectory" ->
(ThisBuild / baseDirectory).value,
"unitResourceDirectory" -> (Compile / resourceDirectory).value,
Expand Down Expand Up @@ -313,6 +341,11 @@ lazy val unit = projectMatrix
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(
scalaVersions = Seq(scala3),
axisValues = Seq(TargetAxis(scala3)),
settings = Seq()
)
.jvmPlatform(
scalaVersions = Seq(scala212),
axisValues = Seq(TargetAxis(scala3)),
Expand Down
15 changes: 10 additions & 5 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Dependencies {
val scala213 = "2.13.10"
val scala3 = "3.1.3"

val buildScalaVersions = Seq(scala211, scala212, scala213)
val buildScalaVersions = Seq(scala211, scala212, scala213, scala3)
val testTargetScalaVersions = Seq(scala211, scala212, scala213, scala3)

// we support 3 last binary versions of scala212 and scala213
Expand All @@ -34,12 +34,15 @@ object Dependencies {
val scalaXmlV = "2.1.0"
val scalaXml211V = "1.3.0" // scala-xml stops publishing for scala 2.11
val scalametaV = "4.6.0"
val scalatestV = "3.0.8" // don't bump, to avoid forcing breaking changes on clients via eviction
val scalatestMinV = "3.0.8" // don't bump, to avoid forcing breaking changes on clients via eviction
val scalatestLatestV = "3.2.13"
val munitV = "0.7.29"

val bijectionCore = "com.twitter" %% "bijection-core" % bijectionCoreV
val collectionCompat = "org.scala-lang.modules" %% "scala-collection-compat" % collectionCompatV
val commonText = "org.apache.commons" % "commons-text" % commontTextV
val coursier = "io.get-coursier" %% "coursier" % coursierV
val coursier = ("io.get-coursier" %% "coursier" % coursierV)
.cross(CrossVersion.for3Use2_13)
val coursierInterfaces = "io.get-coursier" % "interface" % coursierInterfaceV
val googleDiff = "com.googlecode.java-diff-utils" % "diffutils" % googleDiffV
val java8Compat = "org.scala-lang.modules" %% "scala-java8-compat" % java8CompatV
Expand All @@ -56,8 +59,10 @@ object Dependencies {
// https://github.com/scalameta/scalameta/issues/2485
val scalameta = ("org.scalameta" %% "scalameta" % scalametaV)
.cross(CrossVersion.for3Use2_13)
val scalametaTeskit = "org.scalameta" %% "testkit" % scalametaV
val scalatest = "org.scalatest" %% "scalatest" % scalatestV
val scalametaTeskit = ("org.scalameta" %% "testkit" % scalametaV)
.cross(CrossVersion.for3Use2_13)
val scalatest = "org.scalatest" %% "scalatest" % scalatestMinV
val munit = "org.scalameta" %% "munit" % munitV
val semanticdbScalacCore = "org.scalameta" % "semanticdb-scalac-core" % scalametaV cross CrossVersion.full

private def previousVersions(scalaVersion: String): List[String] = {
Expand Down
19 changes: 16 additions & 3 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
lazy val buildInfoSettingsForRules: Seq[Def.Setting[_]] = Seq(
buildInfoObject := "RulesBuildInfo"
)

lazy val scalatestDep = Def.setting {
if (isScala3.value) scalatest.withRevision(scalatestLatestV)
else scalatest
}
}

import autoImport._
Expand All @@ -140,8 +145,7 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
},
commands += Command.command("ci-3") { s =>
"unit2_12Target3/test" ::
"core3/compile" ::
"rules3/compile" ::
"unit3Target3/test" ::
s
},
commands += Command.command("ci-213") { s =>
Expand Down Expand Up @@ -218,8 +222,17 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
Compile / doc / scalacOptions ++= scaladocOptions,
Compile / unmanagedSourceDirectories ++= {
val sourceDir = (Compile / sourceDirectory).value
val scala212PlusSourceDir = "scala-2.12+"
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n >= 12 => Seq(sourceDir / scala212PlusSourceDir)
case Some((3, _)) => Seq(sourceDir / scala212PlusSourceDir)
rvacaru marked this conversation as resolved.
Show resolved Hide resolved
case _ => Seq()
}
},
Compile / unmanagedResourceDirectories ++= {
val resourceParentDir = (Compile / resourceDirectory).value.getParentFile
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n >= 12 => Seq(sourceDir / "scala-2.12+")
case Some((major, _)) => Seq(resourceParentDir / s"resources-${major}")
case _ => Seq()
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ object ScalafixFileEvaluationImpl {
ctx: RuleCtx,
index: Option[v0.SemanticdbIndex]
): ScalafixFileEvaluationImpl = {
val scalafixPatches = patches.map(ScalafixPatchImpl)
val scalafixPatches = patches.map(ScalafixPatchImpl.apply)
ScalafixFileEvaluationImpl(
originalPath = originalPath,
fixedOpt = fixed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.Optional
import scala.{meta => m}

import scala.meta.inputs.Input
import scala.meta.internal.inputs.XtensionInputSyntaxStructure

import scalafix.interfaces.ScalafixInput

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import scala.util.control.NoStackTrace
import scala.util.control.NonFatal

import scala.meta.inputs.Input
import scala.meta.internal.inputs.XtensionInputSyntaxStructure
import scala.meta.internal.semanticdb.TextDocument
import scala.meta.io.AbsolutePath
import scala.meta.parsers.ParseException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalafix.internal.v1
import scala.meta.AbsolutePath
import scala.meta.Input
import scala.meta.Source
import scala.meta.internal.inputs.XtensionInputSyntaxStructure
import scala.meta.internal.io.FileIO
import scala.meta.internal.symtab.SymbolTable
import scala.meta.io.RelativePath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package scalafix.internal.reflect
import java.io.File
import java.nio.file.Paths

import scala.reflect.internal.util.AbstractFileClassLoader
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.Directory
import scala.reflect.io.PlainDirectory
import scala.tools.nsc.Global
import scala.tools.nsc.Settings
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.io.VirtualDirectory
import scala.tools.nsc.reporters.StoreReporter

import scala.meta.io.AbsolutePath

import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Input
import metaconfig.Position

class RuleCompiler(
classpath: String,
target: AbstractFile = new VirtualDirectory("(memory)", None)
targetDirectory: Option[File] = None
) {
private val output = targetDirectory match {
case Some(file) => new PlainDirectory(new Directory(file))
case None => new VirtualDirectory("(memory)", None)
}
private val settings = new Settings()
settings.deprecation.value = true // enable detailed deprecation warnings
settings.unchecked.value = true // enable detailed unchecked warnings
settings.outputDirs.setSingleOutput(target)
settings.outputDirs.setSingleOutput(output)
settings.classpath.value = classpath
lazy val reporter = new StoreReporter
private val global = new Global(settings, reporter)
private val classLoader =
new AbstractFileClassLoader(target, this.getClass.getClassLoader)

def compile(input: Input): Configured[ClassLoader] = {
reporter.reset()
Expand All @@ -55,33 +55,10 @@ class RuleCompiler(
ConfError
.fromResults(errors.toSeq)
.map(_.notOk)
.getOrElse(Configured.Ok(classLoader))
}
}
object RuleCompiler {

def defaultClasspath: String = {
defaultClasspathPaths.mkString(File.pathSeparator)
}

def defaultClasspathPaths: List[AbsolutePath] = {
val classLoader = ClasspathOps.thisClassLoader
val paths = classLoader.getURLs.iterator.map { u =>
if (u.getProtocol.startsWith("bootstrap")) {
import java.nio.file._
val stream = u.openStream
val tmp = Files.createTempFile("bootstrap-" + u.getPath, ".jar")
try {
Files.copy(stream, tmp, StandardCopyOption.REPLACE_EXISTING)
} finally {
stream.close()
}
AbsolutePath(tmp)
} else {
AbsolutePath(Paths.get(u.toURI))
.getOrElse {
val classLoader: AbstractFileClassLoader =
new AbstractFileClassLoader(output, this.getClass.getClassLoader)
Configured.Ok(classLoader)
}
}
paths.toList
}

}
Loading