Skip to content

Commit bedb6a1

Browse files
committed
os: only fallback to root directory if $HOME fails for UserHomeDir
UserHomeDir always returns "/" for platforms where the home directory is not always well defined. However, the user might set HOME before running a Go program on those platforms and on at least iOS, HOME is actually set to something useful (the root of the app specific writable directory). This CL changes UserHomeDir to use the root directory "/" only if $HOME is empty. Change-Id: Icaa01de53cd585d527d9a23b1629375d6b7f67e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/167802 Run-TryBot: Elias Naur <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Tobias Klauser <[email protected]>
1 parent a734601 commit bedb6a1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/os/file.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,16 +445,19 @@ func UserHomeDir() (string, error) {
445445
env, enverr = "USERPROFILE", "%userprofile%"
446446
case "plan9":
447447
env, enverr = "home", "$home"
448+
}
449+
if v := Getenv(env); v != "" {
450+
return v, nil
451+
}
452+
// On some geese the home directory is not always defined.
453+
switch runtime.GOOS {
448454
case "nacl", "android":
449455
return "/", nil
450456
case "darwin":
451457
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
452458
return "/", nil
453459
}
454460
}
455-
if v := Getenv(env); v != "" {
456-
return v, nil
457-
}
458461
return "", errors.New(enverr + " is not defined")
459462
}
460463

0 commit comments

Comments
 (0)