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

go/build: wrong logic to avoid setting implicit GOPATH to GOROOT #30044

Closed
neild opened this issue Jan 31, 2019 · 2 comments
Closed

go/build: wrong logic to avoid setting implicit GOPATH to GOROOT #30044

neild opened this issue Jan 31, 2019 · 2 comments

Comments

@neild
Copy link
Contributor

neild commented Jan 31, 2019

The go/build package will implicitly set $GOPATH to $HOME. It skips setting an implicit GOPATH if it would set GOPATH/go to GOROOT.

		def := filepath.Join(home, "go")
		if def == runtime.GOROOT() {
			// Don't set the default GOPATH to GOROOT,
			// as that will trigger warnings from the go tool.
			return ""
		}

https://go.googlesource.com/go/+/master/src/go/build/build.go#272

Note that the comment and the code disagree. I suspect the comment describes the intended behavior (don't set the default GOPATH to GOROOT), not the code.

Discovered when testing something with a GOROOT of ~/go:

# /usr/bin/go: All is good.
$ go test .
?       github.com/neild/foo    [no test files]

# ~/go/bin/go: Unhappy. Huh?
$ ~/go/bin/go test .
missing $GOPATH

# Explicitly set GOPATH=$HOME: All is good.
$ GOPATH=$HOME ~/go/bin/go test .
?       github.com/neild/foo    [no test files]

# This is presumably the case the code is trying to avoid: Set GOPATH=GOROOT.
$ GOPATH=$HOME/go ~/go/bin/go test .
warning: GOPATH set to GOROOT (/usr/local/google/home/dneil/go) has no effect
?       github.com/neild/foo    [no test files]
@ianlancetaylor
Copy link
Contributor

The default GOPATH isn't HOME, it's HOME/go (as documented at https://golang.org/cmd/go/#hdr-GOPATH_environment_variable). We avoid that default if HOME/go is already GOROOT. I think the code and comment are correct.

@neild
Copy link
Contributor Author

neild commented Feb 1, 2019

My mistake; I misread what was being returned for GOPATH. You're absolutely right.

@neild neild closed this as completed Feb 1, 2019
@golang golang locked and limited conversation to collaborators Feb 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants