diff --git a/modules/build/src/test/scala/scala/build/tests/BuildOptionsTests.scala b/modules/build/src/test/scala/scala/build/tests/BuildOptionsTests.scala index 9d7a1db725..c6008aea82 100644 --- a/modules/build/src/test/scala/scala/build/tests/BuildOptionsTests.scala +++ b/modules/build/src/test/scala/scala/build/tests/BuildOptionsTests.scala @@ -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.* @@ -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 @@ -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 @@ -259,11 +262,7 @@ 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" @@ -271,7 +270,7 @@ class BuildOptionsTests extends munit.FunSuite { 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( @@ -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( diff --git a/modules/options/src/main/scala/scala/build/options/BuildOptions.scala b/modules/options/src/main/scala/scala/build/options/BuildOptions.scala index 7ec5c92f9c..b0de3b6036 100644 --- a/modules/options/src/main/scala/scala/build/options/BuildOptions.scala +++ b/modules/options/src/main/scala/scala/build/options/BuildOptions.scala @@ -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 { diff --git a/modules/options/src/main/scala/scala/build/options/ScalaVersionUtil.scala b/modules/options/src/main/scala/scala/build/options/ScalaVersionUtil.scala index bcccdf247d..bd97fbd79c 100644 --- a/modules/options/src/main/scala/scala/build/options/ScalaVersionUtil.scala +++ b/modules/options/src/main/scala/scala/build/options/ScalaVersionUtil.scala @@ -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 { @@ -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 @@ -208,17 +204,13 @@ 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) ) @@ -226,16 +218,6 @@ object ScalaVersionUtil { } } - 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.")