go/packages: don't use os.LookupEnv

Get the value of GOARCH from the config instead.
It should have been set to the same as the one from the environment by
default, but may have been overidden by the caller.

Change-Id: If9a6c0ae998c1c72ad2a68fe83c8bb9f5614a189
Reviewed-on: https://go-review.googlesource.com/c/142361
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Cottrell 2018-10-15 18:09:00 -04:00
parent 5d4988d199
commit 3bba456143
2 changed files with 7 additions and 7 deletions

View File

@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"golang.org/x/tools/go/gcexportdata"
@ -685,8 +686,11 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
// This is only an approximation.
// TODO(adonovan): derive Sizes from the underlying build system.
goarch := runtime.GOARCH
if x, ok := os.LookupEnv("GOARCH"); ok {
goarch = x
const goarchPrefix = "GOARCH="
for _, e := range ld.Config.Env {
if strings.HasPrefix(e, goarchPrefix) {
goarch = e[len(goarchPrefix):]
}
}
sizes := types.SizesFor("gc", goarch)

View File

@ -1052,15 +1052,11 @@ func TestSizes(t *testing.T) {
})
defer cleanup()
savedGOARCH := os.Getenv("GOARCH")
defer os.Setenv("GOARCH", savedGOARCH)
for arch, wantWordSize := range map[string]int64{"386": 32, "amd64": 64} {
os.Setenv("GOARCH", arch)
cfg := &packages.Config{
Mode: packages.LoadSyntax,
Dir: tmp,
Env: append(os.Environ(), "GOPATH="+tmp, "GO111MODULE=off"),
Env: append(os.Environ(), "GOARCH="+arch, "GOPATH="+tmp, "GO111MODULE=off"),
}
initial, err := packages.Load(cfg, "a")
if err != nil {