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

x/tools/go/packages: Config.Env always uses current environment, not as documented only when it's nil #42590

Closed
fho opened this issue Nov 13, 2020 · 1 comment
Labels
Documentation FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@fho
Copy link

fho commented Nov 13, 2020

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

go version go1.15.5 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
circleci@0b9c61f00e98:/go$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/circleci/.cache/go-build"
GOENV="/home/circleci/.config/go/env"
GOEXE=""
GOFLAGS="-mod=vendor"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/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"
GCCGO="gccgo"
AR="ar"
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-build517168634=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm using packages.Load (golang.org/x/tools/go/packages) to collect source files of go packages.
My program is calling packagesLoad() with Config.Env set to []string{"GO111MODULE=on", "GOFLAGS=-mod=readonly"}.
I ran the program in an environment where the environment variable GOPATH was set to /go.

What did you expect to see?

I expected that packages.Load() runs go list without the GOPATH variable of my current environment, instead that either GOPATH is unset or the default value is used.

I expected this because the documentation of Config.Env states:

// Env is the environment to use when invoking the build system's query tool.
// If Env is nil, the current environment is used.

https://github.com/golang/tools/blob/eeaa07dd769687291ac8e71ec39a7db382b22b65/go/packages/packages.go#L130

What did you see instead?

The setting of my current environment were used for the go list execution.
go list is run via Invocation.run() which uses the current environment and appends additional environment variables to the slice:

	cmd.Env = append(os.Environ(), i.Env...)

https://github.com/golang/tools/blob/eeaa07dd769687291ac8e71ec39a7db382b22b65/internal/gocommand/invoke.go#L210

That Invocation.run() always uses the current environment was introduced in commit:
golang/tools@f8bfb4e#diff-55f055b305279edb4e62ce29481a985df8d488314120de25803e0336efdea6e1

I would prefer if instead of correcting the documentation, packages.Load() would work as it is currently documented.
It makes it easy to run the function in a controlled environment.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Nov 13, 2020
@gopherbot gopherbot added this to the Unreleased milestone Nov 13, 2020
@fho fho changed the title x/tools/go/packages: Config.Env uses current environment, opposite behaviour then documented x/tools/go/packages: Config.Env always uses current environment, not as documented only when it's nil Nov 13, 2020
@gopherbot
Copy link

Change https://golang.org/cl/270038 mentions this issue: go/packages: start with empty environment

@golang golang locked and limited conversation to collaborators Nov 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

2 participants