Skip to content

Commit

Permalink
cmd/go/internal/toolchain: use sync.OnceValue
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent ba2a16c commit 4777fd3
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/cmd/go/internal/toolchain/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,24 @@ import (
"cmd/go/internal/gover"
)

// pathExts is a cached PATHEXT list.
var pathExts struct {
once sync.Once
list []string
}
var pathExts = sync.OnceValue(func() []string {
x := os.Getenv(`PATHEXT`)
if x == "" {
return []string{".com", ".exe", ".bat", ".cmd"}
}

func initPathExts() {
var exts []string
x := os.Getenv(`PATHEXT`)
if x != "" {
for _, e := range strings.Split(strings.ToLower(x), `;`) {
if e == "" {
continue
}
if e[0] != '.' {
e = "." + e
}
exts = append(exts, e)
for _, e := range strings.Split(strings.ToLower(x), `;`) {
if e == "" {
continue
}
if e[0] != '.' {
e = "." + e
}
} else {
exts = []string{".com", ".exe", ".bat", ".cmd"}
exts = append(exts, e)
}
pathExts.list = exts
}
return exts
})

// pathDirs returns the directories in the system search path.
func pathDirs() []string {
Expand All @@ -48,8 +42,7 @@ func pathDirs() []string {
// described by de and info in directory dir.
// 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) {
pathExts.once.Do(initPathExts)
name, _, ok := cutExt(de.Name(), pathExts.list)
name, _, ok := cutExt(de.Name(), pathExts())
if !ok {
return "", false
}
Expand Down

0 comments on commit 4777fd3

Please sign in to comment.