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

cmd/go: env should populate GOBIN as effective install target directory for main packages #28337

Closed
myitcv opened this issue Oct 23, 2018 · 1 comment
Labels
FeatureRequest FrozenDueToAge GoCommand cmd/go NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@myitcv
Copy link
Member

myitcv commented Oct 23, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/gopher/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/gopher/gopath"
GOPROXY=""
GORACE=""
GOROOT="/go"
GOTMPDIR=""
GOTOOLDIR="/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build992263907=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ mkdir main
$ cd main
$ go mod init example.com/main
go: creating new go.mod: module example.com/main
$ cat <<EOD >main.go
package main

func main() {}
EOD

There is currently no simple way to see the directory go install will use as the target for this main package. go list gives us the final target:

$ go list -f "{{.Target}}" .
/home/gopher/gopath/bin/main

go env simply gives us the current environment values of GOBIN and GOPATH:

$ go env | grep 'GOBIN\|GOPATH'
GOBIN=""
GOPATH="/home/gopher/gopath"

If we then set GOBIN:

$ export GOBIN=/tmp/blah
$ go list -f "{{.Target}}" .
/tmp/blah/main
$ go env | grep 'GOBIN\|GOPATH'
GOBIN="/tmp/blah"
GOPATH="/home/gopher/gopath"

I'd like to propose that go env GOBIN be the derived directory that go install will use, following the precedence logic of GOBIN, GOPATH and os.UserHomeDir():

// pseudo code
func effectiveGOBIN() string {
	if gb := os.Getenv("GOBIN"); gb != "" {
		return gb
	}
	if gp := os.Getenv("GOPATH"); gp != "" {
		return filepath.Join(filepath.SplitList(gp)[0], "bin")
	}
	return filepath.Join(os.UserHomeDir(), "go", "bin")
}

What did you expect to see?

The output of go env giving the target directory for go install

What did you see instead?

No output of go env giving the target directory for go install

@myitcv myitcv added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. FeatureRequest GoCommand cmd/go labels Oct 23, 2018
@myitcv myitcv added this to the Go1.12 milestone Oct 23, 2018
@rsc
Copy link
Contributor

rsc commented Oct 25, 2018

This would only make sense to do if we resolve #23439 as being "always use GOPATH/bin as the default GOBIN even outside GOPATH". I'll add a note there and close this issue.

@rsc rsc closed this as completed Oct 25, 2018
@golang golang locked and limited conversation to collaborators Oct 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge GoCommand cmd/go NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants