]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: in Command, update cmd.Path even if LookPath returns an error
authorBryan C. Mills <bcmills@google.com>
Tue, 3 May 2022 14:01:35 +0000 (10:01 -0400)
committerGopher Robot <gobot@golang.org>
Tue, 3 May 2022 16:29:56 +0000 (16:29 +0000)
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>

src/os/exec/exec.go

index dad63f13f9c6bff11f50b30f4745ec53a16a603b..042d7f465d42c79c95b21cbe01cf8d79894e8815 100644 (file)
@@ -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
 }