go-homedir
go-homedir copied to clipboard
Respect Windows directory locations.
https://github.com/mitchellh/go-homedir/blob/af06845cf3004701891bf4fdb884bfe4920b3727/homedir.go#L148-L167
TL;DR, change code to
func dirWindows() (string, error) {
home := os.Getenv("LOCALAPPDATA")
if home == "" {
return "", errors.New("LOCALAPPDATA environment variable is missing or empty")
}
return home, nil
}
$HOME
is non-standard on Windows, and likely references $USERPROFILE
. $USERPROFILE
is not meant to be a location that applications store files other than via a save dialog box or similar situation where the user is explicitly putting files there. The proper location to store data that should be automatically synced to a network or uploaded to the cloud is $APPDATA
. For data that should be machine-local (e.g., caches, application data, etc.) it should be stored in $LOCALAPPDATA
.
In this library, it doesn't appear that you distinguish between data types so $LOCALAPPDATA
is the best location. If someone throws a cache in there you don't break the user's profile.
Note: $LOCALAPPDATA
has been around since Windows XP/2003 while the fallback you were using of $HOMEDRIVE
I think has been deprecated and $HOMEPATH
is fairly new (and I think also deprecated), so this is not only more correct, but also more backward compatible than the code currently present.