Repository: go
Issue: cmd/go: generate should set GOFLAGS env variable
Labels: NeedsInvestigation,FeatureRequest,GoCommand

Please answer these questions before submitting your issue. Thanks!

### What version of Go are you using (`go version`)?

```
go version go1.11 linux/amd64
```

### Does this issue reproduce with the latest release?

Yes.

### What operating system and processor architecture are you using (`go env`)?

```
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/myitcv/go-modules-by-example/.gopath"
GOPROXY=""
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/myitcv/go-modules-by-example/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 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build952142611=/tmp/go-build -gno-record-gcc-switches"
```

### What did you do?

Given the following setup:

```
$ cd $(mktemp -d)
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
$ cat <<EOD >hello.go
package hello
EOD
$ cat <<"EOD" >hello_js_wasm.go
// +build constraint

package hello

//go:generate echo "GOARCH: $GOARCH; GOOS: $GOOS; GOFILE: $GOFILE; GOLINE: $GOLINE; GOPACKAGE: $GOPACKAGE"
EOD
```

Then the following two command produce no output, i.e. `go generate` doesn't find any directives:

```
$ go generate
$ GOOS=js GOARCH=wasm go generate
```

Only when we combine the setting of `GOOS`, `GOARCH` and build tags is the directive run:

```
$ GOOS=js GOARCH=wasm go generate -tags constraint
GOARCH: wasm; GOOS: js; GOFILE: hello_js_wasm.go; GOLINE: 5; GOPACKAGE: hello
```

This makes sense; per `go help generate`, `GOOS` and `GOARCH` are passed through by `go generate` to each directive:

```
$ go help generate
usage: go build [-o output] [-i] [build flags] [packages]
...

Go generate sets several variables when it runs the generator:

        $GOARCH
                The execution architecture (arm, amd64, etc.)
        $GOOS
                The execution operating system (linux, windows, etc.)
        $GOFILE
                The base name of the file.
        $GOLINE
                The line number of the directive in the source file.
        $GOPACKAGE
                The name of the package of the file containing the directive.
        $DOLLAR
                A dollar sign.
...
```

However build tags are not, despite build tags clearly influencing the files that are scanned for directives.

So assuming the observed behaviour is correct/consistent, I'd like to propose that we add a `GOBUILDTAGS` env variable to complement `GOOS` and `GOARCH`. This would pass through the `-tags` build flag:

```
        -tags 'tag list'
                a space-separated list of build tags to consider satisfied during the
                build. For more information about build tags, see the description of
                build constraints in the documentation for the go/build package.
```

The use case here is needing to pass `GOOS`, `GOARCH` and build tags as supplied to `go generate` to a generator that uses [`go/packages`](https://godoc.org/golang.org/x/tools/go/packages) for type checking.

cc @robpike @rsc @bcmills