Description
This is a proposal to add Home()
method to os/user
package to get the user's home directory.
There is a bad trend to use the data returned by user.Current()
in order to get user's home directory. It's bad to use Current()
to get the value of home dir for two reasons:
$HOME
should be used, if available, as user's home directory. Quoting http://man7.org/linux/man-pages/man3/getpwuid_r.3.html:
The pw_dir field contains the name of the initial working directory
of the user. Login programs use the value of this field to
initialize the HOME environment variable for the login shell. An
application that wants to determine its user's home directory should
inspect the value of HOME (rather than the value
getpwuid(getuid())->pw_dir) since this allows the user to modify
their notion of "the home directory" during a login session.
user.Current()
is not implemented for some platforms (notably, Linux when building statically with glibc).
In a sense, currently os/user
provides a not-quite-right way to get user's home directory, and lots of software is using this way. Why it's out of scope of this project to fix broken third-party software, perhaps providing the right way would do some good.
Unix/Linux/Darwin implementation of Home()
can look as simple as:
func Home() string {
if v := os.Getenv("HOME"); v != "" {
return v
}
// fallback
if u, err := Current(); err == nil {
return u.HomeDir
}
return ""
}
Windows version can be like:
func Home() string {
return os.Getenv("USERPROFILE")
}
Example
Here is an example (of bad usage of user.Current()) from this very repo:
go/src/crypto/x509/root_darwin.go
Lines 70 to 82 in d278f09