Skip to content

Commit a708ae9

Browse files
authored
Merge pull request #3169 from Gedochao/maintenance/fix-java-props-setup-ide
Ensure that passing Java props into Scala CLI as launcher args would also pass it into BSP configuration
2 parents 142163b + 6bfd690 commit a708ae9

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ object ScalaCli {
6565
def getDefaultScalaVersion: String =
6666
launcherOptions.scalaRunner.cliUserScalaVersion.getOrElse(Constants.defaultScalaVersion)
6767

68+
private var launcherJavaPropArgs: List[String] = List.empty
69+
70+
def getLauncherJavaPropArgs: List[String] = launcherJavaPropArgs
71+
6872
private def partitionArgs(args: Array[String]): (Array[String], Array[String]) = {
6973
val systemProps = args.takeWhile(_.startsWith("-D"))
7074
(systemProps, args.drop(systemProps.size))
@@ -294,6 +298,7 @@ object ScalaCli {
294298
}
295299
}
296300
val (systemProps, scalaCliArgs) = partitionArgs(remainingArgs)
301+
if systemProps.nonEmpty then launcherJavaPropArgs = systemProps.toList
297302
setSystemProps(systemProps)
298303

299304
(new BouncycastleSignerMaker).maybeInit()

modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
4141
private val globalOptionsAtomic: AtomicReference[GlobalOptions] =
4242
new AtomicReference(GlobalOptions.default)
4343

44-
private def globalOptions: GlobalOptions = globalOptionsAtomic.get()
45-
protected def launcherOptions: LauncherOptions = ScalaCli.launcherOptions
46-
protected def defaultScalaVersion: String = ScalaCli.getDefaultScalaVersion
44+
private def globalOptions: GlobalOptions = globalOptionsAtomic.get()
45+
protected def launcherOptions: LauncherOptions = ScalaCli.launcherOptions
46+
protected def defaultScalaVersion: String = ScalaCli.getDefaultScalaVersion
47+
protected def launcherJavaPropArgs: List[String] = ScalaCli.getLauncherJavaPropArgs
4748

4849
def sharedOptions(t: T): Option[SharedOptions] = // hello borked unused warning
4950
None

modules/cli/src/main/scala/scala/cli/commands/setupide/SetupIde.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
162162
val bspArgs =
163163
List(launcher) ++
164164
finalLauncherOptions.toCliArgs ++
165+
launcherJavaPropArgs ++
165166
List("bsp") ++
166167
debugOpt ++
167168
List("--json-options", scalaCliBspJsonDestination.toString) ++

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
21852185
) {
21862186
val scriptName = "cli-version.sc"
21872187
val inputs = TestInputs(
2188-
os.rel / scriptName -> s"""println("Hello from launcher v$cliVersion"""
2188+
os.rel / scriptName -> s"""println("Hello from launcher v$cliVersion")"""
21892189
)
21902190
inputs.fromRoot { root =>
21912191
val cliVersionArgs = List("--cli-version", cliVersion)
@@ -2202,4 +2202,17 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
22022202
expect(bspConfig.argv.indexOfSlice(cliVersionArgs) < bspConfig.argv.indexOf("bsp"))
22032203
}
22042204
}
2205+
2206+
test("setup-ide passes Java props to the BSP configuration correctly") {
2207+
val scriptName = "hello.sc"
2208+
TestInputs(os.rel / scriptName -> s"""println("Hello")""").fromRoot { root =>
2209+
val javaProps = List("-Dfoo=bar", "-Dbar=baz")
2210+
os.proc(TestUtil.cli, javaProps, "setup-ide", scriptName, extraOptions)
2211+
.call(cwd = root)
2212+
val bspConfig = readBspConfig(root)
2213+
expect(bspConfig.argv.head == TestUtil.cliPath)
2214+
expect(bspConfig.argv.containsSlice(javaProps))
2215+
expect(bspConfig.argv.indexOfSlice(javaProps) < bspConfig.argv.indexOf("bsp"))
2216+
}
2217+
}
22052218
}

0 commit comments

Comments
 (0)