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(