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

Fix calculation of Scala version and turn off the -release flag for 2.12.x < 2.12.5 #1377

Merged
merged 2 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions modules/build/src/main/scala/scala/build/Build.scala
Expand Up @@ -18,13 +18,14 @@ import scala.build.compiler.{ScalaCompiler, ScalaCompilerMaker}
import scala.build.errors.*
import scala.build.internal.resource.ResourceMapper
import scala.build.internal.{Constants, CustomCodeWrapper, MainClass, Util}
import scala.build.options.ScalaVersionUtil.maybeScalaPatchVersion
import scala.build.options.*
import scala.build.options.validation.ValidationException
import scala.build.postprocessing.*
import scala.collection.mutable.ListBuffer
import scala.concurrent.duration.DurationInt
import scala.util.Properties
import scala.util.control.NonFatal
import scala.util.{Properties, Try}

trait Build {
def inputs: Inputs
Expand Down Expand Up @@ -825,9 +826,15 @@ object Build {
Seq(ScalacOpt("-scalajs"))
else Nil

val scalacReleaseV = releaseFlagVersion
.map(v => List("-release", v).map(ScalacOpt(_)))
.getOrElse(Nil)
val scalacReleaseV =
// the -release flag is not supported for Scala 2.12.x < 2.12.5
if params.scalaVersion.startsWith("2.12") &&
params.scalaVersion.maybeScalaPatchVersion.exists(_ < 5)
Comment on lines +831 to +832
Copy link
Contributor

Choose a reason for hiding this comment

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

No big deal, but version comparison with coursier.core.Version could have worked here, it seems, like

val sv = coursier.core.Version(params.scalaVersion)
if params.scalaVersion.startsWith("2.12.") && sv.compareTo(coursier.core.Version("2.12.15")) < 0

then Nil
else
releaseFlagVersion
.map(v => List("-release", v).map(ScalacOpt(_)))
.getOrElse(Nil)

val scalapyOptions =
if (
Expand Down
Expand Up @@ -2380,6 +2380,20 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}
}

if (actualScalaVersion.startsWith("2.12."))
test("verify that Scala version 2.12.x < 2.12.4 is respected and compiles correctly") {
TestInputs(os.rel / "s.sc" -> "println(util.Properties.versionNumberString)").fromRoot {
root =>
(1 until 4).foreach { scalaPatchVersion =>
val scala212VersionString = s"2.12.$scalaPatchVersion"
val res =
os.proc(TestUtil.cli, "run", ".", "-S", scala212VersionString, TestUtil.extraOptions)
.call(cwd = root)
expect(res.out.trim == scala212VersionString)
}
}
}

def scalapyNativeTest(): Unit = {
val inputs = TestInputs(
os.rel / "helloscalapy.sc" ->
Expand Down
Expand Up @@ -19,6 +19,7 @@ import scala.build.errors.{
import scala.build.internal.Regexes.scala2NightlyRegex
import scala.build.internal.Util
import scala.concurrent.duration.DurationInt
import scala.util.Try
import scala.util.control.NonFatal

object ScalaVersionUtil {
Expand Down Expand Up @@ -221,13 +222,14 @@ object ScalaVersionUtil {
if (filtered.isEmpty) matchingStableVersions
else filtered
}.filter(v => isSupportedVersion(v.repr))
if (validMatchingVersions.isEmpty)
Left(new UnsupportedScalaVersionError(
scalaVersionStringArg,
latestSupportedStableVersions
))
else
Right(validMatchingVersions.max.repr)

validMatchingVersions.find(_.repr == scalaVersionStringArg) match {
case Some(v) => Right(v.repr)
case None if validMatchingVersions.nonEmpty => Right(validMatchingVersions.max.repr)
case _ => Left(
new UnsupportedScalaVersionError(scalaVersionStringArg, latestSupportedStableVersions)
)
}
}
}

Expand Down Expand Up @@ -290,4 +292,10 @@ object ScalaVersionUtil {
.distinct
}

extension (sv: String) {
def maybeScalaPatchVersion: Option[Int] = sv
.split('.').drop(2).headOption
.flatMap(_.split('-').headOption)
.flatMap(pv => Try(pv.toInt).toOption)
}
}