Skip to content

Commit

Permalink
Merge pull request #1666 from bjaglin/xsource3
Browse files Browse the repository at this point in the history
add support for -Xsource:3 testkit input & output
  • Loading branch information
bjaglin authored Jan 21, 2024
2 parents 8965deb + 39922fb commit 8984220
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 147 deletions.
29 changes: 21 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ lazy val shared = projectMatrix
coverageEnabled := false
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatformFull(buildWithTargetVersions.map(_._2))
// just compile shared with the latest patch for each minor Scala version
// to reduce the cardinality of the build
.jvmPlatform(buildScalaVersions)
.disablePlugins(ScalafixPlugin)

lazy val input = projectMatrix
Expand All @@ -198,25 +200,36 @@ lazy val input = projectMatrix
scalacOptions ++= warnUnused.value, // For RemoveUnusedTerms
logLevel := Level.Error, // avoid flood of compiler warnings
libraryDependencies ++= testsDependencies.value,
coverageEnabled := false
coverageEnabled := false,
// mimic dependsOn(shared) but allowing binary Scala version matching
Compile / internalDependencyClasspath ++=
resolve(shared, Compile / exportedProducts).value
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatformFull(buildWithTargetVersions.map(_._2))
.jvmPlatformTargets(buildScalaVersionsWithTargets.map(_._2))
.disablePlugins(ScalafixPlugin)
.dependsOn(shared)

lazy val output = projectMatrix
.in(file("scalafix-tests/output"))
.settings(
noPublishAndNoMima,
scalacOptions --= warnUnusedImports.value,
libraryDependencies ++= testsDependencies.value,
coverageEnabled := false
coverageEnabled := false,
// mimic dependsOn(shared) but allowing binary Scala version matching
Compile / internalDependencyClasspath ++=
resolve(shared, Compile / exportedProducts).value
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatform(buildScalaVersions)
.jvmPlatformTargets(
buildScalaVersionsWithTargets
.map(_._2)
// don't compile output with old Scala patch versions to reduce the
// cardinality of the build: checking that it compiles with the
// latest patch of each minor Scala version is enough
.filter(axis => buildScalaVersions.contains(axis.scalaVersion))
)
.disablePlugins(ScalafixPlugin)
.dependsOn(shared)

lazy val unit = projectMatrix
.in(file("scalafix-tests/unit"))
Expand Down Expand Up @@ -359,7 +372,7 @@ lazy val expect = projectMatrix
}
)
.defaultAxes(VirtualAxis.jvm)
.jvmPlatformWithTargets(buildWithTargetVersions)
.jvmPlatformAgainstTargets(buildScalaVersionsWithTargets)
.dependsOn(integration)

lazy val docs = projectMatrix
Expand Down
25 changes: 0 additions & 25 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@ import ScalafixBuild.autoImport.isScala2
import sbt.Keys.scalaVersion
import sbt._

import scala.util.Try

/* scalafmt: { maxColumn = 120 }*/

