diff --git a/src/cmd/go/internal/cache/default.go b/src/cmd/go/internal/cache/default.go index 02fc1e896f..4a69bf2a44 100644 --- a/src/cmd/go/internal/cache/default.go +++ b/src/cmd/go/internal/cache/default.go @@ -9,7 +9,6 @@ import ( "io/ioutil" "os" "path/filepath" - "runtime" "sync" ) @@ -78,52 +77,19 @@ func defaultDir() (string, bool) { } // Compute default location. - // TODO(rsc): This code belongs somewhere else, - // like maybe ioutil.CacheDir or os.CacheDir. - showWarnings := true - switch runtime.GOOS { - case "windows": - dir = os.Getenv("LocalAppData") - if dir == "" { - // Fall back to %AppData%, the old name of - // %LocalAppData% on Windows XP. - dir = os.Getenv("AppData") - } - if dir == "" { - return "off", true - } - - case "darwin": - dir = os.Getenv("HOME") - if dir == "" { - return "off", true - } - dir += "/Library/Caches" - - case "plan9": - dir = os.Getenv("home") - if dir == "" { - return "off", true - } - // Plan 9 has no established per-user cache directory, - // but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix. - dir += "/lib/cache" - - default: // Unix - // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html - dir = os.Getenv("XDG_CACHE_HOME") - if dir == "" { - dir = os.Getenv("HOME") - if dir == "" { - return "off", true - } - if dir == "/" { - // probably docker run with -u flag - // https://golang.org/issue/26280 - showWarnings = false - } - dir += "/.cache" - } + dir, err := os.UserCacheDir() + if err != nil { + return "off", true } - return filepath.Join(dir, "go-build"), showWarnings + dir = filepath.Join(dir, "go-build") + + // Do this after filepath.Join, so that the path has been cleaned. + showWarnings := true + switch dir { + case "/.cache/go-build": + // probably docker run with -u flag + // https://golang.org/issue/26280 + showWarnings = false + } + return dir, showWarnings }