Skip to content

Commit 88c3c32

Browse files
authored
Add env for configuring home directory overriding (#2587)
* Add env for configuring home directory overriding * Update test conditions to check cache path for each system
1 parent d28977a commit 88c3c32

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,38 @@ import scala.cli.util.ConfigDbUtils
1919
import scala.util.Properties
2020

2121
object ScalaCli {
22-
2322
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
24-
if (scala.util.Try(os.Path(System.getProperty("user.home"))).isFailure) {
25-
System.err.println("Warning: user.home property is not set, setting it to user.dir")
26-
System.setProperty("user.home", System.getProperty("user.dir"))
23+
sys.env.get("SCALA_CLI_HOME_DIR_OVERRIDE")
24+
.filter(_.nonEmpty)
25+
.filter(homeDir => scala.util.Try(os.Path(homeDir)).isSuccess)
26+
.foreach { homeDirOverride =>
27+
System.err.println(
28+
s"Warning: user.home property overridden with the SCALA_CLI_HOME_DIR_OVERRIDE env var to: $homeDirOverride"
29+
)
30+
System.setProperty("user.home", homeDirOverride)
31+
}
32+
private def getInvalidPropMessage(homeDirOpt: Option[String]): String = homeDirOpt match {
33+
case Some(value) => s"not valid: $value"
34+
case None => "not set"
35+
}
36+
sys.props.get("user.home") -> sys.props.get("user.dir") match {
37+
case (Some(homeDir), _)
38+
if scala.util.Try(os.Path(homeDir)).isSuccess => // all is good, do nothing
39+
case (invalidHomeDirOpt, Some(userDir)) if scala.util.Try(os.Path(userDir)).isSuccess =>
40+
System.err.println(
41+
s"Warning: user.home property is ${getInvalidPropMessage(invalidHomeDirOpt)}, setting it to user.dir value: $userDir"
42+
)
43+
System.setProperty("user.home", userDir)
44+
case (invalidHomeDirOpt, invalidUserDirOpt) =>
45+
System.err.println(
46+
s"Error: user.home property is ${getInvalidPropMessage(invalidHomeDirOpt)}"
47+
)
48+
System.err.println(s"Error: user.dir property is ${getInvalidPropMessage(invalidUserDirOpt)}")
49+
System.err.println("Scala CLI cannot work correctly without a valid home or user directory.")
50+
System.err.println(
51+
"Consider overriding it to a valid directory path with the SCALA_CLI_HOME_DIR_OVERRIDE environment variable."
52+
)
53+
sys.exit(1)
2754
}
2855

2956
if (Properties.isWin && isGraalvmNativeImage)

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,4 +2087,62 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
20872087
expect(res.err.trim().contains("JVM (11)"))
20882088
}
20892089
}
2090+
2091+
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
2092+
def getCoursierCacheRelPath: os.RelPath =
2093+
if (Properties.isWin) os.rel / "Coursier" / "Cache"
2094+
else if (Properties.isMac) os.rel / "Library" / "Caches" / "Coursier"
2095+
else os.rel / ".cache" / "coursier"
2096+
if (TestUtil.isJvmCli) // can't reproduce on native image launchers
2097+
test("user.home is overridden with user.dir") {
2098+
val customCall =
2099+
Seq("java", "-Xmx512m", "-Xms128m", "-Duser.home=?", "-jar", TestUtil.cliPath)
2100+
val msg = "Hello"
2101+
val input = "script.sc"
2102+
TestInputs(os.rel / input -> s"println(\"$msg\")")
2103+
.fromRoot { root =>
2104+
expect(!os.isDir(root / getCoursierCacheRelPath))
2105+
val res = os.proc(customCall, "run", extraOptions, "--server=false", input)
2106+
.call(
2107+
cwd = root,
2108+
stderr = os.Pipe
2109+
)
2110+
expect(res.out.trim() == msg)
2111+
expect(
2112+
res.err.trim().contains(
2113+
"user.home property is not valid: ?, setting it to user.dir value"
2114+
)
2115+
)
2116+
if (!Properties.isWin) // coursier cache location on Windows does not depend on home dir
2117+
expect(os.isDir(root / getCoursierCacheRelPath))
2118+
}
2119+
}
2120+
2121+
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
2122+
test("user.home is overridden by SCALA_CLI_HOME_DIR_OVERRIDE") {
2123+
val msg = "Hello"
2124+
val input = "script.sc"
2125+
TestInputs(os.rel / input -> s"println(\"$msg\")")
2126+
.fromRoot { root =>
2127+
val newHomePath = root / "home"
2128+
os.makeDir(newHomePath)
2129+
expect(!os.isDir(newHomePath / getCoursierCacheRelPath))
2130+
val extraEnv = Map("SCALA_CLI_HOME_DIR_OVERRIDE" -> newHomePath.toString())
2131+
2132+
val res = os.proc(TestUtil.cli, "run", extraOptions, "--server=false", input)
2133+
.call(
2134+
cwd = root,
2135+
stderr = os.Pipe,
2136+
env = extraEnv
2137+
)
2138+
expect(res.out.trim() == msg)
2139+
expect(
2140+
res.err.trim().contains(
2141+
"user.home property overridden with the SCALA_CLI_HOME_DIR_OVERRIDE"
2142+
)
2143+
)
2144+
if (!Properties.isWin) // coursier cache location on Windows does not depend on home dir
2145+
expect(os.isDir(newHomePath / getCoursierCacheRelPath))
2146+
}
2147+
}
20902148
}

0 commit comments

Comments
 (0)