go: cmd/go,runtime: `runtime.GOROOT` returns an invalid, non-empty string when built with -trimpath
Reopening #51409 because I don’t think “it’s a counterfeiter bug” is a sufficient explanation. How can counterfeiter (or any application) cause the go tool to lose track of the implied GOROOT?
What version of Go are you using (go version
)?
$ go version go version go1.17.7 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
$ go env GO111MODULE="auto" GOARCH="amd64" GOBIN="" GOCACHE="/tmp/.gocache" GOENV="/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/Users/rittneje/test/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/Users/rittneje/test" 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.7" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/Users/rittneje/test/src/gorootbug/go.mod" 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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1092411943=/tmp/go-build -gno-record-gcc-switches"
Note that the GOROOT
environment variable is not set. (It is being calculated automatically from the go command’s location.)
What did you do?
package gorootbug
//go:generate /usr/local/gotools/bin/counterfeiter io.Reader
Then ran go generate
.
What did you expect to see?
It should work.
What did you see instead?
$ go generate -x
/usr/local/gotools/bin/counterfeiter io.Reader
Writing `FakeReader` to `gorootbugfakes/fake_reader.go`...
go/build: go list io: exit status 2
go: cannot find GOROOT directory: go
bug.go:3: running "/usr/local/gotools/bin/counterfeiter": exit status 1
If I explicitly set GOROOT to itself, then it works.
$ GOROOT=$(go env GOROOT) go generate -x
/usr/local/gotools/bin/counterfeiter io.Reader
Writing `FakeReader` to `gorootbugfakes/fake_reader.go`... Done
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (7 by maintainers)
Commits related to this issue
- cmd/compile/internal/syntax: don't try to parse files in GOROOT/.git This test was failing locally in my clone of the go repo due to a Git branch ending in ".go", which the test found and was attempt... — committed to golang/go by bcmills 2 years ago
- runtime/pprof: do not require a GOROOT/src prefix in tests When paths are trimmed, the reported file locations begin with the package import path (not GOROOT/src). Updates #51461 Change-Id: Idbd408... — committed to golang/go by bcmills 2 years ago
- go/internal/srcimporter: use the 'go' command from the Importer's GOROOT We have no guarantee in general that there is any 'go' command in $PATH at all, let alone the correct one. However, we can exp... — committed to golang/go by bcmills 2 years ago
- runtime/debug: do not require a GOROOT/src prefix in TestStack When paths are trimmed, the reported file locations begin with the package import path (not GOROOT/src). Updates #51461. Change-Id: Ia... — committed to golang/go by bcmills 2 years ago
- internal/buildcfg: extract logic specific to cmd/go cmd/go/internal/cfg duplicates many of the fields of internal/buildcfg, but initializes them from a Go environment file in addition to the usual pr... — committed to golang/go by bcmills 2 years ago
- internal/buildcfg: initialize GOROOT to runtime.GOROOT In the beginning the Go compiler was in C, and C had a function 'getgoroot' that returned GOROOT from either the environment or a generated cons... — committed to golang/go by bcmills 2 years ago
- internal/testenv: add GOROOT and use it to fix tests broken with -trimpath This fixes many (but not all) of the tests that currently fail (due to a bogus path reported by runtime.GOROOT) when run wit... — committed to golang/go by bcmills 2 years ago
- cmd/go/internal/test: ensure that build.ToolDir is accurate in tests This fixes a build failure due to inability to locate the "vet" tool when the test binary is built with -trimpath. Updates #51461... — committed to golang/go by bcmills 2 years ago
- cmd/go: include the "-trimpath" flag in the stamped build settings The -trimpath flag has a strong effect on the resulting binary: in particular, it determines whether runtime.GOROOT can report a mea... — committed to golang/go by bcmills 2 years ago
- cmd/go: fix TestScript/build_trimpath_goroot when built with a mismatched GOROOT_FINAL Fixes #52236. Updates #51461. Change-Id: Ie91e0256afd45e9bbd60fd8cdc696363027ab696 Reviewed-on: https://go-revi... — committed to golang/go by bcmills 2 years ago
- cmd/go: set GOROOT explicitly for 'go generate' subprocesses Code generators may reasonably expect to find the GOROOT for which the code is being generated. If the generator invokes 'go run' (which ... — committed to golang/go by bcmills 2 years ago
- doc/go1.19: document cmd/go changes involving -trimpath Updates #51461. Change-Id: Ie878a9f630062d62027de895750a070b50428a9f Reviewed-on: https://go-review.googlesource.com/c/go/+/399214 Run-TryBot:... — committed to golang/go by bcmills 2 years ago
- cmd/fix: disallow cgo errors in tests The 'cgo' command invoked by 'go fix' was not valid when built with -trimpath, but the test was not failing because errors from the command were being logged and... — committed to golang/go by bcmills 2 years ago
@seankhliao @bcmills I have tracked this issue down to a bug in Go. When you pass
-trimpath
togo build
(or similar), it corruptsruntime.GOROOT()
in the resulting binary.With
go run
, it prints/usr/local/go
as expected. Withgo run -trimpath
, it incorrectly printsgo
.