Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly parse java version #2291

Merged
merged 1 commit into from Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 24 additions & 17 deletions backend/src/main/scala/bloop/Compiler.scala
Expand Up @@ -40,6 +40,7 @@ import sbt.util.InterfaceUtil
import xsbti.T2
import xsbti.VirtualFileRef
import xsbti.compile._
import scala.util.Try

case class CompileInputs(
scalaInstance: ScalaInstance,
Expand Down Expand Up @@ -653,27 +654,33 @@ object Compiler {
case None => scalacOptions
case Some(_) if existsReleaseSetting || sameHome => scalacOptions
case Some(version) =>
try {
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
if (bloopNumVer > numVer) {
scalacOptions ++ List("-release", numVer.toString())
} else if (bloopNumVer < numVer) {
logger.warn(
s"Bloop is running with ${JavaRuntime.version} but your code requires $version to compile, " +
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
)
scalacOptions
} else {
scalacOptions
val options: Option[Array[String]] =
for {
numVer <- parseJavaVersion(version)
bloopNumVer <- parseJavaVersion(JavaRuntime.version)
if (bloopNumVer >= 9 && numVer != bloopNumVer)
} yield {
if (bloopNumVer > numVer) {
scalacOptions ++ List("-release", numVer.toString())
} else {
logger.warn(
s"Bloop is running with ${JavaRuntime.version} but your code requires $version to compile, " +
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
)
scalacOptions
}
}
} catch {
case NonFatal(_) =>
scalacOptions
}
options.getOrElse(scalacOptions)
}
}

private def parseJavaVersion(version: String): Option[Int] =
version.split('-').head.split('.').toList match {
case "1" :: minor :: _ => Try(minor.toInt).toOption
case single :: _ => Try(single.toInt).toOption
case _ => None
}

private def getCompilationOptions(
inputs: CompileInputs,
logger: Logger,
Expand Down