Skip to content

Commit b8d5150

Browse files
committed
os/user: make Current return better error w/o cgo & complete environment
Fixes #31949 Change-Id: Ib96a43e4c56a00c5ba04e4d213255a063058ae08 Reviewed-on: https://go-review.googlesource.com/c/go/+/176337 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a4f5c9c commit b8d5150

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/os/user/lookup_stubs.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ func current() (*User, error) {
5757
if u.Uid != "" && u.Username != "" && u.HomeDir != "" {
5858
return u, nil
5959
}
60-
return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
60+
var missing string
61+
if u.Username == "" {
62+
missing = "$USER"
63+
}
64+
if u.HomeDir == "" {
65+
if missing != "" {
66+
missing += ", "
67+
}
68+
missing += "$HOME"
69+
}
70+
return u, fmt.Errorf("user: Current requires cgo or %s set in environment", missing)
6171
}
6272

6373
func listGroups(*User) ([]string, error) {

0 commit comments

Comments
 (0)