Skip to content

Commit dc8edf3

Browse files
authored
remove user.home hack (#2710)
* remove `user.home` hack * delete unused method and tests + wrap `user.home` in try * run fix
1 parent 40d42a4 commit dc8edf3

File tree

3 files changed

+6
-75
lines changed

3 files changed

+6
-75
lines changed

modules/build/src/main/scala/scala/build/input/Inputs.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import scala.build.internal.zip.WrappedZipInputStream
1414
import scala.build.options.Scope
1515
import scala.build.preprocessing.ScopePath
1616
import scala.build.preprocessing.SheBang.isShebangScript
17-
import scala.util.Properties
1817
import scala.util.matching.Regex
18+
import scala.util.{Properties, Try}
1919

2020
final case class Inputs(
2121
elements: Seq[Element],
@@ -88,9 +88,10 @@ final case class Inputs(
8888
def reallyOwnedByUser(p: os.Path): Boolean =
8989
if (Properties.isWin)
9090
p.toIO.canWrite // Wondering if there's a better way to do that…
91-
else
92-
os.owner(p) == os.owner(os.home) &&
93-
p.toIO.canWrite
91+
else {
92+
val maybeUserHome = Try(os.owner(os.home)).toOption
93+
maybeUserHome.exists(_ == os.owner(p)) && p.toIO.canWrite
94+
}
9495
val canWrite = existingParent(workspace).exists(reallyOwnedByUser)
9596
if canWrite then this else inHomeDir(directories)
9697
}

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,6 @@ import scala.cli.util.ConfigDbUtils
1919
import scala.util.Properties
2020

2121
object ScalaCli {
22-
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
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)
54-
}
5522

5623
if (Properties.isWin && isGraalvmNativeImage)
5724
// have to be initialized before running (new Argv0).get because Argv0SubstWindows uses csjniutils library

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,65 +2107,28 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
21072107
}
21082108
}
21092109

2110-
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
21112110
def getCoursierCacheRelPath: os.RelPath =
21122111
if (Properties.isWin) os.rel / "Coursier" / "Cache"
21132112
else if (Properties.isMac) os.rel / "Library" / "Caches" / "Coursier"
21142113
else os.rel / ".cache" / "coursier"
21152114

21162115
if (TestUtil.isJvmCli) // can't reproduce on native image launchers
2117-
test("user.home is overridden with user.dir") {
2116+
test("doesn't fail on invalid user.home") {
21182117
val customCall =
21192118
Seq("java", "-Xmx512m", "-Xms128m", "-Duser.home=?", "-jar", TestUtil.cliPath)
21202119
val msg = "Hello"
21212120
val input = "script.sc"
21222121
TestInputs(os.rel / input -> s"println(\"$msg\")")
21232122
.fromRoot { root =>
2124-
expect(!os.isDir(root / getCoursierCacheRelPath))
21252123
val res = os.proc(customCall, "run", extraOptions, "--server=false", input)
21262124
.call(
21272125
cwd = root,
21282126
stderr = os.Pipe
21292127
)
21302128
expect(res.out.trim() == msg)
2131-
expect(
2132-
res.err.trim().contains(
2133-
"user.home property is not valid: ?, setting it to user.dir value"
2134-
)
2135-
)
2136-
if (!Properties.isWin) // coursier cache location on Windows does not depend on home dir
2137-
expect(os.isDir(root / getCoursierCacheRelPath))
21382129
}
21392130
}
21402131

2141-
// TODO: Remove this part once fix is released in os-lib (Issue #2585)
2142-
test("user.home is overridden by SCALA_CLI_HOME_DIR_OVERRIDE") {
2143-
val msg = "Hello"
2144-
val input = "script.sc"
2145-
TestInputs(os.rel / input -> s"println(\"$msg\")")
2146-
.fromRoot { root =>
2147-
val newHomePath = root / "home"
2148-
os.makeDir(newHomePath)
2149-
expect(!os.isDir(newHomePath / getCoursierCacheRelPath))
2150-
val extraEnv = Map("SCALA_CLI_HOME_DIR_OVERRIDE" -> newHomePath.toString())
2151-
2152-
val res = os.proc(TestUtil.cli, "run", extraOptions, "--server=false", input)
2153-
.call(
2154-
cwd = root,
2155-
stderr = os.Pipe,
2156-
env = extraEnv
2157-
)
2158-
expect(res.out.trim() == msg)
2159-
expect(
2160-
res.err.trim().contains(
2161-
"user.home property overridden with the SCALA_CLI_HOME_DIR_OVERRIDE"
2162-
)
2163-
)
2164-
if (!Properties.isWin) // coursier cache location on Windows does not depend on home dir
2165-
expect(os.isDir(newHomePath / getCoursierCacheRelPath))
2166-
}
2167-
}
2168-
21692132
test("toolkit default") {
21702133
val inputs = TestInputs(
21712134
os.rel / "Main.scala" ->

0 commit comments

Comments
 (0)