Skip to content

os/exec: cmd.Env doesn't use provided PATH #53996

@anuraaga

Description

@anuraaga

What version of Go are you using (go version)?

$ go version
go version go1.18.4 darwin/arm64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="arm64"
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

cmd := exec.Command("protoc", args...)
cmd.Env = []string{fmt.Sprintf("PATH=%s", protocDir)}
cmd.Run()

What did you expect to see?

protoc is resolved from the PATH passed to the command

What did you see instead?

protoc is resolved from the system PATH

The documentation does specifically say it uses LookPath

https://github.com/golang/go/blob/master/src/os/exec/exec.go#L249

But I interpreted that to still be when calling Run, not the constructor and believe that would be more intuitive for most users. Currently using a custom PATH and executable name is just not possible and an absolute path must be passed by the caller.

Changing the behavior would be incompatible though, so there may not really be anything we can do here but wanted to bring up the possibility of having the file resolved when run rather than before

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions