Skip to content

os.exec and file path resolving #73910

Closed as not planned
Closed as not planned
@mwriter

Description

@mwriter

Go version

1.24.3

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/mwriter/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/mwriter/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build589407262=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/mwriter/go/test/go.mod'
GOMODCACHE='/home/mwriter/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/mwriter/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/mwriter/bin/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/mwriter/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/mwriter/bin/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I don't know how such a bug could have gone unnoticed for years, but it still exists.

It's working

import (
	"os"
	"os/exec"
)

func main() {
	p := "some_path"
	os.Setenv("PATH", p);
	cmd := exec.Command("some_binary_name")
	err := cmd.Start() // <nil>
}

But

import (
	"os"
	"os/exec"
)

func main() {
	p := "some_path"
	cmd := exec.Command("some_binary_name")
	os.Setenv("PATH", p);
	err := cmd.Start() // exec: "some_binary_name": executable file not found in $PATH
}
import (
	"fmt"
	"os/exec"
)

func main() {
	p := "some_path"
	cmd := exec.Command("some_binary_name")
	cmd.Env = append([]string{}, fmt.Sprintf("PATH=%v", p))
	err := cmd.Start() // exec: "some_binary_name": executable file not found in $PATH
}

What did you see happen?

I see a big problem in resolving file paths.

What did you expect to see?

Need to change the resolving of file paths not at the time of creation of exec command object, but at the time of its launch. And rewrite it so that the field cmd.Env is also used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions