From 94a118cccf1afb250fc0b337fd38c5123825e1a5 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Mon, 28 Oct 2024 16:48:11 +0100 Subject: [PATCH] improvement: Don't add release flag for versions from 17 And also filter out if it exists. I noticed some issues with Scala 2.13.15 and I am not sure how to work around it. This can be worked around if the user needs it as Metals does work with 17 and up. For reference: - https://github.com/scala/bug/issues/13045 - https://github.com/scalameta/metals/issues/5272 --- .../metals/CompilerConfiguration.scala | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala b/metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala index 48d4acd4e30..e2d1edbfa0a 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala @@ -396,18 +396,40 @@ class CompilerConfiguration( } yield jvmVersion.major releaseVersion match { - case Some(version) => + // https://github.com/scala/bug/issues/13045 + case Some(version) + if version < 17 && scalaTarget.scalaBinaryVersion == "2.13" => /* Filter out -target: and -Xtarget: options, since they are not relevant and * might interfere with -release option */ val filterOutTarget = scalacOptions.filterNot(opt => opt.startsWith("-target:") || opt.startsWith("-Xtarget:") ) filterOutTarget ++ List("-release", version.toString()) - case _ => scalacOptions + case _ if scalaTarget.scalaBinaryVersion == "2.13" => + removeReleaseOptions(scalacOptions) + case _ => + scalacOptions } } } + private def isHigherThan17(version: String) = + Try(version.toInt).toOption.exists(_ >= 17) + + private def removeReleaseOptions(options: Seq[String]): Seq[String] = { + options match { + case "-release" :: version :: tail if isHigherThan17(version) => + removeReleaseOptions(tail) + case opt :: tail + if opt.startsWith("-release") && isHigherThan17( + opt.stripPrefix("-release:") + ) => + removeReleaseOptions(tail) + case head :: tail => head +: removeReleaseOptions(tail) + case Nil => options + } + } + private def log: List[String] = if (config.initialConfig.compilers.debug) { List(