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

System.property "scala.home" is not being set when launching scripts #13758

Closed
philwalk opened this issue Oct 17, 2021 · 2 comments · Fixed by #13759
Closed

System.property "scala.home" is not being set when launching scripts #13758

philwalk opened this issue Oct 17, 2021 · 2 comments · Fixed by #13759
Assignees

Comments

@philwalk
Copy link
Contributor

Compiler version

Scala code runner version 3.1.1-RC1-bin-SNAPSHOT-git-3e192a3 -- Copyright 2002-2021, LAMP/EPFL

Minimized code

The following script attempts to print out the runtime scala install directory.

#!/usr/bin/env scala3
def main(args: Arra[String]): Unit =
   println(sys.props("scala.home"))

Output

null

Expectation

should print the path to the runtime scala.

Note that there is no simple alternative to figuring out the path to the current runtime scala.
Different scripts can specify different versions of scala via the hashbang line, so environment variables can't be relied on.

@michelou
Copy link
Collaborator

michelou commented Oct 17, 2021

@philwalk Unsurprisingly one (possibly two) batch file is also concerned by this issue :

> c:\opt\scala3-3.1.0-RC3\bin\scala
Welcome to Scala 3.1.0-RC3 (1.8.0_302, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> println(sys.props("scala.home"))
null

With a small code tweak in scalac.bat we get the correct value for the Scala 3 installation path :

> c:\opt\scala3-3.1.0-RC3\bin\scala
Welcome to Scala 3.1.0-RC3 (1.8.0_302, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> println(sys.props("scala.home"))
c:\opt\scala3-3.1.0-RC3

Later on I can create a PR (once our addition to the bash script is validated), i.e. in scalac.bat :

42: for %%i in ("%_PROG_HOME%\..") do set "_SCALA_HOME=%%~fi"
[...]
45: -Dscala.home="%_SCALA_HOME%" -Dscala.usejavacp=true
[...]

PS. For comparison here is what we currently get with Scala 2 on MS Windows:

> c:\opt\scala-2.13.6\bin\scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_302).
Type in expressions for evaluation. Or try :help.

scala> println(sys.props("scala.home"))
c:\opt\SCALA-~1.6\bin\..

With a small code tweak in tool-windows.tmpl we can improve the value of the Scala 2 installation path (without \.. and short directory name) :

> c:\opt\scala-2.13.6\bin\scala
Welcome to Scala 2.13.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_302).
Type in expressions for evaluation. Or try :help.

scala> println(sys.props("scala.home"))
c:\opt\scala-2.13.6

The modified code for setting _SCALA_HOME looks as follows :
Current :

:set_home
  set _BIN_DIR=
  for %%i in ("%~sf0") do set _BIN_DIR=%_BIN_DIR%%%~dpsi
  set _SCALA_HOME=%_BIN_DIR%..
goto :eof

New (Tip : Always surround variable assignments dealing with paths by double quotes) :

:set_home
  set _SCALA_HOME=
  for %%i in ("%~f0") do set "_BIN_DIR=%%~dpi"
  for %%i in ("%_BIN_DIR%..") do set "_SCALA_HOME=%%~fi"
goto :eof

@philwalk
Copy link
Contributor Author

The PR for the bash script fix is #13759

@BarkingBad BarkingBad linked a pull request Oct 18, 2021 that will close this issue
BarkingBad added a commit that referenced this issue Oct 18, 2021
provide scala.home setting for scripts - fix for #13758
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants