Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: cannot pass java opts that contain whitespaces #4858

Closed
scabug opened this issue Jul 31, 2011 · 2 comments
Closed

Windows: cannot pass java opts that contain whitespaces #4858

scabug opened this issue Jul 31, 2011 · 2 comments
Labels

Comments

@scabug
Copy link

scabug commented Jul 31, 2011

Disclaimer: that's NOT yet another "scala won't work if SCALA_HOME resides in C:\Program Files (x86)" issue. Please, take a look at the details provided below.

The bug

Here's the excerpt from scala.bat that contains the bug (though, the bug affects not only scala.bat, but all batch files that work with JAVA_OPTS):

rem We use the value of the JAVA_OPTS environment variable if defined
set _JAVA_OPTS=%JAVA_OPTS%
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=-Xmx256M -Xms32M

Since the expansion of _JAVA_OPTS is wrapped in quotes, it works finely when the variable contains spaces (i.e. if the user has provided more than one javaopt). However, it fails when the variable itself contains quotes. As explained in http://stackoverflow.com/questions/430376/batch-files-vista-x64-if-and-parenthesis, in batch files, variables in "if" statements are expanded before the "if" statement is parsed. That's why, after the variable expansion, the "if" line no longer conforms to the "if == " pattern (since we get multiple strings instead of the first ), and command interpreter reports an error.

This issue could be dismissed as insignificant, but it blocks an important use-case, namely passing a string with spaces as a javaopt value. The exceprt provided below explains that:

"%_JAVACMD%" %_JAVA_OPTS% %_PROPS% -cp "%_TOOL_CLASSPATH%" scala.tools.nsc.MainGenericRunner %*

That's the raison d'etre of the script. Note, that the variable expansion of _JAVA_OPTS here is not guarded by quotes. This means that the user herself has to protect whitespaces in her javaopts. The only way to do that is to use quotes, e.g. like that:

SET JAVA_OPTS="-Dfoo=qu ux" "-Dbar=bar"
rem or like that: SET JAVA_OPTS=-Dfoo="qu ux"
scala.bat HelloWorld.scala

But in that case, scala.bat will fail when the batch interpreter reaches the "if" line due to the reasons outlined above.

A fix that won't work

This bug cannot be fixed by simply quoting %JAVA_OPTS% or %_JAVA_OPTS% expansions in the script. If we quote the expansion in "set _JAVA_OPTS=%JAVA_OPTS%", then _JAVA_OPTS itself will contain quotes (since set does not remove quotes automatically). If we quote the expansion in "%_JAVACMD% %_JAVA_OPTS% ...", then we got two problems: 1) if _JAVA_OPTS does not contain quotes, it will be passed as a single command-line parameter, even if it contains multiple javaopts, 2) if _JAVA_OPTS contains quotes, the interpreter will get confused and will incorrectly chop it into command-line arguments to JAVACMD.

A fix that will work

Luckily, there's a very simple fix to the problem. We only have to replace the check at the "if" line with "if not defined _JAVA_OPTS". That check does not perform variable expansion at all, that's why we completely evade quoting issues.

Before:

if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=-Xmx256M -Xms32M

After:

if not defined _JAVA_OPTS set _JAVA_OPTS=-Xmx256M -Xms32M

P.S.

Scala batch scripts also contain other checks that might suffer from the same idiosyncrasy of Windows batch interpreter. For example, if "%_JAVACMD%" == "" or if not "%JAVA_HOME%"=="". However, the analysis of whether this will lead to issues has yet to be done.

@scabug
Copy link
Author

scabug commented Jul 31, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4858?orig=1
Reporter: Eugene Burmako (xeno.by)

@scabug
Copy link
Author

scabug commented Nov 13, 2011

Commit Message Bot (anonymous) said:
(extempore in r25997) scala.bat expansion with quotes and spaces.

Closes #4858.

@scabug scabug closed this as completed Nov 13, 2011
@scabug scabug added the repl label Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant