Skip to content

cmd/go: adjust PATH when go run/go generate runs the built binary  #68005

Closed
@hyangah

Description

@hyangah

Go version

go version go1.22.0 darwin/amd64

Output of go env in your module/workspace:

$ go env GOTOOLCHAIN
auto
$ GOTOOLCHAIN=local go version
go version go1.21.9 darwin/amd64
$ go version
go version go1.22.0 darwin/amd64

What did you do?

-- go.mod --
module example.com/m

go 1.22.0
-- main.go --
package main

import "os"
import "os/exec"

func main() {
        println(os.Getenv("GOROOT"))
        bin, _ := exec.LookPath("go")
        println(bin)
}

What did you see happen?

$ go run .
/Users/hakim/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.darwin-amd64
/usr/local/go/bin/go

Since my local version is go1.21.9, but my go.mod requires go1.22.0+,
toolchain switch occurs when running go run .. During the toolchain switch
the go command sets GOROOT so the switched go command can choose
the toolchains correctly.

When go run executes the compiled binary, this GOROOT is left behind.
But the PATH is unchanged.

This can be potentially problematic, if the binary invokes go install mod@ver, or runs go build outside the current module.

In my specific case, /usr/local/go/bin/go is go1.21.9, but GOROOT is go1.22.0 root.
The go install or go generate program will fail to run due to this mismatched binary/GOROOT pair.

@rsc said 'go test' handles this problem by adjusting PATH, too.

The 'go test' docs explain:

The go command places $GOROOT/bin at the beginning of $PATH
in the test's environment, so that tests that execute
'go' commands use the same 'go' as the parent 'go test' command.

We should do the same for go run, go generate, ...

What did you expect to see?

Subprocess can pick matching go binary and GOROOT.

Activity

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jun 15, 2024
mauri870

mauri870 commented on Jun 18, 2024

@mauri870
Member

From a brief investigation looks like 'go generate' already adjusts the path accordingly since CL 404134. I'll push a fix for go run.

gopherbot

gopherbot commented on Jun 18, 2024

@gopherbot
Contributor

Change https://go.dev/cl/593255 mentions this issue: cmd/go: place GOROOT/bin at the beginning of PATH in 'go run'

self-assigned this
on Jun 18, 2024
matloob

matloob commented on Jun 27, 2024

@matloob
Contributor

This seems reasonable to me. Thanks for the CL!

added a commit that references this issue on Oct 9, 2024
935bf13
added
NeedsFixThe path to resolution is known, but the work has not been done.
and removed
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 9, 2024
added this to the Go1.24 milestone on Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GoCommandcmd/goNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @dmitshur@hyangah@gopherbot@mauri870@seankhliao

    Issue actions

      cmd/go: adjust `PATH` when `go run`/`go generate` runs the built binary · Issue #68005 · golang/go