Skip to content

Commit

Permalink
refactor: Extract adjustScalacReleaseOptions method
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Oct 12, 2023
1 parent 03c5355 commit 13b7606
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
55 changes: 37 additions & 18 deletions backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -604,54 +604,73 @@ object Compiler {
}
}

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

val scalacOptions = inputs.javacBin.flatMap(binary =>
javacBin.flatMap(binary =>
// <JAVA_HOME>/bin/java
JavaRuntime.getJavaVersionFromJavaHome(binary.getParent.getParent)
) match {
case None => inputs.scalacOptions
case Some(_) if existsReleaseSetting || sameHome => inputs.scalacOptions
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) {
inputs.scalacOptions ++ List("-release", numVer.toString())
scalacOptions ++ List("-release", numVer.toString())
} else {
logger.warn(
s"Bloop is runing 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"
)
inputs.scalacOptions
scalacOptions
}
} catch {
case NonFatal(_) =>
inputs.scalacOptions
scalacOptions
}
}
}

private def getCompilationOptions(
inputs: CompileInputs,
logger: Logger,
newClassesDir: Path
): CompileOptions = {
// Sources are all files
val sources = inputs.sources.map(path => converter.toVirtualFile(path.underlying))
val classpath = inputs.classpath.map(path => converter.toVirtualFile(path.underlying))

val scalacOptions = adjustScalacReleaseOptions(
scalacOptions = inputs.scalacOptions,
javacBin = inputs.javacBin,
logger = logger
)

val optionsWithoutFatalWarnings = scalacOptions.filter(_ != "-Xfatal-warnings")
val isFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length
val areFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length

// Enable fatal warnings in the reporter if they are enabled in the build
if (isFatalWarningsEnabled)
if (areFatalWarningsEnabled)
inputs.reporter.enableFatalWarnings()

CompileOptions
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/test/scala/bloop/JavaVersionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import bloop.config.Config
import bloop.logging.RecordingLogger
import bloop.util.TestProject
import bloop.util.TestUtil
import scala.util.Try
import bloop.util.JavaRuntime
import bloop.io.AbsolutePath

object JavaVersionSpec extends bloop.testing.BaseSuite {

private val jvmManager = coursierapi.JvmManager.create()

def checkFlag(scalacOpts: List[String], jdkVersion: String = "8") = {
val javaHome = jvmManager.get(jdkVersion).toPath()
Try {
val version = JavaRuntime.getJavaVersionFromJavaHome(AbsolutePath(javaHome)).get
println(version)
val numVer = if (version.startsWith("1.8")) 8 else version.takeWhile(_.isDigit).toInt
println(numVer)
val bloopNumVer = JavaRuntime.version.takeWhile(_.isDigit).toInt
println(bloopNumVer)
println(bloopNumVer > numVer)
}
val jvmConfig = Some(Config.JvmConfig(Some(javaHome), Nil))
TestUtil.withinWorkspace { workspace =>
val sources = List(
Expand All @@ -37,7 +49,7 @@ object JavaVersionSpec extends bloop.testing.BaseSuite {
| value repeat is not a member of String
| L2: val output = "La ".repeat(2) + "Land";
| ^
|a/src/main/scala/Foo.scala: L2 [E1]
|$targetFoo: L2 [E1]
|Failed to compile 'a'
|""".stripMargin
)
Expand Down

0 comments on commit 13b7606

Please sign in to comment.