Skip to content

Commit 23a9f10

Browse files
committed
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: - scala/bug#13045 - scalameta#5272
1 parent 6453328 commit 23a9f10

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

metals/src/main/scala/scala/meta/internal/metals/CompilerConfiguration.scala

+24-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import scala.meta.pc.SymbolSearch
2525

2626
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
2727
import org.eclipse.lsp4j.InitializeParams
28+
import scala.meta.internal.metals.MtagsBinaries.BuildIn.scalaVersion
2829

2930
class CompilerConfiguration(
3031
workspace: AbsolutePath,
@@ -396,18 +397,39 @@ class CompilerConfiguration(
396397
} yield jvmVersion.major
397398

398399
releaseVersion match {
399-
case Some(version) =>
400+
// https://github.com/scala/bug/issues/13045
401+
case Some(version) if version < 17 && scalaVersion.startsWith("2.13") =>
400402
/* Filter out -target: and -Xtarget: options, since they are not relevant and
401403
* might interfere with -release option */
402404
val filterOutTarget = scalacOptions.filterNot(opt =>
403405
opt.startsWith("-target:") || opt.startsWith("-Xtarget:")
404406
)
405407
filterOutTarget ++ List("-release", version.toString())
406-
case _ => scalacOptions
408+
case _ if scalaVersion.startsWith("2.13") =>
409+
removeReleaseOptions(scalacOptions)
410+
case _ =>
411+
scalacOptions
407412
}
408413
}
409414
}
410415

416+
private def isHigherThan17(version: String) =
417+
Try(version.toInt).toOption.exists(_ >= 17)
418+
419+
private def removeReleaseOptions(options: Seq[String]): Seq[String] = {
420+
options match {
421+
case "-release" :: version :: tail if isHigherThan17(version) =>
422+
removeReleaseOptions(tail)
423+
case opt :: tail
424+
if opt.startsWith("-release") && isHigherThan17(
425+
opt.stripPrefix("-release:")
426+
) =>
427+
removeReleaseOptions(tail)
428+
case head :: tail => head +: removeReleaseOptions(tail)
429+
case Nil => options
430+
}
431+
}
432+
411433
private def log: List[String] =
412434
if (config.initialConfig.compilers.debug) {
413435
List(

0 commit comments

Comments
 (0)