1
0
mirror of https://github.com/golang/go synced 2024-11-17 16:24:42 -07:00

os/exec: in Command, update cmd.Path even if LookPath returns an error

Fixes #52666.
Updates #43724.
Updates #43947.

Change-Id: I72cb585036b7e93cd7adbff318b400586ea97bd5
Reviewed-on: https://go-review.googlesource.com/c/go/+/403694
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
This commit is contained in:
Bryan C. Mills 2022-05-03 10:01:35 -04:00 committed by Gopher Robot
parent e7508598bb
commit 61a585a32c

View File

@ -19,7 +19,7 @@
// They may not run on Windows, and they do not run in the Go Playground // They may not run on Windows, and they do not run in the Go Playground
// used by golang.org and godoc.org. // used by golang.org and godoc.org.
// //
// Executables in the current directory // # Executables in the current directory
// //
// The functions Command and LookPath look for a program // The functions Command and LookPath look for a program
// in the directories listed in the current path, following the // in the directories listed in the current path, following the
@ -256,11 +256,16 @@ func Command(name string, arg ...string) *Cmd {
Args: append([]string{name}, arg...), Args: append([]string{name}, arg...),
} }
if filepath.Base(name) == name { if filepath.Base(name) == name {
if lp, err := LookPath(name); err != nil { lp, err := LookPath(name)
cmd.Err = err if lp != "" {
} else { // Update cmd.Path even if err is non-nil.
// If err is ErrDot (especially on Windows), lp may include a resolved
// extension (like .exe or .bat) that should be preserved.
cmd.Path = lp cmd.Path = lp
} }
if err != nil {
cmd.Err = err
}
} }
return cmd return cmd
} }