Skip to content

Commit 68ce528

Browse files
authored
Log a warning when invalid java properties are being passed to JAVA_OPTS or JDK_JAVA_OPTIONS env vars (#2843)
1 parent 7767dce commit 68ce528

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ object ScalaCli {
184184
}
185185
if otherOpts.nonEmpty then
186186
System.err.println(
187-
s"Warning: Only java properties are supported in .scala-jvmopts file. Other options are ignored: ${otherOpts.mkString(", ")} "
187+
s"Warning: Only java properties are supported in .scala-jvmopts file. Other options are ignored: ${otherOpts.mkString(", ")}"
188188
)
189189
// load java properties from config
190190
for {
@@ -201,14 +201,21 @@ object ScalaCli {
201201
// load java properties from JAVA_OPTS and JDK_JAVA_OPTIONS environment variables
202202
val javaOpts = sys.env.get("JAVA_OPTS").toSeq ++ sys.env.get("JDK_JAVA_OPTIONS").toSeq
203203

204-
javaOpts
205-
.flatMap(_.split("\\s+"))
206-
.foreach { opt =>
207-
opt.stripPrefix("-D").split("=", 2) match {
208-
case Array(key, value) => System.setProperty(key, value)
209-
case _ => // Ignore if not a Java property
210-
}
211-
}
204+
val ignoredJavaOpts =
205+
javaOpts
206+
.flatMap(_.split("\\s+"))
207+
.flatMap { opt =>
208+
opt.stripPrefix("-D").split("=", 2) match {
209+
case Array(key, value) =>
210+
System.setProperty(key, value)
211+
None
212+
case ignored => Some(ignored) // non-property opts are ignored here
213+
}
214+
}.flatten
215+
if ignoredJavaOpts.nonEmpty then
216+
System.err.println(
217+
s"Warning: Only java properties are supported in JAVA_OPTS and JDK_JAVA_OPTIONS environment variables. Other options are ignored: ${ignoredJavaOpts.mkString(", ")}"
218+
)
212219
}
213220

214221
private def main0(args: Array[String]): Unit = {

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,4 +2192,44 @@ abstract class RunTestDefinitions
21922192
expect(warningCount == 1)
21932193
}
21942194
}
2195+
2196+
test(s"warn about invalid values present in JAVA_OPTS") {
2197+
val expectedOutput = "Hello"
2198+
TestInputs(os.rel / "example.sc" -> s"println(\"$expectedOutput\")")
2199+
.fromRoot { root =>
2200+
val invalidOpt = "--invalid"
2201+
val validOpt = "-Dfoo=bar"
2202+
val res = os.proc(TestUtil.cli, "run", "example.sc", "--server=false", extraOptions)
2203+
.call(cwd = root, env = Map("JAVA_OPTS" -> s"$invalidOpt $validOpt"), stderr = os.Pipe)
2204+
val errOutput = res.err.trim()
2205+
expect(errOutput.contains(
2206+
s"Only java properties are supported in JAVA_OPTS"
2207+
))
2208+
expect(errOutput.contains(s"Other options are ignored: $invalidOpt"))
2209+
expect(!errOutput.contains(validOpt))
2210+
expect(res.out.trim() == expectedOutput)
2211+
}
2212+
}
2213+
2214+
test(s"warn about invalid values present in .scala-jvmopts") {
2215+
val expectedOutput = "Hello"
2216+
val invalidOpt = "--invalid"
2217+
val validOpt = "-Dfoo=bar"
2218+
TestInputs(
2219+
os.rel / "example.sc" -> s"println(\"$expectedOutput\")",
2220+
os.rel / ".scala-jvmopts" ->
2221+
s"""$invalidOpt
2222+
|$validOpt
2223+
|""".stripMargin
2224+
)
2225+
.fromRoot { root =>
2226+
val res = os.proc(TestUtil.cli, "run", "example.sc", extraOptions)
2227+
.call(cwd = root, stderr = os.Pipe)
2228+
val errOutput = res.err.trim()
2229+
expect(errOutput.contains(s"Only java properties are supported in .scala-jvmopts file"))
2230+
expect(errOutput.contains(s"Other options are ignored: $invalidOpt"))
2231+
expect(!errOutput.contains(validOpt))
2232+
expect(res.out.trim() == expectedOutput)
2233+
}
2234+
}
21952235
}

0 commit comments

Comments
 (0)