[go] cmd/go/internal/envcmd: print a default value for GOBIN

2 views
Skip to first unread message

Sean Liao (Gerrit)

unread,
May 16, 2026, 3:52:49 PM (3 days ago) May 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Sean Liao has uploaded the change for review

Commit message

cmd/go/internal/envcmd: print a default value for GOBIN

A continued source of confusion is: where does "go install" put the
binaries. The actual answer is somewhat complicated, see go.dev/cl/232017
But for the purposes of informing the user, we can have "go env" report
GOPATH[0]/bin (or their current GOPATH/bin) as the probable location
(ignoring cross compiles).

Fixes #23439
Fixes #45546
Updates #42823
Updates #44930
Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964

Change diff

diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go
index a4edd85..78cbbb9 100644
--- a/src/cmd/go/internal/cfg/cfg.go
+++ b/src/cmd/go/internal/cfg/cfg.go
@@ -460,7 +460,7 @@
GOROOTpkg string
GOROOTsrc string

- GOBIN = Getenv("GOBIN")
+ GOBIN, GOBINChanged = EnvOrAndChanged("GOBIN", "")
GOCACHEPROG, GOCACHEPROGChanged = EnvOrAndChanged("GOCACHEPROG", "")
GOMODCACHE, GOMODCACHEChanged = EnvOrAndChanged("GOMODCACHE", gopathDir("pkg/mod"))

diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go
index e950c65..1c54fc6 100644
--- a/src/cmd/go/internal/envcmd/env.go
+++ b/src/cmd/go/internal/envcmd/env.go
@@ -83,7 +83,6 @@
{Name: "GO111MODULE", Value: cfg.Getenv("GO111MODULE")},
{Name: "GOARCH", Value: cfg.Goarch, Changed: cfg.Goarch != runtime.GOARCH},
{Name: "GOAUTH", Value: cfg.GOAUTH, Changed: cfg.GOAUTHChanged},
- {Name: "GOBIN", Value: cfg.GOBIN},
{Name: "GOCACHE"},
{Name: "GOCACHEPROG", Value: cfg.GOCACHEPROG, Changed: cfg.GOCACHEPROGChanged},
{Name: "GODEBUG", Value: os.Getenv("GODEBUG")},
@@ -126,7 +125,7 @@
if env[i].Value != "on" && env[i].Value != "" {
env[i].Changed = true
}
- case "GOBIN", "GOEXPERIMENT", "GOFLAGS", "GOINSECURE", "GOPRIVATE", "GOTMPDIR", "GOVCS":
+ case "GOEXPERIMENT", "GOFLAGS", "GOINSECURE", "GOPRIVATE", "GOTMPDIR", "GOVCS":
if env[i].Value != "" {
env[i].Changed = true
}
@@ -203,9 +202,30 @@
if cfg.Getenv("GOWORK") == "off" {
gowork = "off"
}
+ gobin := cfg.GOBIN
+ if gobin == "" && cfg.ModulesEnabled {
+ gobin = modload.BinDir(ld)
+ } else if gobin == "" {
+ // Best effort guess of where the binary will be installed.
+ // go.dev/issue/23439
+ wd, err := os.Getwd()
+ if err == nil {
+ gopaths := filepath.SplitList(cfg.BuildContext.GOPATH)
+ gopath := gopaths[0]
+ for _, p := range gopaths {
+ if strings.HasPrefix(wd, p) {
+ gopath = p
+ break
+ }
+ }
+ gobin = filepath.Join(gopath, "bin")
+ }
+ }
+
return []cfg.EnvVar{
{Name: "GOMOD", Value: gomod},
{Name: "GOWORK", Value: gowork},
+ {Name: "GOBIN", Value: gobin, Changed: cfg.GOBINChanged},
}
}

diff --git a/src/cmd/go/testdata/script/env_gobin.txt b/src/cmd/go/testdata/script/env_gobin.txt
new file mode 100644
index 0000000..0e0efb9
--- /dev/null
+++ b/src/cmd/go/testdata/script/env_gobin.txt
@@ -0,0 +1,44 @@
+
+go env GOBIN
+stdout $WORK${/}gopath${/}bin
+go env -changed GOBIN
+! stdout $WORK
+
+# report explicit settings
+env GOBIN=/tmp/gobin
+go env GOBIN
+stdout /tmp/gobin
+go env -changed GOBIN
+stdout /tmp/gobin
+
+# reset
+env GOBIN=
+go env -changed GOBIN
+! stdout $WORK
+
+# In GOPATH mode
+# the default install directory depends on the GOPATH
+# technically it also depends on the install target
+# but that's not something we can report.
+env GO111MODULE=off
+env GOPATH=$WORK/a${:}$WORK/b
+
+# report GOPATH[0]/bin outside of GOPATH
+cd $WORK
+go env GOBIN
+stdout $WORK${/}a${/}bin
+
+# report the current GOPATH/bin inside of a GOPATH
+cd $WORK/a/src/example1.com
+go env GOBIN
+stdout $WORK${/}a${/}bin
+
+cd $WORK/b/src/example2.com
+go env GOBIN
+stdout $WORK${/}b${/}bin
+
+-- $WORK/a/src/example1.com/main.go --
+package main
+
+-- $WORK/b/src/example2.com/main.go --
+package main

Change information

Files:
  • M src/cmd/go/internal/cfg/cfg.go
  • M src/cmd/go/internal/envcmd/env.go
  • A src/cmd/go/testdata/script/env_gobin.txt
