Skip to content

Commit c9d444c

Browse files
MaciejG604lolgablefou
authored
Make os.home a def (#239)
In some environments the home directory is not configured, thus `user.home` can be set to e.g. "?" (like in the issue the Scala CLI team has bumped into). See [OpenJDK sources](https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/libjava/java_props_md.c#L508) In those cases using e.g. `os.pwd` throws, since the `os.home` is also initialized. This fix allows `os.pwd` to still be used when `user.home` is set to something weird. One example of such environment with `user.home` set to "?" is running Jenking on CloudBees, the results of trying to run Scala CLI there looked like this, since we heavily use os.pwd in our code base: Pull request: #239 --------- Co-authored-by: Lorenzo Gabriele <[email protected]> Co-authored-by: Tobias Roeser <[email protected]>
1 parent 650dee6 commit c9d444c

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

build.sc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ object os extends Module {
126126
object jvm extends Cross[OsJvmModule](scalaVersions)
127127
trait OsJvmModule extends OsModule with MiMaChecks {
128128
object test extends ScalaTests with OsLibTestModule
129+
object nohometest extends ScalaTests with OsLibTestModule
129130
}
130131

131132
object native extends Cross[OsNativeModule](scalaVersions)
@@ -134,6 +135,7 @@ object os extends Module {
134135
object test extends ScalaNativeTests with OsLibTestModule {
135136
def nativeLinkStubs = true
136137
}
138+
object nohometest extends ScalaNativeTests with OsLibTestModule
137139
}
138140

139141
object watch extends Module {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test.os
2+
3+
import utest._
4+
5+
object NoHomeTests extends TestSuite {
6+
private lazy val isWindows = sys.props("os.name").toLowerCase().contains("windows")
7+
8+
val tests = Tests {
9+
test("pwd when home is not available") {
10+
System.setProperty("user.home", "?")
11+
val homeException = intercept[IllegalArgumentException] { os.home }
12+
.getMessage()
13+
14+
val expectedException =
15+
if (isWindows)
16+
"Illegal char <?> at index 0: ?"
17+
else
18+
"requirement failed: ? is not an absolute path"
19+
20+
assert(homeException == expectedException)
21+
os.pwd
22+
()
23+
}
24+
}
25+
}

os/src-jvm/package.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ package object os {
2323
os.ResourcePath.resource(resRoot)
2424
}
2525

26+
// See https://github.com/com-lihaoyi/os-lib/pull/239
27+
// and https://github.com/lightbend/mima/issues/794
28+
// why the need the inner object to preserve binary compatibility
29+
private object _home {
30+
lazy val value = Path(System.getProperty("user.home"))
31+
}
32+
2633
/**
2734
* The user's home directory
2835
*/
29-
val home: Path = Path(System.getProperty("user.home"))
36+
def home: Path = _home.value
3037

3138
/**
3239
* The current working directory for this process.

os/src-native/package.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ package object os {
1616
path
1717
}
1818

19+
// See https://github.com/com-lihaoyi/os-lib/pull/239
20+
// and https://github.com/lightbend/mima/issues/794
21+
// why the need the inner object to preserve binary compatibility
22+
private object _home {
23+
lazy val value = Path(System.getProperty("user.home"))
24+
}
25+
1926
/**
2027
* The user's home directory
2128
*/
22-
val home: Path = Path(System.getProperty("user.home"))
29+
def home: Path = _home.value
2330

2431
/**
2532
* The current working directory for this process.

0 commit comments

Comments
 (0)