Repository: go
Issue: cmd/go: 'go test' fails to vet runtime/cgo when gcc not installed
Labels: NeedsInvestigation,GoCommand,Analysis

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.15.5 linux/amd64
</pre>

### Does this issue reproduce with the latest release?
Yes


### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
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-build679279315=/tmp/go-build -gno-record-gcc-switches"


</pre></details>

### What did you do?
I am running `go test` during the docker image build. My codebase consists of a single Go package and doesn't have any cgo code. Here is my Dockerfile:
```Dockerfile
FROM golang:1.15.5 as build

WORKDIR /go/src/app

# Cache Go dependencies
COPY go.mod go.sum ./
RUN go mod download

COPY . .

# This is needed to prove that the Go standard library gets recompiled. 
# Since in order to recompile it requires gcc. 
RUN apt-get remove -y gcc
RUN go test ./...
```


### What did you expect to see?
I expected to see that `go test` successfully finishes after running my tests



### What did you see instead?
`go test` failed with this message: 
```
# runtime/cgo
exec: "gcc": executable file not found in $PATH
FAIL    github.com/georgysavva/driver-app/gateway/pkg/gateway [build failed]
FAIL    github.com/georgysavva/driver-app/gateway/pkg/gateway2 [build failed]
```

I've noticed that if I add `go build -i -a std` before the `go test ./...` command. It no longer needs to recompile:
```Dockerfile
FROM golang:1.15.5 as build

WORKDIR /go/src/app

# Cache Go dependencies
COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -i -a std
# This is needed to prove that the Go standard library doesn't get recompiled.
RUN apt-get remove -y gcc
RUN go test ./...
```
`go test ./...` finishes successfully in that case. 

My question is what is the condition for `go test ./...` to recompile that standard library, because if I remove any of those `go build` flags: `-i` or `-a` the solution is no longer works. And why would `go test ./...` need to rebuild the standard library on a fresh Go installation at all? 
BTW, the situation is the same if I do `go vet ./...` instead of `go test ./...`.

I encountered this issue the first time with golang alpine image(golang:1.15.5-alpine3.12) because it doesn't have gcc installed. So this is pretty related to https://github.com/golang/go/issues/28065. Maybe it's worth adding that command `go build -i -a std` to the official golang images to fix that kind of problem in user images that inherit them?  
Thanks!