Skip to content

Commit 34ad4cc

Browse files
kasiaMarektgodzik
authored andcommitted
fix: correctly parse java version
1 parent 9192e6e commit 34ad4cc

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

backend/src/main/scala/bloop/Compiler.scala

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import sbt.util.InterfaceUtil
4040
import xsbti.T2
4141
import xsbti.VirtualFileRef
4242
import xsbti.compile.{CompilerCache => _, ScalaInstance => _, _}
43+
import scala.util.Try
4344

4445
case class CompileInputs(
4546
scalaInstance: ScalaInstance,
@@ -653,27 +654,33 @@ object Compiler {
653654
case None => scalacOptions
654655
case Some(_) if existsReleaseSetting || sameHome => scalacOptions
655656
case Some(version) =>
656-
try {
657-
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
658-
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
659-
if (bloopNumVer > numVer) {
660-
scalacOptions ++ List("-release", numVer.toString())
661-
} else if (bloopNumVer < numVer) {
662-
logger.warn(
663-
s"Bloop is running with ${JavaRuntime.version} but your code requires $version to compile, " +
664-
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
665-
)
666-
scalacOptions
667-
} else {
668-
scalacOptions
657+
val options: Option[Array[String]] =
658+
for {
659+
numVer <- parseJavaVersion(version)
660+
bloopNumVer <- parseJavaVersion(JavaRuntime.version)
661+
if (bloopNumVer >= 9 && numVer != bloopNumVer)
662+
} yield {
663+
if (bloopNumVer > numVer) {
664+
scalacOptions ++ List("-release", numVer.toString())
665+
} else {
666+
logger.warn(
667+
s"Bloop is running with ${JavaRuntime.version} but your code requires $version to compile, " +
668+
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
669+
)
670+
scalacOptions
671+
}
669672
}
670-
} catch {
671-
case NonFatal(_) =>
672-
scalacOptions
673-
}
673+
options.getOrElse(scalacOptions)
674674
}
675675
}
676676

677+
private def parseJavaVersion(version: String): Option[Int] =
678+
version.split('-').head.split('.').toList match {
679+
case "1" :: minor :: _ => Try(minor.toInt).toOption
680+
case single :: _ => Try(single.toInt).toOption
681+
case _ => None
682+
}
683+
677684
private def getCompilationOptions(
678685
inputs: CompileInputs,
679686
logger: Logger,

0 commit comments

Comments
 (0)