Change size: M
Delta: 3 files changed, 67 insertions(+), 3 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
Gerrit-Change-Number: 778560
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Liao <se...@liao.dev>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
May 16, 2026, 4:29:01 PM (3 days ago) May 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Sean Liao

Sean Liao uploaded new patchset

Sean Liao uploaded patch set #2 to this change.
Following approvals got outdated and were removed:
Open in Gerrit

Related details

Attention is currently required from:
  • Sean Liao
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
Gerrit-Change-Number: 778560
Gerrit-PatchSet: 2
Gerrit-Owner: Sean Liao <se...@liao.dev>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
May 16, 2026, 4:56:53 PM (3 days ago) May 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Sean Liao

Sean Liao uploaded new patchset

Sean Liao uploaded patch set #3 to this change.
Following approvals got outdated and were removed:
Open in Gerrit

Related details

Attention is currently required from:
  • Sean Liao
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
Gerrit-Change-Number: 778560
Gerrit-PatchSet: 3
unsatisfied_requirement
satisfied_requirement
open
diffy

Hyang-Ah Hana Kim (Gerrit)

unread,
May 18, 2026, 2:25:08 PM (18 hours ago) May 18
to goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, golang-co...@googlegroups.com
Attention needed from Michael Matloob and Sean Liao

Hyang-Ah Hana Kim voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Matloob
  • Sean Liao
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
    Gerrit-Change-Number: 778560
    Gerrit-PatchSet: 3
    Gerrit-Owner: Sean Liao <se...@liao.dev>
    Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Sean Liao <se...@liao.dev>
    Gerrit-Comment-Date: Mon, 18 May 2026 18:25:04 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    qiu laidongfeng (Gerrit)

    unread,
    May 18, 2026, 9:39:25 PM (10 hours ago) May 18
    to goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, golang-co...@googlegroups.com
    Attention needed from Michael Matloob and Sean Liao

    qiu laidongfeng added 1 comment

    File src/cmd/go/testdata/script/env_gobin.txt
    Line 6, Patchset 3 (Latest):
    qiu laidongfeng . unresolved

    add test when env GOBIN=$WORK${/}gopath${/}bin

    Currently, explicitly setting other environment variables to their default values (e.g., running set GOAMD64=v1 in CMD) is not reported by go env -changed, which does not seem to be the case for GOBIN with this CL.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Matloob
    • Sean Liao
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
      Gerrit-Change-Number: 778560
      Gerrit-PatchSet: 3
      Gerrit-Owner: Sean Liao <se...@liao.dev>
      Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-CC: qiu laidongfeng <26454...@qq.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Attention: Sean Liao <se...@liao.dev>
      Gerrit-Comment-Date: Tue, 19 May 2026 01:39:17 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Sean Liao (Gerrit)

      unread,
      May 18, 2026, 10:08:05 PM (10 hours ago) May 18
      to goph...@pubsubhelper.golang.org, qiu laidongfeng, Hyang-Ah Hana Kim, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, golang-co...@googlegroups.com
      Attention needed from Michael Matloob and qiu laidongfeng

      Sean Liao added 1 comment

      File src/cmd/go/testdata/script/env_gobin.txt
      qiu laidongfeng . resolved

      add test when env GOBIN=$WORK${/}gopath${/}bin

      Currently, explicitly setting other environment variables to their default values (e.g., running set GOAMD64=v1 in CMD) is not reported by go env -changed, which does not seem to be the case for GOBIN with this CL.

      Sean Liao

      The issue has been GOBIN does not have a real default value, the default behavior of go install changes based on the arguments, so explicitly setting GOBIN=GOPATH[0]/bin is materially different from relying on the default.
      That's why I think -changed should report any value that's set.

      https://go-review.googlesource.com/c/go/+/232017/4#message-75b3b75fc59c83ddca9fba768d9a7ced203f4c29

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • qiu laidongfeng
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
        Gerrit-Change-Number: 778560
        Gerrit-PatchSet: 3
        Gerrit-Owner: Sean Liao <se...@liao.dev>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
        Gerrit-Reviewer: Sean Liao <se...@liao.dev>
        Gerrit-CC: qiu laidongfeng <26454...@qq.com>
        Gerrit-Attention: qiu laidongfeng <26454...@qq.com>
        Gerrit-Attention: Michael Matloob <mat...@golang.org>
        Gerrit-Comment-Date: Tue, 19 May 2026 02:07:59 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: qiu laidongfeng <26454...@qq.com>
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy

        qiu laidongfeng (Gerrit)

        unread,
        May 18, 2026, 10:25:04 PM (10 hours ago) May 18
        to goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, golang-co...@googlegroups.com
        Attention needed from Michael Matloob and Sean Liao

        qiu laidongfeng voted Code-Review+1

        Code-Review+1
        Open in Gerrit

        Related details

        Attention is currently required from:
        • Michael Matloob
        • Sean Liao
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ie705793db6090d2d735af1b62064c13c6a6a6964
        Gerrit-Change-Number: 778560
        Gerrit-PatchSet: 3
        Gerrit-Owner: Sean Liao <se...@liao.dev>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
        Gerrit-Reviewer: Sean Liao <se...@liao.dev>
        Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
        Gerrit-Attention: Michael Matloob <mat...@golang.org>
        Gerrit-Attention: Sean Liao <se...@liao.dev>
        Gerrit-Comment-Date: Tue, 19 May 2026 02:24:56 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages