Skip to content

Commit

Permalink
Make explicitly passed scala version use the latest release, not the …
Browse files Browse the repository at this point in the history
…default one (#2411)
  • Loading branch information
MaciejG604 authored Sep 22, 2023
1 parent ecd9ad4 commit 743b870
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package scala.build.tests

import com.eed3si9n.expecty.Expecty.{assert => expect}
import com.eed3si9n.expecty.Expecty.assert as expect
import coursier.Repositories
import coursier.cache.FileCache
import coursier.core.Version
import dependency.ScalaParameters

import scala.build.Ops.*
Expand All @@ -19,11 +20,12 @@ import scala.build.options.{
InternalOptions,
MaybeScalaVersion,
ScalaOptions,
ScalaVersionUtil,
ScalacOpt,
ShadowingSeq
}
import scala.build.{Build, BuildThreads, LocalRepo}
import scala.build.Directories
import scala.build.options.ScalacOpt
import scala.build.Positioned
import scala.build.tests.util.BloopServer
import scala.concurrent.duration.DurationInt
Expand Down Expand Up @@ -100,6 +102,7 @@ class BuildOptionsTests extends munit.FunSuite {
scalaVersion = Some(MaybeScalaVersion("2.11.2"))
)
)

assert(
options.projectParams.swap.exists {
case _: UnsupportedScalaVersionError => true; case _ => false
Expand Down Expand Up @@ -259,19 +262,15 @@ class BuildOptionsTests extends munit.FunSuite {
}

val expectedScalaVersions = Seq(
Some("3") -> defaultScalaVersion,
None -> defaultScalaVersion,
Some("2.13") -> defaultScala213Version,
Some("2.12") -> defaultScala212Version,
Some("2") -> defaultScala213Version,
Some("2.13.2") -> "2.13.2",
Some("3.0.1") -> "3.0.1",
Some("3.0") -> "3.0.2"
)

for ((prefix, expectedScalaVersion) <- expectedScalaVersions)
test(
s"use expected default scala version for prefix scala version: ${prefix.getOrElse("empty")}"
s"use scala $expectedScalaVersion for prefix scala version: ${prefix.getOrElse("empty")}"
) {
val options = BuildOptions(
scalaOptions = ScalaOptions(
Expand All @@ -288,6 +287,39 @@ class BuildOptionsTests extends munit.FunSuite {
expect(scalaParams == expectedScalaParams)
}

for {
(prefix, defaultMatchingVersion) <- Seq(
"2.12" -> defaultScala212Version,
"2.13" -> defaultScala213Version,
"3" -> defaultScalaVersion
)
} {
val options = BuildOptions(
scalaOptions = ScalaOptions(
scalaVersion = Some(prefix).map(MaybeScalaVersion(_))
),
internal = InternalOptions(
cache = Some(FileCache().withTtl(0.seconds))
)
)

val latestMatchingVersion = ScalaVersionUtil
.allMatchingVersions(None, options.finalCache, options.finalRepositories.orThrow)
.filter(ScalaVersionUtil.isStable)
.filter(_.startsWith(prefix))
.maxBy(Version(_))

test(
s"-S $prefix should chose the latest version ($latestMatchingVersion), not necessarily the default ($defaultMatchingVersion)"
) {
val scalaParams = options.scalaParams.orThrow.getOrElse(???)

val expectedScalaParams = ScalaParameters(latestMatchingVersion)

expect(scalaParams == expectedScalaParams, s"expected $expectedScalaParams, got $scalaParams")
}
}

test("User scalac options shadow internal ones") {
val defaultOptions = BuildOptions(
internal = InternalOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,7 @@ final case class BuildOptions(
}
Some(sv)

case None =>
val allStableVersions =
ScalaVersionUtil.allMatchingVersions(None, finalCache, value(finalRepositories))
.filter(ScalaVersionUtil.isStable)
val sv = value {
ScalaVersionUtil.default(allStableVersions)
}
Some(sv)
case None => Some(Constants.defaultScalaVersion)
}

svOpt match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ object ScalaVersionUtil {
def scala212Nightly = "2.12.nightly"
def scala213Nightly = List("2.13.nightly", "2.nightly")
def scala3Nightly = "3.nightly"
def maxSupportedStableScalaVersions = Seq(
Constants.defaultScala212Version,
Constants.defaultScala213Version,
Constants.defaultScalaVersion
).map(Version(_))
extension (cache: FileCache[Task]) {
def fileWithTtl0(artifact: Artifact): Either[ArtifactError, File] =
cache.logger.use {
Expand Down Expand Up @@ -200,6 +195,7 @@ object ScalaVersionUtil {
val versionPool =
ScalaVersionUtil.allMatchingVersions(Some(scalaVersionStringArg), cache, repositories)
.filter(ScalaVersionUtil.isStable)

val prefix =
if (Util.isFullScalaVersion(scalaVersionStringArg)) scalaVersionStringArg
else if (scalaVersionStringArg.endsWith(".")) scalaVersionStringArg
Expand All @@ -208,34 +204,20 @@ object ScalaVersionUtil {
if (matchingStableVersions.isEmpty)
Left(new InvalidBinaryScalaVersionError(scalaVersionStringArg))
else {
val validMaxVersions = maxSupportedStableScalaVersions
.filter(_.repr.startsWith(prefix))
val validMatchingVersions = {
val filtered = matchingStableVersions.filter(v => validMaxVersions.exists(v <= _))
if (filtered.isEmpty) matchingStableVersions
else filtered
}.filter(v => isSupportedVersion(v.repr))

validMatchingVersions.find(_.repr == scalaVersionStringArg) match {
case Some(v) => Right(v.repr)
case None if validMatchingVersions.nonEmpty => Right(validMatchingVersions.max.repr)
val supportedMatchingStableVersions =
matchingStableVersions.filter(v => isSupportedVersion(v.repr))

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

def default(versionPool: Seq[String]): Either[ScalaVersionError, String] = {
val validVersions = versionPool
.map(Version(_))
.filter(v => maxSupportedStableScalaVersions.exists(v <= _))
if (validVersions.isEmpty)
Left(new NoValidScalaVersionFoundError(versionPool))
else
Right(validVersions.max.repr)
}

private def isSupportedVersion(version: String): Boolean =
version.startsWith("2.12.") || version.startsWith("2.13.") || version.startsWith("3.")

Expand Down

0 comments on commit 743b870

Please sign in to comment.