Skip to content

Commit 25bcba5

Browse files
committed
SI-7295 Fix windows batch file with args containing parentheses
In command scripts, substitution of `FOO` in `if cond ( %FOO% )` happens *before* the condition is evaluated. One can use delayed expansion with `if cond (!FOO!)` to get a saner behaviour. Or, as I ended up doing here, use a goto in the body of the if rather than referring directly to variables there. Here's a cut down version to demonstrate the old problem: C:\Users\IEUser>type test.cmd @echo off setlocal enableextensions enabledelayedexpansion if [%~1]==[-toolcp] ( set CP=%~2 shift shift ) echo -toolcp %CP% echo %~1 %~2 C:\Users\IEUser>test.cmd a b -toolcp a b C:\Users\IEUser>test.cmd -toolcp "c:\program files" a b -toolcp c:\program files a b C:\Users\IEUser>test.cmd -toolcp "c:\program files" "a()b" "c()d" -toolcp c:\program files a()b c()d C:\Users\IEUser>test.cmd "a()b" "c()d" d was unexpected at this time. I don't understand exactly why the parentheses only mess things up in this situation. But regardless, lets find another way. My first attempt to fix this was based on the suggestion in the ticket. But, as shown below, this fails to capture the -toolcp. C:\Users\IEUser>type test.cmd @echo off setlocal enableextensions enabledelayedexpansion if [%~1]==[-toolcp] ( set CP=!2! shift shift ) echo -toolcp %CP% echo %~1 %~2 C:\Users\IEUser>test.cmd "a()b" "c()d" -toolcp a()b c()d C:\Users\IEUser>test.cmd -toolcp "c:\program files" "a()b" "c()d" -toolcp a()b c()d Last stop was the goto you'll find in this patch. With this patch applied, I tested on Windows 8 with the following: C:\Users\IEUser>type Desktop\temp.cmd ::#! @echo off call scala %0 %* goto :eof ::!# println("hello, world") println(argv.toList) C:\Users\IEUser>scala Desktop\temp.cmd "foo(bar)baz" "java" -Xmx256M -Xms32M -Dscala.home="C:\PROGRA~3\scala\bin\.." -Denv.emacs="" -Dscala.usejavacp=true -cp "..." scala.tools.nsc.MainGenericRunner Desktop\temp.cmd "foo(bar)baz" hello, world List(foo(bar)baz) C:\Users\IEUser>scala -toolcp "c:\program files" Desktop\temp.cmd "foo(bar)baz" "java" -Xmx256M -Xms32M -Dscala.home="C:\PROGRA~3\scala\bin\.." -Denv.emacs="" -Dscala.usejavacp=true -cp "...;c:\program files" scala.tools.nsc.MainGenericRunner -toolcp "c:\program files" Desktop\temp.cmd "foo(bar)baz" hello, world List(foo(bar)baz)
1 parent 371bad2 commit 25bcba5

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/compiler/scala/tools/ant/templates/tool-windows.tmpl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ setlocal enableextensions enabledelayedexpansion
1212

1313
set _LINE_TOOLCP=
1414

15-
:another_param
16-
1715
rem Use "%~1" to handle spaces in paths. See http://ss64.com/nt/syntax-args.html
18-
if "%~1"=="-toolcp" (
19-
set _LINE_TOOLCP=%~2
20-
shift
21-
shift
22-
goto another_param
16+
rem SI-7295 The goto here is needed to avoid problems with `scala Script.cmd "arg(with)paren"`,
17+
rem we must not evaluate %~2 eagerly, but delayed expansion doesn't seem to allow
18+
rem removal of quotation marks.
19+
if not [%~1]==[-toolcp] (
20+
goto :notoolcp
2321
)
22+
shift
23+
set _LINE_TOOLCP=%~1
24+
shift
25+
26+
:notoolcp
2427

2528
rem We keep in _JAVA_PARAMS all -J-prefixed and -D-prefixed arguments
2629
set _JAVA_PARAMS=

0 commit comments

Comments
 (0)