From 8e919b8268897e42b405c5057813c45f610b68d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=A9tan=20Lepage?= <33058747+GaetanLepage@users.noreply.github.com> Date: Wed, 19 Oct 2022 17:56:04 +0200 Subject: [PATCH] Fix user query when using libc implementation of os/user (#972) --- os.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/os.go b/os.go index 3dd9b1f9..222c0bf1 100644 --- a/os.go +++ b/os.go @@ -64,12 +64,19 @@ func init() { u, err := user.Current() if err != nil { + // When the user is not in /etc/passwd (for e.g. LDAP) and CGO_ENABLED=1 in go env, + // the cgo implementation of user.Current() fails even when HOME and USER are set. + log.Printf("user: %s", err) if os.Getenv("HOME") == "" { - log.Print("$HOME variable is empty or not set") + panic("$HOME variable is empty or not set") } if os.Getenv("USER") == "" { - log.Print("$USER variable is empty or not set") + panic("$USER variable is empty or not set") + } + u = &user.User{ + Username: os.Getenv("USER"), + HomeDir: os.Getenv("HOME"), } } gUser = u