cmd/go, go/build: add ToolTags to build.Default

The build.Default context really needs to accurately describe
the default build context. The goexperiment tags being a special
case in the go command violates that rule and is the root cause
of the various try-bot failures blocking the enabling of regabi.

(The cleanups I made in golang.org/x/tools were long overdue
but are not strictly necessary for making regabi work; this CL is.)

Having moved the GOEXPERIMENT parsing into internal/buildcfg,
go/build can now use it to set up build.Default, in the new field
ToolTags, meant to hold toolchain-determined tags (for now,
just the experiments). And at the same time we can remove the
duplication of GOOS and GOARCH defaults.

And then once build.Default is set up accurately, the special case
code in cmd/go itself can be removed, and the special case code
in test/run.go is at least a bit less special.

Change-Id: Ib7394e10aa018e492cb9a83fb8fb9a5011a8c25b
Reviewed-on: https://go-review.googlesource.com/c/go/+/310732
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Russ Cox 2021-04-15 23:07:41 -04:00
parent 95ed5c3800
commit dba89283ad
6 changed files with 35 additions and 38 deletions

View file

@ -12,6 +12,7 @@ import (
"errors"
"flag"
"fmt"
"go/build"
"hash/fnv"
"io"
"io/fs"
@ -376,7 +377,6 @@ type context struct {
GOARCH string
cgoEnabled bool
noOptEnv bool
expTags map[string]bool // Set lazily
}
// shouldTest looks for build tags in a source file and returns
@ -447,27 +447,12 @@ func (ctxt *context) match(name string) bool {
}
if strings.HasPrefix(name, "goexperiment.") {
// Query goexperiment tags from the toolchain.
if ctxt.expTags == nil {
ctxt.expTags = make(map[string]bool)
cmd := exec.Command(goTool(), "tool", "compile", "-V=goexperiment")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("failed to get GOEXPERIMENT configuration:\n%s", out)
}
i := bytes.Index(out, []byte("X:"))
if i != -1 {
for _, exp := range strings.Split(string(out[i+2:]), ",") {
v := true
if strings.HasPrefix(exp, "no") {
v, exp = false, exp[2:]
}
ctxt.expTags["goexperiment."+exp] = v
}
for _, tag := range build.Default.ToolTags {
if tag == name {
return true
}
}
return ctxt.expTags[name]
return false
}
if name == "cgo" && ctxt.cgoEnabled {