Skip to content

Commit 77b4cba

Browse files
committed
Fix passing jvm options
1 parent c3ebebe commit 77b4cba

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

compiler/test-coursier/dotty/tools/coursier/CoursierScalaTests.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class CoursierScalaTests:
6060
assertTrue(output.mkString("\n").endsWith("scriptPath.sc"))
6161
scriptPath()
6262

63+
def scriptEnvDashJDashD() =
64+
val scriptPath = scripts("/scripting").find(_.getName == "envtest.sc").get.absPath
65+
val args = scriptPath
66+
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", args)
67+
assertEquals(output.mkString("\n"), "Hello World")
68+
scriptEnvDashJDashD()
69+
6370
def version() =
6471
val output = CoursierScalaTests.csScalaCmd("-version")
6572
assertTrue(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
@@ -75,6 +82,11 @@ class CoursierScalaTests:
7582
assertEquals(output.mkString("\n"), "Hello")
7683
run()
7784

85+
def runDashJDashD() =
86+
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", "-classpath", scripts("/run").head.getParentFile.getParent, "-run", "run.envtest")
87+
assertEquals(output.mkString("\n"), "Hello World")
88+
runDashJDashD()
89+
7890
def notOnlyOptionsEqualsRun() =
7991
val output = CoursierScalaTests.csScalaCmd("-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
8092
assertEquals(output.mkString("\n"), "Hello")
@@ -147,10 +159,12 @@ object CoursierScalaTests:
147159
csCmd("dotty.tools.dotc.Main", options*)
148160

149161
private def csCmd(entry: String, options: String*): List[String] =
150-
val newOptions = options match
151-
case Nil => options
152-
case _ => "--" +: options
153-
execCmd("./cs", (s"""launch "org.scala-lang:scala3-compiler_3:${sys.env("DOTTY_BOOTSTRAPPED_VERSION")}" --main-class "$entry" --property "scala.usejavacp=true"""" +: newOptions)*)
162+
val (jOpts, args) = options.partition(_.startsWith("-J"))
163+
val newOptions = args match
164+
case Nil => args
165+
case _ => "--" +: args
166+
val newJOpts = jOpts.map(s => s"--java-opt ${s.stripPrefix("-J")}").mkString(" ")
167+
execCmd("./cs", (s"""launch "org.scala-lang:scala3-compiler_3:${sys.env("DOTTY_BOOTSTRAPPED_VERSION")}" $newJOpts --main-class "$entry" --property "scala.usejavacp=true"""" +: newOptions)*)
154168

155169
/** Get coursier script */
156170
@BeforeClass def setup(): Unit =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package run
2+
3+
object envtest extends App:
4+
println("Hello " + sys.props("key"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!dist/target/pack/bin/scala
2+
3+
def main(args: Array[String]): Unit =
4+
println("Hello " + util.Properties.propOrNull("key"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package run
2+
3+
object envtest extends App:
4+
println("Hello " + sys.props("key"))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dotty.tools.repl
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
class EnvPropsTest extends ReplTest(options = Array("-J-Dkey=value", "-Dkey2=value2")) {
7+
@Test def fromDashJDashD = fromInitialState { implicit s =>
8+
run("util.Properties.propOrNull(\"key\")")
9+
assertTrue(storedOutput().contains("val res0: String = value"))
10+
}
11+
12+
@Test def fromDashD = fromInitialState { implicit s =>
13+
run("util.Properties.propOrNull(\"key2\")")
14+
assertTrue(storedOutput().contains("val res0: String = value2"))
15+
}
16+
}
17+

compiler/test/dotty/tools/scripting/BashScriptsTests.scala

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class BashScriptsTests:
2828
printf("scalac path: [%s]\n", scalacPath)
2929

3030
lazy val expectedOutput = List(
31-
"arg 0:[a]",
32-
"arg 1:[b]",
33-
"arg 2:[c]",
34-
"arg 3:[-repl]",
35-
"arg 4:[-run]",
36-
"arg 5:[-script]",
37-
"arg 6:[-debug]",
31+
"arg 0:[a]",
32+
"arg 1:[b]",
33+
"arg 2:[c]",
34+
"arg 3:[-repl]",
35+
"arg 4:[-run]",
36+
"arg 5:[-script]",
37+
"arg 6:[-debug]",
3838
)
3939
lazy val testScriptArgs = Seq(
4040
"a", "b", "c", "-repl", "-run", "-script", "-debug"
@@ -56,6 +56,18 @@ class BashScriptsTests:
5656
if fail then
5757
assert(stdout == expectedOutput)
5858

59+
/* verify `dist/bin/scala` with -J setting */
60+
@Test def verifyScalaJProperty =
61+
val commandline = Seq(scalaPath, "-J-Dkey=World", "-script", testFiles.find(_.getName == "envtest.sc").get.absPath).mkString(" ")
62+
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
63+
assertEquals(stdout.mkString("\n"), "Hello World")
64+
65+
/* verify `dist/bin/scala` with -J setting */
66+
@Test def verifyScalaJProperty =
67+
val commandline = Seq(scalaPath, "-J-Dkey=World", testFiles.find(_.getName == "envtest.scala").get.absPath).mkString(" ")
68+
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
69+
assertEquals(stdout.mkString("\n"), "Hello World")
70+
5971
/* verify `dist/bin/scala` non-interference with command line args following script name */
6072
@Test def verifyScalaArgs =
6173
val commandline = (Seq("SCALA_OPTS= ", scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
@@ -73,7 +85,7 @@ class BashScriptsTests:
7385
assert(stdout == expectedOutput)
7486

7587
/*
76-
* verify that scriptPath.sc sees a valid script.path property,
88+
* verify that scriptPath.sc sees a valid script.path property,
7789
* and that it's value is the path to "scriptPath.sc".
7890
*/
7991
@Test def verifyScriptPathProperty =
@@ -192,7 +204,7 @@ class BashScriptsTests:
192204
val envPairs = testEnvPairs ++ additionalEnvPairs
193205
val proc = Process(cmd, None, envPairs *)
194206
val exitVal = proc ! ProcessLogger (
195-
(out: String) => stdout ::= out,
207+
(out: String) => stdout ::= out,
196208
(err: String) => stderr ::= err
197209
)
198210
val validTest = exitVal == 0 && ! stderr.exists(_.contains("Permission denied"))

dist/bin/scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ source "$PROG_HOME/bin/common"
3131
# exec here would prevent onExit from being called, leaving terminal in unusable state
3232
compilerJavaClasspathArgs
3333
[ -z "${ConEmuPID-}" -o -n "${cygwin-}" ] && export MSYSTEM= PWD= # workaround for #12405
34-
eval "\"$JAVACMD\"" "-Dscala.home=$PROG_HOME" "-classpath \"$jvm_cp_args\"" "dotty.tools.MainGenericRunner" "-classpath \"$jvm_cp_args\"" "$@"
34+
echo "$@" | xargs -d' ' -n 1 | grep "^[^-J]"
35+
java_args=`echo "$@" | xargs -d' ' -n 1 | grep "^-J" | cut -c 3- | tr '\n' ' '`
36+
args=`echo "$@" | xargs -d' ' -n 1 | grep "^[^-J]" | tr '\n' ' '`
37+
eval "\"$JAVACMD\"" "$java_args" "-Dscala.home=$PROG_HOME" "-classpath \"$jvm_cp_args\"" "dotty.tools.MainGenericRunner" "-classpath \"$jvm_cp_args\"" "$args"
3538
scala_exit_status=$?
3639

3740

0 commit comments

Comments
 (0)