Skip to content

Commit 466fbb6

Browse files
neolit123albertvaka
authored andcommitted
klog_file: workaround stdlib.user not supporting Nano Server
user.Current() can panic on Windows if the OS is missing netapi32.dll. This library is stripped from Windows Nano Server. To workaround this, use environment variables on all versions on Windows.
1 parent 2ca9ad3 commit 466fbb6

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

klog_file.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"os/user"
2626
"path/filepath"
27+
"runtime"
2728
"strings"
2829
"sync"
2930
"time"
@@ -55,13 +56,31 @@ func init() {
5556
host = shortHostname(h)
5657
}
5758

58-
current, err := user.Current()
59-
if err == nil {
60-
userName = current.Username
61-
}
59+
// On Windows, the Go 'user' package requires netapi32.dll.
60+
// This affects Windows Nano Server:
61+
// https://github.com/golang/go/issues/21867
62+
// Fallback to using environment variables.
63+
if runtime.GOOS == "windows" {
64+
u := os.Getenv("USERNAME")
65+
if len(u) == 0 {
66+
return
67+
}
68+
// Sanitize the USERNAME since it may contain filepath separators.
69+
u = strings.Replace(u, `\`, "_", -1)
6270

63-
// Sanitize userName since it may contain filepath separators on Windows.
64-
userName = strings.Replace(userName, `\`, "_", -1)
71+
// user.Current().Username normally produces something like 'USERDOMAIN\USERNAME'
72+
d := os.Getenv("USERDOMAIN")
73+
if len(d) != 0 {
74+
userName = d + "_" + u
75+
} else {
76+
userName = u
77+
}
78+
} else {
79+
current, err := user.Current()
80+
if err == nil {
81+
userName = current.Username
82+
}
83+
}
6584
}
6685

6786
// shortHostname returns its argument, truncating at the first period.

0 commit comments

Comments
 (0)