aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2023-09-12 00:07:55 +0000
committerGopher Robot <gobot@golang.org>2023-09-12 00:57:26 +0000
commit8221f90f1347bd96548d48b178c8bcb0607cde8f (patch)
treee8bf15b64d139b7e93a4d6b56e2c29f75501990b /src/os/exec/exec.go
parent06138963a2a16e0ac982ac85877092920a21e60f (diff)
downloadgo-8221f90f1347bd96548d48b178c8bcb0607cde8f.tar.xz
Revert "os/exec: avoid calling LookPath in cmd.Start for resolved paths"
This reverts CL 512155. Reason for revert: CL 512155 introduced a race in that it caused cmd.Start to set cmd.Path. Previously it was fine if code looked at cmd.Path in one goroutine while calling cmd.Start in a different goroutine. A test case for this race is in CL 527495. Change-Id: Ic18fdadf6763727f8ea748280d5f0e601b9bf374 Reviewed-on: https://go-review.googlesource.com/c/go/+/527337 Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/os/exec/exec.go')
-rw-r--r--src/os/exec/exec.go36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go
index 2881345fb3..dfede0e7e2 100644
--- a/src/os/exec/exec.go
+++ b/src/os/exec/exec.go
@@ -590,6 +590,32 @@ func (c *Cmd) Run() error {
return c.Wait()
}
+// lookExtensions finds windows executable by its dir and path.
+// It uses LookPath to try appropriate extensions.
+// lookExtensions does not search PATH, instead it converts `prog` into `.\prog`.
+func lookExtensions(path, dir string) (string, error) {
+ if filepath.Base(path) == path {
+ path = "." + string(filepath.Separator) + path
+ }
+ if dir == "" {
+ return LookPath(path)
+ }
+ if filepath.VolumeName(path) != "" {
+ return LookPath(path)
+ }
+ if len(path) > 1 && os.IsPathSeparator(path[0]) {
+ return LookPath(path)
+ }
+ dirandpath := filepath.Join(dir, path)
+ // We assume that LookPath will only add file extension.
+ lp, err := LookPath(dirandpath)
+ if err != nil {
+ return "", err
+ }
+ ext := strings.TrimPrefix(lp, dirandpath)
+ return path + ext, nil
+}
+
// Start starts the specified command but does not wait for it to complete.
//
// If Start returns successfully, the c.Process field will be set.
@@ -623,11 +649,13 @@ func (c *Cmd) Start() error {
}
return c.Err
}
- lp, err := lookExtensions(c.Path, c.Dir)
- if err != nil {
- return err
+ if runtime.GOOS == "windows" {
+ lp, err := lookExtensions(c.Path, c.Dir)
+ if err != nil {
+ return err
+ }
+ c.Path = lp
}
- c.Path = lp
if c.Cancel != nil && c.ctx == nil {
return errors.New("exec: command with a non-nil Cancel was not created with CommandContext")
}