-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: os: add UserHomeDir #26463
Comments
#22536 acts as a bit of a precedent. Also drawing from that proposal, I presume that the function should be called |
@mvdan thanks for the input. I tend to agree that it should be named |
I've re-titled the issue to be a proposal, and to use the same package and naming scheme as the previous proposal for consistency. @kolyshkin feel free to change it if you disagree. Another open question is whether or not the function should return an error. |
Yes, I believe it makes sense to return an error. One other related thing is, it might make sense to add simple /etc/passwd parser to use as replacement for calling |
(Wow, |
This differs from What should it look like on Plan 9? @0intro |
On Plan 9, I think it should look like Unix, except the environment variable is called The Home function would look like:
|
FWIW, I still don’t understand why it is |
#22536 (comment) |
os.UserHomeDir is OK to add, alongside os.UserCacheDir. The "User" in UserCacheDir was there to distinguish the user-local cache directory from any system-wide cache directories. There is less ambiguity for HomeDir but maybe UserHomeDir fits next to UserCacheDir better. |
As discussed in golang/go#26463, Go's 'user.Current().HomeDir' does not do the right thing. The 'HOME' environment variable is meant to take precedent over whatever's in /etc/passwd. This updates the config directory initialization code to use the correct home directory.
As discussed in golang/go#26463, Go's 'user.Current().HomeDir' does not do the right thing. The 'HOME' environment variable is meant to take precedent over whatever's in /etc/passwd. This updates the config directory initialization code to use the correct home directory.
Change https://golang.org/cl/139418 mentions this issue: |
As pointed out in #26463, HOME (or equivalent) environment variable (rather than the value obtained by parsing /etc/passwd or the like) should be used to obtain user's home directory. Since commit fa1a49a there's a method to obtain user's home directory -- use it here. Change-Id: I852fbb24249bcfe08f3874fae6e7b9d01d869190 Reviewed-on: https://go-review.googlesource.com/c/139426 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
Is it intentional that UserHomeDir no longer matches UserCacheDir in how errors are handled? UserHomeDir unconditionally returns Getenv("HOME"), whereas commit 50bd1c4 changed UserCacheDir to return an error instead of an empty string when HOME is unset. Shouldn’t they be consistent? |
@stapelberg Thanks, opened #28562 for this. |
As pointed out in golang/go#26463, HOME (or equivalent) environment variable (rather than the value obtained by parsing /etc/passwd or the like) should be used to obtain user's home directory. Since commit fa1a49aa556d8 there's a method to obtain user's home directory -- use it here. Change-Id: I852fbb24249bcfe08f3874fae6e7b9d01d869190 Reviewed-on: https://go-review.googlesource.com/c/139426 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
This is a proposal to add
Home()
method toos/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 useCurrent()
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: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:Windows version can be like:
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
The text was updated successfully, but these errors were encountered: