Merge pull request #7948 from eed3si9n/wip/java-home

[2.x] fix: Use JDK path, not JRE path
This commit is contained in:
eugene yokota 2024-12-15 21:47:57 -05:00 committed by GitHub
commit 6d0b43a2b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 16 deletions

View File

@ -8,11 +8,12 @@
package sbt.internal.util package sbt.internal.util
import java.nio.file.{ Path, Paths }
import java.util.Locale import java.util.Locale
import scala.collection.concurrent.TrieMap import scala.collection.concurrent.TrieMap
object Util { object Util:
def makeList[T](size: Int, value: T): List[T] = List.fill(size)(value) def makeList[T](size: Int, value: T): List[T] = List.fill(size)(value)
def separate[T, A, B](ps: Seq[T])(f: T => Either[A, B]): (Seq[A], Seq[B]) = { def separate[T, A, B](ps: Seq[T])(f: T => Either[A, B]): (Seq[A], Seq[B]) = {
@ -78,4 +79,11 @@ object Util {
val cache = TrieMap.empty[A1, A2] val cache = TrieMap.empty[A1, A2]
x => cache.getOrElseUpdate(x, f(x)) x => cache.getOrElseUpdate(x, f(x))
} }
}
lazy val javaHome: Path =
sys.env.get("JAVA_HOME") match
case Some(home) => Paths.get(home)
case None =>
if sys.props("java.home").endsWith("jre") then Paths.get(sys.props("java.home")).getParent()
else Paths.get(sys.props("java.home"))
end Util

View File

@ -9,7 +9,7 @@
package sbt package sbt
import java.io.{ File, PrintWriter } import java.io.{ File, PrintWriter }
import java.nio.file.{ Files, Paths, Path => NioPath } import java.nio.file.{ Files, Path => NioPath }
import java.util.Optional import java.util.Optional
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import lmcoursier.CoursierDependencyResolution import lmcoursier.CoursierDependencyResolution
@ -245,13 +245,12 @@ object Defaults extends BuildCommon {
val base = app.baseDirectory.getCanonicalFile.toPath val base = app.baseDirectory.getCanonicalFile.toPath
val boot = app.provider.scalaProvider.launcher.bootDirectory.toPath val boot = app.provider.scalaProvider.launcher.bootDirectory.toPath
val ih = app.provider.scalaProvider.launcher.ivyHome.toPath val ih = app.provider.scalaProvider.launcher.ivyHome.toPath
val javaHome = Paths.get(sys.props("java.home"))
ListMap( ListMap(
"OUT" -> out, "OUT" -> out,
"BASE" -> base, "BASE" -> base,
"SBT_BOOT" -> boot, "SBT_BOOT" -> boot,
"IVY_HOME" -> ih, "IVY_HOME" -> ih,
"JAVA_HOME" -> javaHome "JAVA_HOME" -> Util.javaHome,
) )
private[sbt] lazy val globalIvyCore: Seq[Setting[?]] = private[sbt] lazy val globalIvyCore: Seq[Setting[?]] =

View File

@ -33,7 +33,7 @@ import xsbti.{ FileConverter, HashedVirtualFileRef, VirtualFile }
import xsbti.compile.{ ClasspathOptionsUtil, Compilers } import xsbti.compile.{ ClasspathOptionsUtil, Compilers }
import java.io.File import java.io.File
import java.net.URI import java.net.URI
import java.nio.file.{ Path, Paths } import java.nio.file.Path
import scala.annotation.tailrec import scala.annotation.tailrec
import scala.collection.mutable import scala.collection.mutable
import sbt.internal.util.Util import sbt.internal.util.Util
@ -71,7 +71,7 @@ private[sbt] object Load {
val scalaProvider = app.provider.scalaProvider val scalaProvider = app.provider.scalaProvider
val launcher = scalaProvider.launcher val launcher = scalaProvider.launcher
val stagingDirectory = getStagingDirectory(state, globalBase).getCanonicalFile val stagingDirectory = getStagingDirectory(state, globalBase).getCanonicalFile
val javaHome = Paths.get(sys.props("java.home")) val javaHome = Util.javaHome
val out = baseDirectory.toPath.resolve("target").resolve("out") val out = baseDirectory.toPath.resolve("target").resolve("out")
val rootPaths = Map( val rootPaths = Map(
"OUT" -> out, "OUT" -> out,

View File

@ -9,6 +9,7 @@
package sbt.internal.bsp package sbt.internal.bsp
import sbt.internal.bsp.codec.JsonProtocol.BspConnectionDetailsFormat import sbt.internal.bsp.codec.JsonProtocol.BspConnectionDetailsFormat
import sbt.internal.util.Util
import sbt.io.IO import sbt.io.IO
import sjsonnew.support.scalajson.unsafe.{ CompactPrinter, Converter } import sjsonnew.support.scalajson.unsafe.{ CompactPrinter, Converter }
@ -25,7 +26,7 @@ object BuildServerConnection {
private[sbt] def writeConnectionFile(sbtVersion: String, baseDir: File): Unit = { private[sbt] def writeConnectionFile(sbtVersion: String, baseDir: File): Unit = {
val bspConnectionFile = new File(baseDir, ".bsp/sbt.json") val bspConnectionFile = new File(baseDir, ".bsp/sbt.json")
val javaHome = System.getProperty("java.home") val javaHome = Util.javaHome
val classPath = System.getProperty("java.class.path") val classPath = System.getProperty("java.class.path")
val sbtScript = Option(System.getProperty("sbt.script")) val sbtScript = Option(System.getProperty("sbt.script"))

View File

@ -234,7 +234,6 @@ class BuildServerTest extends AbstractServerTest {
val buildTarget = buildTargetUri("javaProj", "Compile") val buildTarget = buildTargetUri("javaProj", "Compile")
compile(buildTarget) compile(buildTarget)
assertMessage( assertMessage(
"build/publishDiagnostics", "build/publishDiagnostics",
"Hello.java", "Hello.java",
@ -257,16 +256,17 @@ class BuildServerTest extends AbstractServerTest {
val testFile = new File(svr.baseDirectory, s"java-proj/src/main/java/example/Hello.java") val testFile = new File(svr.baseDirectory, s"java-proj/src/main/java/example/Hello.java")
val otherBuildFile = new File(svr.baseDirectory, "force-java-out-of-process-compiler.sbt") val otherBuildFile = new File(svr.baseDirectory, "force-java-out-of-process-compiler.sbt")
// Setting `javaHome` will force SBT to shell out to an external Java compiler instead // Setting `javaHome` will force sbt to shell out to an external Java compiler instead
// of using the local compilation service offered by the JVM running this SBT instance. // of using the local compilation service offered by the JVM running this SBT instance.
IO.write( IO.write(
otherBuildFile, otherBuildFile,
""" """
|def jdk: File = sbt.internal.Util.javaHome.toFile()
|lazy val javaProj = project |lazy val javaProj = project
| .in(file("java-proj")) | .in(file("java-proj"))
| .settings( | .settings(
| javacOptions += "-Xlint:all", | javacOptions += "-Xlint:all",
| javaHome := Some(file(System.getProperty("java.home"))) | javaHome := Some(jdk)
| ) | )
|""".stripMargin |""".stripMargin
) )
@ -278,16 +278,17 @@ class BuildServerTest extends AbstractServerTest {
"build/publishDiagnostics", "build/publishDiagnostics",
"Hello.java", "Hello.java",
""""severity":2""", """"severity":2""",
"""found raw type: List""" """found raw type"""
)(message = "should send publishDiagnostics with severity 2 for Hello.java") )(message = "should send publishDiagnostics with severity 2 for Hello.java", debug = false)
assertMessage( assertMessage(
"build/publishDiagnostics", "build/publishDiagnostics",
"Hello.java", "Hello.java",
""""severity":1""", """"severity":1""",
"""incompatible types: int cannot be converted to String""" """incompatible types: int cannot be converted"""
)( )(
message = "should send publishDiagnostics with severity 1 for Hello.java" message = "should send publishDiagnostics with severity 1 for Hello.java",
debug = true
) )
// Note the messages changed slightly in both cases. That's interesting // Note the messages changed slightly in both cases. That's interesting
@ -690,9 +691,14 @@ class BuildServerTest extends AbstractServerTest {
def assertion = def assertion =
svr.waitForString(duration) { msg => svr.waitForString(duration) { msg =>
if (debug) println(msg) if (debug) println(msg)
if debug then
parts.foreach: p =>
if msg.contains(p) then println(s"> $msg contains $p")
else ()
parts.forall(msg.contains) parts.forall(msg.contains)
} }
if (message.nonEmpty) assert(assertion, message) else assert(assertion) if message.nonEmpty then assert(assertion, message)
else assert(assertion)
} }
private def reloadWorkspace(): Int = private def reloadWorkspace(): Int =