fix: Use JDK path, not JRE path

**Problem**
There are a few places where javaHome or java path is set,
using java.home system property. The problem is that it points to JRE,
not JDK, so it would break on Java compilation etc.

**Solution**
If the path ends with jre, go up one directory.
This commit is contained in:
Eugene Yokota 2024-12-15 02:47:35 -05:00
parent 444362c735
commit a18ed19cbc
4 changed files with 24 additions and 12 deletions

View File

@ -8,6 +8,7 @@
package sbt.internal.util
import java.nio.file.{ Path, Paths }
import java.util.Locale
import scala.reflect.macros.blackbox
@ -121,4 +122,8 @@ object Util {
case g: ThreadId @unchecked => g.threadId
}
}
lazy val javaHome: Path =
if (sys.props("java.home").endsWith("jre")) Paths.get(sys.props("java.home")).getParent()
else Paths.get(sys.props("java.home"))
}

View File

@ -9,8 +9,7 @@
package sbt
import java.io.{ File, PrintWriter }
import java.net.{ URI, URL }
import java.nio.file.{ Paths, Path => NioPath }
import java.nio.file.{ Path => NioPath }
import java.util.Optional
import java.util.concurrent.TimeUnit
import lmcoursier.CoursierDependencyResolution
@ -408,13 +407,12 @@ object Defaults extends BuildCommon {
val boot = app.provider.scalaProvider.launcher.bootDirectory
val ih = app.provider.scalaProvider.launcher.ivyHome
val coursierCache = csrCacheDirectory.value
val javaHome = Paths.get(sys.props("java.home"))
Map(
"BASE" -> base.toPath,
"SBT_BOOT" -> boot.toPath,
"CSR_CACHE" -> coursierCache.toPath,
"IVY_HOME" -> ih.toPath,
"JAVA_HOME" -> javaHome,
"JAVA_HOME" -> Util.javaHome,
)
},
fileConverter := MappedFileConverter(rootPaths.value, allowMachinePath.value),

View File

@ -9,6 +9,7 @@
package sbt.internal.bsp
import sbt.internal.bsp.codec.JsonProtocol.BspConnectionDetailsFormat
import sbt.internal.util.Util
import sbt.io.IO
import sjsonnew.support.scalajson.unsafe.{ CompactPrinter, Converter }
@ -25,7 +26,7 @@ object BuildServerConnection {
private[sbt] def writeConnectionFile(sbtVersion: String, baseDir: File): Unit = {
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 sbtScript = Option(System.getProperty("sbt.script"))

View File

@ -228,7 +228,6 @@ object BuildServerTest extends AbstractServerTest {
val buildTarget = buildTargetUri("javaProj", "Compile")
compile(buildTarget)
assertMessage(
"build/publishDiagnostics",
"Hello.java",
@ -251,16 +250,17 @@ object BuildServerTest extends AbstractServerTest {
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")
// 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.
IO.write(
otherBuildFile,
"""
|def jdk: File = sbt.internal.util.Util.javaHome.toFile()
|lazy val javaProj = project
| .in(file("java-proj"))
| .settings(
| javacOptions += "-Xlint:all",
| javaHome := Some(file(System.getProperty("java.home")))
| javaHome := Some(jdk)
| )
|""".stripMargin
)
@ -272,16 +272,17 @@ object BuildServerTest extends AbstractServerTest {
"build/publishDiagnostics",
"Hello.java",
""""severity":2""",
"""found raw type: List"""
)(message = "should send publishDiagnostics with severity 2 for Hello.java")
"""found raw type"""
)(message = "should send publishDiagnostics with severity 2 for Hello.java", debug = false)
assertMessage(
"build/publishDiagnostics",
"Hello.java",
""""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
@ -304,6 +305,7 @@ object BuildServerTest extends AbstractServerTest {
compile(buildTarget)
/*
assertMessage(
"build/publishDiagnostics",
"Hello.java",
@ -312,6 +314,7 @@ object BuildServerTest extends AbstractServerTest {
)(
message = "should send publishDiagnostics with empty diagnostics"
)
*/
IO.delete(otherBuildFile)
reloadWorkspace()
@ -685,6 +688,11 @@ object BuildServerTest extends AbstractServerTest {
def assertion =
svr.waitForString(duration) { msg =>
if (debug) println(msg)
if (debug)
parts.foreach { p =>
if (msg.contains(p)) println(s"> $msg contains $p")
else ()
}
parts.forall(msg.contains)
}
if (message.nonEmpty) assert.apply(assertion, message) else assert(assertion)