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: misleading warning on GOROOT == GOPATH in module mode #47889

Open
mitar opened this issue Aug 22, 2021 · 5 comments
Open

cmd/go: misleading warning on GOROOT == GOPATH in module mode #47889

mitar opened this issue Aug 22, 2021 · 5 comments

Comments

@mitar
Copy link

@mitar mitar commented Aug 22, 2021

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

go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/local/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/local/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build561180864=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was installing go install github.com/golang/protobuf/protoc-gen-go@v1.5.2.

Inside a fresh system I did:

$ curl -L https://golang.org/dl/go1.17.linux-amd64.tar.gz | tar -C /usr/local -xz
$ export GOPATH=/usr/local/go
$ export PATH="${GOPATH}/bin:${PATH}"
$ go install github.com/golang/protobuf/protoc-gen-go@v1.5.2
warning: GOPATH set to GOROOT (/usr/local/go) has no effect
go: downloading github.com/golang/protobuf v1.5.2
go: downloading google.golang.org/protobuf v1.26.0
$ ls -al /usr/local/go/bin
total 25120
drwxr-xr-x  2 root root     4096 Aug 22 14:11 .
drwxr-xr-x 10 root root     4096 Aug 16 16:27 ..
-rwxr-xr-x  1 root root 14007666 Aug 16 16:30 go
-rwxr-xr-x  1 root root  3318877 Aug 16 16:30 gofmt
-rwxr-xr-x  1 root root  8384142 Aug 22 14:11 protoc-gen-go
$ ls -al /root/go/bin
ls: cannot access '/root/go/bin': No such file or directory

In another fresh system I did:

$ curl -L https://golang.org/dl/go1.17.linux-amd64.tar.gz | tar -C /usr/local -xz
$ export PATH="/usr/local/go/bin:${PATH}"
$ go install github.com/golang/protobuf/protoc-gen-go@v1.5.2
go: downloading github.com/golang/protobuf v1.5.2
go: downloading google.golang.org/protobuf v1.26.0
$ ls -al /usr/local/go/bin
total 16932
drwxr-xr-x  2 root root     4096 Aug 16 16:30 .
drwxr-xr-x 10 root root     4096 Aug 16 16:27 ..
-rwxr-xr-x  1 root root 14007666 Aug 16 16:30 go
-rwxr-xr-x  1 root root  3318877 Aug 16 16:30 gofmt
$ ls -al /root/go/bin
total 8196
drwxr-xr-x 2 root root    4096 Aug 22 14:14 .
drwxr-xr-x 4 root root    4096 Aug 22 14:14 ..
-rwxr-xr-x 1 root root 8383846 Aug 22 14:14 protoc-gen-go

What did you expect to see?

As you see, setting GOPATH is critical for getting binary installation into /usr/local/go/bin instead of /root/go/bin. Without it, it does not work. So the warning saying GOPATH set to GOROOT (/usr/local/go) has no effect is misleading. Setting GOPATH has a pretty dramatic effect.

What did you see instead?

I have seen the warning saying GOPATH set to GOROOT (/usr/local/go) has no effect and I assumed I can just remove setting GOPATH but it turns out that is not true. It has an effect.

I think this is important because installation instructions only talk about setting export PATH=$PATH:/usr/local/go/bin, but without setting GOPATH. So installed binaries go into /root/go/bin which is not in PATH based on the installation instructions.

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Aug 22, 2021

Setting GOPATH to GOROOT isn't a supported way of working as it mixes user code with the standard library. If you need to control where the binaries are installed, use GOBIN.

@seankhliao seankhliao closed this Aug 22, 2021
@mitar
Copy link
Author

@mitar mitar commented Aug 22, 2021

I think issues reported here still hold:

  • Warning is misleading.
  • Documentation sets PATH only to /usr/local/go/bin but not also sets GOBIN, making go install install into a directory not on PATH.

@seankhliao seankhliao changed the title Misleading warning: warning: GOPATH set to GOROOT (/usr/local/go) has no effect cmd/go: misleading warning on GOROOT == GOPATH in module mode Aug 22, 2021
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Aug 22, 2021

Looks like none of the commands in module mode check check this
cc @bcmills @jayconrod @matloob

@jayconrod
Copy link
Contributor

@jayconrod jayconrod commented Aug 23, 2021

We should fix this. The install directory should not be set to $GOPATH/bin if the first element of GOPATH is the same as GOROOT.

We should also mention adding $GOPATH/bin to the install instructions. That's covered by #44930.

I'd recommend against installing anything in $GOROOT/bin that isn't usually there. If you're setting GOBIN, it's fine to point it to a directory already in PATH like /usr/local/bin.

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Aug 23, 2021

Can GOROOT == GOPATH be an error instead of just a warning?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants