Skip to content

proposal: os: add UserHomeDir #26463

Closed
Closed
@kolyshkin

Description

@kolyshkin

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:

  1. $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.

  1. 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:

u, err := user.Current()
if err != nil {
if debugExecDarwinRoots {
println(fmt.Sprintf("crypto/x509: get current user: %v", err))
}
} else {
args = append(args,
filepath.Join(u.HomeDir, "/Library/Keychains/login.keychain"),
// Fresh installs of Sierra use a slightly different path for the login keychain
filepath.Join(u.HomeDir, "/Library/Keychains/login.keychain-db"),
)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions