Skip to content

Commit 61a585a

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
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>
1 parent e750859 commit 61a585a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/os/exec/exec.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// They may not run on Windows, and they do not run in the Go Playground
2020
// used by golang.org and godoc.org.
2121
//
22-
// Executables in the current directory
22+
// # Executables in the current directory
2323
//
2424
// The functions Command and LookPath look for a program
2525
// in the directories listed in the current path, following the
@@ -256,11 +256,16 @@ func Command(name string, arg ...string) *Cmd {
256256
Args: append([]string{name}, arg...),
257257
}
258258
if filepath.Base(name) == name {
259-
if lp, err := LookPath(name); err != nil {
260-
cmd.Err = err
261-
} else {
259+
lp, err := LookPath(name)
260+
if lp != "" {
261+
// Update cmd.Path even if err is non-nil.
262+
// If err is ErrDot (especially on Windows), lp may include a resolved
263+
// extension (like .exe or .bat) that should be preserved.
262264
cmd.Path = lp
263265
}
266+
if err != nil {
267+
cmd.Err = err
268+
}
264269
}
265270
return cmd
266271
}

0 commit comments

Comments
 (0)