Skip to content

Commit

Permalink
fix: Use JDK path, not JRE path
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
eed3si9n committed Mar 3, 2025
1 parent 444362c commit 106135e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package sbt.internal.util

import java.nio.file.{ Path, Paths }
import java.util.Locale

import scala.reflect.macros.blackbox
Expand Down Expand Up @@ -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"))
}
6 changes: 2 additions & 4 deletions main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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"))
Expand Down
20 changes: 13 additions & 7 deletions server-test/src/test/scala/testpkg/BuildServerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ object BuildServerTest extends AbstractServerTest {
val buildTarget = buildTargetUri("javaProj", "Compile")

compile(buildTarget)

assertMessage(
"build/publishDiagnostics",
"Hello.java",
Expand All @@ -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.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
)
Expand All @@ -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…

Expand Down Expand Up @@ -685,6 +686,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)
Expand Down

0 comments on commit 106135e

Please sign in to comment.