From: Bryan C. Mills Date: Tue, 3 May 2022 14:01:35 +0000 (-0400) Subject: os/exec: in Command, update cmd.Path even if LookPath returns an error X-Git-Tag: go1.19beta1~461 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=61a585a32cc44cb1d8d00d12dcf101a61f145d69;p=gostls13.git 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 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills --- diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index dad63f13f9..042d7f465d 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -19,7 +19,7 @@ // They may not run on Windows, and they do not run in the Go Playground // 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 // 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...), } if filepath.Base(name) == name { - if lp, err := LookPath(name); err != nil { - cmd.Err = err - } else { + lp, err := LookPath(name) + if lp != "" { + // 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 } + if err != nil { + cmd.Err = err + } } return cmd }