Skip to content

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

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

Closed
fho opened this issue Nov 13, 2020 · 1 comment
Labels
Documentation Issues describing a change to 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
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label 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
Contributor

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
apstndb pushed a commit to apstndb/gotoolsdiff that referenced this issue Jan 11, 2025
go/packages is documented as starting with an empty environment if
Config.Env is populated. Fixing all uses of gocommand.Invocation was a
daunting task, so add a mode flag to it instead and set it just in
go/packages.

Clean up packagesdriver.GetSizes, which seems to be completely unused?

Fixes golang/go#42590.

Change-Id: Idac2e1a4798b4a2f5e7c8aa0a1a089a6bd3630ba
Reviewed-on: https://go-review.googlesource.com/c/tools/+/270038
Trust: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to 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