From f7f266c88598398dcf32b448bcea2100e1702630 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 13 Sep 2023 12:02:52 -0400 Subject: os/exec: avoid calling LookPath in cmd.Start for resolved paths This reapplies CL 512155, which was previously reverted in CL 527337. The race that prompted the revert should be fixed by CL 527820, which will be submitted before this one. For #36768. Updates #62596. Change-Id: I3c3cd92470254072901b6ef91c0ac52c8071e0a2 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-race,gotip-windows-amd64-race,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/528038 LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Auto-Submit: Bryan Mills --- src/os/exec/exec.go | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) (limited to 'src/os/exec/exec.go') diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index ea520f872a..c88ee7f52c 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -429,9 +429,6 @@ func Command(name string, arg ...string) *Cmd { } else if runtime.GOOS == "windows" && filepath.IsAbs(name) { // We may need to add a filename extension from PATHEXT // or verify an extension that is already present. - // (We need to do this even for names that already have an extension - // in case of weird names like "foo.bat.exe".) - // // Since the path is absolute, its extension should be unambiguous // and independent of cmd.Dir, and we can go ahead and update cmd.Path to // reflect it. @@ -439,7 +436,7 @@ func Command(name string, arg ...string) *Cmd { // Note that we cannot add an extension here for relative paths, because // cmd.Dir may be set after we return from this function and that may cause // the command to resolve to a different extension. - lp, err := LookPath(name) + lp, err := lookExtensions(name, "") if lp != "" { cmd.Path = lp } @@ -610,32 +607,6 @@ 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. -- cgit v1.3