1
0
mirror of https://github.com/golang/go synced 2024-09-23 09:23:20 -06:00

cmd/go/internal/toolchain: use sync.OnceValue

Rename initPathExts to pathExts, make it return the slice of extensions,
and wrap into sync.OnceValue.

While at it, return early if PATHEXT is empty.

Change-Id: I33508762e87edd226e0a52df4063473c496c0210
Reviewed-on: https://go-review.googlesource.com/c/go/+/611017
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Kir Kolyshkin 2024-09-04 17:11:40 -07:00 committed by Gopher Robot
parent ba2a16cb63
commit 4777fd3d31

View File

@ -14,30 +14,24 @@ import (
"cmd/go/internal/gover" "cmd/go/internal/gover"
) )
// pathExts is a cached PATHEXT list. var pathExts = sync.OnceValue(func() []string {
var pathExts struct {
once sync.Once
list []string
}
func initPathExts() {
var exts []string
x := os.Getenv(`PATHEXT`) x := os.Getenv(`PATHEXT`)
if x != "" { if x == "" {
for _, e := range strings.Split(strings.ToLower(x), `;`) { return []string{".com", ".exe", ".bat", ".cmd"}
if e == "" {
continue
}
if e[0] != '.' {
e = "." + e
}
exts = append(exts, e)
}
} else {
exts = []string{".com", ".exe", ".bat", ".cmd"}
} }
pathExts.list = exts
} var exts []string
for _, e := range strings.Split(strings.ToLower(x), `;`) {
if e == "" {
continue
}
if e[0] != '.' {
e = "." + e
}
exts = append(exts, e)
}
return exts
})
// pathDirs returns the directories in the system search path. // pathDirs returns the directories in the system search path.
func pathDirs() []string { func pathDirs() []string {
@ -48,8 +42,7 @@ func pathDirs() []string {
// described by de and info in directory dir. // described by de and info in directory dir.
// The analysis only uses the name itself; it does not run the program. // The analysis only uses the name itself; it does not run the program.
func pathVersion(dir string, de fs.DirEntry, info fs.FileInfo) (string, bool) { func pathVersion(dir string, de fs.DirEntry, info fs.FileInfo) (string, bool) {
pathExts.once.Do(initPathExts) name, _, ok := cutExt(de.Name(), pathExts())
name, _, ok := cutExt(de.Name(), pathExts.list)
if !ok { if !ok {
return "", false return "", false
} }