check the default user cachedir if GOCACHE is not explicitly set

This commit is contained in:
Gani Georgiev
2025-09-06 21:19:16 +03:00
parent 40f2ba731c
commit eda90d4555
4 changed files with 78 additions and 51 deletions

33
tools/osutils/run.go Normal file
View File

@@ -0,0 +1,33 @@
package osutils
import (
"os"
"strings"
)
var runDirs = []string{os.TempDir(), cacheDir()}
// IsProbablyGoRun loosely checks if the current executable is running
// as a result of "go run".
func IsProbablyGoRun() bool {
for _, dir := range runDirs {
if dir != "" && strings.HasPrefix(os.Args[0], dir) {
return true
}
}
return false
}
func cacheDir() string {
dir := os.Getenv("GOCACHE")
if dir == "off" {
return ""
}
if dir == "" {
dir, _ = os.UserCacheDir()
}
return dir
}