object Dependencies {
val scala212 = sys.props.getOrElse("scala212.nightly", "2.12.18")
val scala213 = sys.props.getOrElse("scala213.nightly", "2.13.12")
val scala3 = sys.props.getOrElse("scala3.nightly", "3.3.1")

val buildScalaVersions = Seq(scala212, scala213, scala3)
val buildWithTargetVersions: Seq[(String, String)] = {
val all = buildScalaVersions.map(sv => (sv, sv)) ++
Seq(scala213, scala212).flatMap(sv => previousVersions(sv).map(prev => (sv, prev))) ++
Seq(scala213, scala212).map(sv => (sv, scala3))

all.filter {
case (_, v) if System.getProperty("java.version") == "21" =>
!Seq("2.12.16", "2.12.17", "2.13.10").contains(v)
case _ =>
true
}
}

val bijectionCoreV = "0.9.7"
val collectionCompatV = "2.11.0"
val coursierV = "2.1.8"
Expand Down Expand Up @@ -74,13 +58,4 @@ object Dependencies {
val scalatest = "org.scalatest" %% "scalatest" % scalatestV
val munit = "org.scalameta" %% "munit" % munitV
val semanticdbScalacCore = "org.scalameta" % "semanticdb-scalac-core" % scalametaV cross CrossVersion.full

private def previousVersions(scalaVersion: String): Seq[String] = {
val split = scalaVersion.split('.')
val binaryVersion = split.take(2).mkString(".")
val compilerVersion = Try(split.last.toInt).toOption
val previousPatchVersions =
compilerVersion.map(version => List.range(version - 2, version).filter(_ >= 0)).getOrElse(Nil)
previousPatchVersions.map(v => s"$binaryVersion.$v")
}
}
72 changes: 64 additions & 8 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.github.sbt.sbtghpages.GhpagesKeys
import sbt.librarymanagement.ivy.IvyDependencyResolution
import sbt.plugins.IvyPlugin
import sbtversionpolicy.DependencyCheckReport
import scala.util.Try

object ScalafixBuild extends AutoPlugin with GhpagesKeys {
override def trigger = allRequirements
Expand All @@ -30,6 +31,33 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
publish / skip := true
)
lazy val supportedScalaVersions = List(scala213, scala212)
lazy val buildScalaVersions = Seq(scala212, scala213, scala3)
lazy val buildScalaVersionsWithTargets: Seq[(String, TargetAxis)] =
buildScalaVersions.map(sv => (sv, TargetAxis(sv))) ++
Seq(scala213, scala212).flatMap { sv =>
def previousVersions(scalaVersion: String): Seq[String] = {
val split = scalaVersion.split('.')
val binaryVersion = split.take(2).mkString(".")
val compilerVersion = Try(split.last.toInt).toOption
val previousPatchVersions =
compilerVersion
.map(version => List.range(version - 2, version).filter(_ >= 0))
.getOrElse(Nil)
previousPatchVersions
.map { patch => s"$binaryVersion.$patch" }
.filterNot { v =>
System.getProperty("java.version").startsWith("21") &&
Seq("2.12.16", "2.12.17", "2.13.10").contains(v)
}
}

val prevVersions = previousVersions(sv).map(prev => TargetAxis(prev))
val scala3FromScala2 = TargetAxis(scala3)
val xsource3 = TargetAxis(sv, xsource3 = true)

(prevVersions :+ scala3FromScala2 :+ xsource3).map((sv, _))
}

lazy val publishLocalTransitive =
taskKey[Unit]("Run publishLocal on this project and its dependencies")
lazy val isFullCrossVersion = Seq(
Expand Down Expand Up @@ -123,32 +151,29 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
taskKey[Unit]("run tests, excluding those incompatible with Windows")

/**
* Lookup a setting key for the project of the same scala version in the
* given matrix
* Lookup the project with the closest Scala version, and resolve `key`
*/
def resolve[T](
matrix: ProjectMatrix,
key: SettingKey[T]
): Def.Initialize[T] =
Def.settingDyn {
val sv = scalaVersion.value
val project = matrix.jvm(sv)
val project = lookup(matrix, scalaVersion.value)
Def.setting((project / key).value)
}

/**
* Lookup a task key for the project of the same scala version in the given
* matrix
* Lookup the project with the closest Scala version, and resolve `key`
*/
def resolve[T](
matrix: ProjectMatrix,
key: TaskKey[T]
): Def.Initialize[Task[T]] =
Def.taskDyn {
val sv = scalaVersion.value
val project = matrix.jvm(sv)
val project = lookup(matrix, scalaVersion.value)
Def.task((project / key).value)
}

}

import autoImport._
Expand Down Expand Up @@ -317,4 +342,35 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
}
}
)

/**
* Find the project matching the full Scala version when available or a binary
* one otherwise
*/
private def lookup(matrix: ProjectMatrix, scalaVersion: String): Project = {
val projects = matrix
.allProjects()
.collect {
case (project, projectVirtualAxes)
// CliSemanticSuite depends on classes compiled without -Xsource:3
if !projectVirtualAxes.contains(Xsource3Axis) =>
(
projectVirtualAxes
.collectFirst { case x: VirtualAxis.ScalaVersionAxis => x }
.get
.value,
project
)
}
.toMap

val fullMatch = projects.get(scalaVersion)

def binaryMatch = {
val scalaBinaryVersion = CrossVersion.binaryScalaVersion(scalaVersion)
projects.find(_._1.startsWith(scalaBinaryVersion)).map(_._2)
}

fullMatch.orElse(binaryMatch).get
}
}
Loading

0 comments on commit 8984220

Please sign in to comment.