Skip to content

Commit 1021d42

Browse files
committed
refactor: Extract adjustScalacReleaseOptions method
1 parent d4226a3 commit 1021d42

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -604,54 +604,73 @@ object Compiler {
604604
}
605605
}
606606

607-
private def getCompilationOptions(
608-
inputs: CompileInputs,
609-
logger: Logger,
610-
newClassesDir: Path
611-
): CompileOptions = {
612-
// Sources are all files
613-
val sources = inputs.sources.map(path => converter.toVirtualFile(path.underlying))
614-
val classpath = inputs.classpath.map(path => converter.toVirtualFile(path.underlying))
615-
def existsReleaseSetting = inputs.scalacOptions.exists(opt =>
607+
/**
608+
* Bloop runs Scala compilation in the same process as the main server,
609+
* so the compilation process will use the same JDK that Bloop is using.
610+
* That's why we must ensure that produce class files will be compliant with expected JDK version
611+
* and compilation errors will show up when using wrong JDK API.
612+
*/
613+
private def adjustScalacReleaseOptions(
614+
scalacOptions: Array[String],
615+
javacBin: Option[AbsolutePath],
616+
logger: Logger
617+
): Array[String] = {
618+
def existsReleaseSetting = scalacOptions.exists(opt =>
616619
opt.startsWith("-release") ||
617620
opt.startsWith("--release") ||
618621
opt.startsWith("-java-output-version")
619622
)
620-
def sameHome = inputs.javacBin match {
623+
def sameHome = javacBin match {
621624
case Some(bin) => bin.getParent.getParent == JavaRuntime.home
622625
case None => false
623626
}
624627

625-
val scalacOptions = inputs.javacBin.flatMap(binary =>
628+
javacBin.flatMap(binary =>
626629
// <JAVA_HOME>/bin/java
627630
JavaRuntime.getJavaVersionFromJavaHome(binary.getParent.getParent)
628631
) match {
629-
case None => inputs.scalacOptions
630-
case Some(_) if existsReleaseSetting || sameHome => inputs.scalacOptions
632+
case None => scalacOptions
633+
case Some(_) if existsReleaseSetting || sameHome => scalacOptions
631634
case Some(version) =>
632635
try {
633636
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
634637
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
635638
if (bloopNumVer > numVer) {
636-
inputs.scalacOptions ++ List("-release", numVer.toString())
639+
scalacOptions ++ List("-release", numVer.toString())
637640
} else {
638641
logger.warn(
639642
s"Bloop is runing with ${JavaRuntime.version} but your code requires $version to compile, " +
640643
"this might cause some compilation issues when using JDK API unsupported by the Bloop's current JVM version"
641644
)
642-
inputs.scalacOptions
645+
scalacOptions
643646
}
644647
} catch {
645648
case NonFatal(_) =>
646-
inputs.scalacOptions
649+
scalacOptions
647650
}
648651
}
652+
}
653+
654+
private def getCompilationOptions(
655+
inputs: CompileInputs,
656+
logger: Logger,
657+
newClassesDir: Path
658+
): CompileOptions = {
659+
// Sources are all files
660+
val sources = inputs.sources.map(path => converter.toVirtualFile(path.underlying))
661+
val classpath = inputs.classpath.map(path => converter.toVirtualFile(path.underlying))
662+
663+
val scalacOptions = adjustScalacReleaseOptions(
664+
scalacOptions = inputs.scalacOptions,
665+
javacBin = inputs.javacBin,
666+
logger = logger
667+
)
649668

650669
val optionsWithoutFatalWarnings = scalacOptions.filter(_ != "-Xfatal-warnings")
651-
val isFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length
670+
val areFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length
652671

653672
// Enable fatal warnings in the reporter if they are enabled in the build
654-
if (isFatalWarningsEnabled)
673+
if (areFatalWarningsEnabled)
655674
inputs.reporter.enableFatalWarnings()
656675

657676
CompileOptions

backend/src/main/scala/bloop/util/JavaRuntime.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.typesafe.config.ConfigFactory
1414
import com.typesafe.config.ConfigParseOptions
1515
import com.typesafe.config.ConfigSyntax
1616
import scala.collection.concurrent.TrieMap
17+
import scala.util.Properties
1718

1819
sealed trait JavaRuntime
1920
object JavaRuntime {
@@ -74,7 +75,8 @@ object JavaRuntime {
7475
* appropriately.
7576
*/
7677
def javacBinaryFromJavaHome(home: AbsolutePath): Option[AbsolutePath] = {
77-
def toJavaBinary(home: AbsolutePath) = home.resolve("bin").resolve("javac")
78+
val binaryName = if (Properties.isWin) "javac.exe" else "javac"
79+
def toJavaBinary(home: AbsolutePath) = home.resolve("bin").resolve(binaryName)
7880
if (!home.exists) None
7981
else {
8082
Option(toJavaBinary(home))

frontend/src/test/scala/bloop/JavaVersionSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object JavaVersionSpec extends bloop.testing.BaseSuite {
3737
| value repeat is not a member of String
3838
| L2: val output = "La ".repeat(2) + "Land";
3939
| ^
40-
|a/src/main/scala/Foo.scala: L2 [E1]
40+
|$targetFoo: L2 [E1]
4141
|Failed to compile 'a'
4242
|""".stripMargin
4343
)

0 commit comments

Comments
 (0)