Skip to content

Commit

Permalink
Add reading JAVA_OPTS and JDK_JAVA_OPTIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Aug 21, 2023
1 parent db25ae6 commit 4caebb3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ trait Cli extends SbtModule with ProtoBuildModule with CliLaunchers
def runClasspath = T {
super.runClasspath() ++ Seq(localRepoJar())
}

override def forkArgs: T[Seq[String]] = T {
super.forkArgs() ++ Seq("--add-opens=java.base/java.util=ALL-UNNAMED")
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions modules/cli/src/main/scala/scala/cli/ScalaCli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ object ScalaCli {
val jvmoptsLines = jvmoptsContent.linesIterator.toSeq
val (javaOpts, otherOpts) = jvmoptsLines.partition(_.startsWith("-D"))
javaOpts.foreach { opt =>
opt.stripPrefix("-D").split("=", 2).match {
opt.stripPrefix("-D").split("=", 2) match {
case Array(key, value) => System.setProperty(key, value)
case _ => System.err.println(s"Warning: Invalid java property: $opt")
}
Expand All @@ -192,11 +192,23 @@ object ScalaCli {
properties <- configDb.get(Keys.javaProperties).getOrElse(Nil)
}
properties.foreach { opt =>
opt.stripPrefix("-D").split("=", 2).match {
opt.stripPrefix("-D").split("=", 2) match {
case Array(key, value) => System.setProperty(key, value)
case _ => System.err.println(s"Warning: Invalid java property in config: $opt")
}
}

// load java properties from JAVA_OPTS and JDK_JAVA_OPTIONS environment variables
val javaOpts = sys.env.get("JAVA_OPTS").toSeq ++ sys.env.get("JDK_JAVA_OPTIONS").toSeq

javaOpts
.flatMap(_.split("\\s+"))
.foreach { opt =>
opt.stripPrefix("-D").split("=", 2) match {
case Array(key, value) => System.setProperty(key, value)
case _ => // Ignore if not a Java property
}
}
}

private def main0(args: Array[String]): Unit = {
Expand Down
36 changes: 34 additions & 2 deletions modules/cli/src/test/scala/cli/tests/SetupScalaCLITests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cli.tests

import com.eed3si9n.expecty.Expecty.expect

import java.util.Properties
import java.util.{Collections, Properties}

import scala.build.internal.Constants
import scala.build.tests.TestInputs
import scala.cli.ScalaCli
import scala.util.Properties
import scala.jdk.CollectionConverters.{MapHasAsJava, MapHasAsScala}

class SetupScalaCLITests extends munit.FunSuite {

Expand All @@ -32,4 +32,36 @@ class SetupScalaCLITests extends munit.FunSuite {
System.setProperties(currentProps)
)
}

test(s"should read java properties from JAVA_OPTS and JDK_JAVA_OPTIONS") {
// Adapted from https://stackoverflow.com/a/496849
def setEnvVars(newEnv: Map[String, String]): Unit = {
val classes = classOf[Collections].getDeclaredClasses
val env = System.getenv()
for (cl <- classes)
if (cl.getName.equals("java.util.Collections$UnmodifiableMap")) {
val field = cl.getDeclaredField("m")
field.setAccessible(true)
val obj = field.get(env)
val map = obj.asInstanceOf[java.util.Map[String, String]]
map.clear()
map.putAll(newEnv.asJava)
}
}

val javaOptsValues = " -Xmx2048m -Dhttp.proxy=4.4.4.4 -Xss8m"
val jdkJavaOptionsValues = " -Xmx1024m -Dscala-cli=true -Xss4m"

TestInputs().fromRoot(root =>
//
val currentEnv = System.getenv().asScala.toMap
// modify environment variable of this process
setEnvVars(Map("JAVA_OPTS" -> javaOptsValues, "JDK_JAVA_OPTIONS" -> jdkJavaOptionsValues))
ScalaCli.loadJavaProperties(root)
expect(sys.props.get("http.proxy").contains("4.4.4.4"))
expect(sys.props.get("scala-cli").contains("true"))
// reset the env
setEnvVars(currentEnv)
)
}
}

0 comments on commit 4caebb3

Please sign in to comment.