From ad2b4838e2964dfe7fea1f3c223a871dca1b53d1 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 22 Jul 2023 10:27:57 +0800 Subject: [PATCH] os/exec: fix GOOS=windows,Cmd.Run always calls LookPath before returning Follow up on CL 511458, see https://go-review.googlesource.com/c/go/+/511458/2..4/src/cmd/go/main.go#b270 . Fixes #36768 Change-Id: Icc2a4dbb1219b1d69dd10a900478957b0e975847 --- src/os/exec/exec.go | 9 +++++++++ src/os/exec/lp_plan9.go | 4 ++++ src/os/exec/lp_unix.go | 4 ++++ src/os/exec/lp_wasm.go | 4 ++++ src/os/exec/lp_windows.go | 9 +++++++++ 5 files changed, 30 insertions(+) diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 138be29ecfba6d..d6b5a749359fbd 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -597,6 +597,15 @@ func lookExtensions(path, dir string) (string, error) { if filepath.Base(path) == path { path = "." + string(filepath.Separator) + path } + exts := pathExt() + if ext := filepath.Ext(path); ext != "" { + for _, e := range exts { + if strings.EqualFold(ext, e) { + // Assume that path has already been resolved. + return path, nil + } + } + } if dir == "" { return LookPath(path) } diff --git a/src/os/exec/lp_plan9.go b/src/os/exec/lp_plan9.go index 9344b14e8cdfe6..c4943cfc4cc017 100644 --- a/src/os/exec/lp_plan9.go +++ b/src/os/exec/lp_plan9.go @@ -64,3 +64,7 @@ func LookPath(file string) (string, error) { } return "", &Error{file, ErrNotFound} } + +func pathExt() []string { + return nil +} diff --git a/src/os/exec/lp_unix.go b/src/os/exec/lp_unix.go index fd2c6efbef08cd..af57f97c3b854a 100644 --- a/src/os/exec/lp_unix.go +++ b/src/os/exec/lp_unix.go @@ -80,3 +80,7 @@ func LookPath(file string) (string, error) { } return "", &Error{file, ErrNotFound} } + +func pathExt() []string { + return nil +} diff --git a/src/os/exec/lp_wasm.go b/src/os/exec/lp_wasm.go index f2c8e9c5de4790..a031196018b43c 100644 --- a/src/os/exec/lp_wasm.go +++ b/src/os/exec/lp_wasm.go @@ -21,3 +21,7 @@ func LookPath(file string) (string, error) { // Wasm can not execute processes, so act as if there are no executables at all. return "", &Error{file, ErrNotFound} } + +func pathExt() []string { + return nil +} diff --git a/src/os/exec/lp_windows.go b/src/os/exec/lp_windows.go index 066d38dfdb981b..373a8f80153925 100644 --- a/src/os/exec/lp_windows.go +++ b/src/os/exec/lp_windows.go @@ -63,6 +63,11 @@ func findExecutable(file string, exts []string) (string, error) { // As of Go 1.19, LookPath will instead return that path along with an error satisfying // errors.Is(err, ErrDot). See the package documentation for more details. func LookPath(file string) (string, error) { + return lookPath(file, pathExt()) + +} + +func pathExt() []string { var exts []string x := os.Getenv(`PATHEXT`) if x != "" { @@ -78,7 +83,11 @@ func LookPath(file string) (string, error) { } else { exts = []string{".com", ".exe", ".bat", ".cmd"} } + return exts +} +// lookPath implements LookPath for the given PATHEXT list. +func lookPath(file string, exts []string) (string, error) { if strings.ContainsAny(file, `:\/`) { f, err := findExecutable(file, exts) if err == nil {