Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/exec: on Windows look for extensions in Run if not already done #67035

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/os/exec/exec.go
Expand Up @@ -332,6 +332,10 @@ type Cmd struct {
// See https://go.dev/blog/path-security
// and https://go.dev/issue/43724 for more context.
lookPathErr error

// calledLookExtensions indicates whether
// lookExtensions has been called in Cmd.Command.
calledLookExtensions bool
}

// A ctxResult reports the result of watching the Context associated with a
Expand Down Expand Up @@ -436,6 +440,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.
cmd.calledLookExtensions = true
lp, err := lookExtensions(name, "")
if lp != "" {
cmd.Path = lp
Expand Down Expand Up @@ -641,7 +646,7 @@ func (c *Cmd) Start() error {
return c.Err
}
lp := c.Path
if runtime.GOOS == "windows" && !filepath.IsAbs(c.Path) {
if runtime.GOOS == "windows" && !c.calledLookExtensions {
// If c.Path is relative, we had to wait until now
// to resolve it in case c.Dir was changed.
// (If it is absolute, we already resolved its extension in Command
Expand Down
14 changes: 14 additions & 0 deletions src/os/exec/exec_windows_test.go
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -107,3 +108,16 @@ func TestChildCriticalEnv(t *testing.T) {
t.Error("no SYSTEMROOT found")
}
}

func TestIssue66586(t *testing.T) {
testenv.MustHaveGoBuild(t)
path := filepath.Join(testenv.GOROOT(t), "bin", "go")
cmd := exec.Cmd{Path: path, Args: []string{path, "version"}}
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
if path != cmd.Path {
t.Fatalf("unexpected path: %s", cmd.Path)
}
}