Skip to content

Commit

Permalink
improvement: Don't add release flag for versions from 17
Browse files Browse the repository at this point in the history
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:
- scala/bug#13045
- scalameta#5272
  • Loading branch information
tgodzik committed Oct 29, 2024
1 parent 6453328 commit 23a9f10
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.meta.pc.SymbolSearch

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import org.eclipse.lsp4j.InitializeParams
import scala.meta.internal.metals.MtagsBinaries.BuildIn.scalaVersion

class CompilerConfiguration(
workspace: AbsolutePath,
Expand Down Expand Up @@ -396,18 +397,39 @@ class CompilerConfiguration(
} yield jvmVersion.major

releaseVersion match {
case Some(version) =>
// https://github.com/scala/bug/issues/13045
case Some(version) if version < 17 && scalaVersion.startsWith("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 scalaVersion.startsWith("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(
Expand Down

0 comments on commit 23a9f10

Please sign in to comment.