Repository: go
Issue: cmd/test2json: CGO_ENABLED is set, when running tests from prebuilt binary 
Labels: NeedsInvestigation

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.18.1 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="/home/ksuszyns/.cache/go-build"
GOENV="/home/ksuszyns/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ksuszyns/.gvm/pkgsets/go1.18.1/global/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ksuszyns/.gvm/pkgsets/go1.18.1/global"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/ksuszyns/git/golang-go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ksuszyns/git/golang-go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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-build2603697588=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

1. Checkout the reproduction repo: https://github.com/cardil/repro-cgo-drift
2. Run `make repro`

### What did you expect to see?

I expect to see the test pass regardless if executed via:

```
go test -count=1 ./
``` 

or

```
go test -c -o ./repro.test ./
go tool test2json -t ./repro.test -test.count=1
```

### What did you see instead?

The process fails with the following output:

```
go test -c -o ./repro.test ./
go tool test2json -t ./repro.test -test.count=1
{"Time":"2022-08-19T12:42:13.742590288+02:00","Action":"output","Test":"TestCgo","Output":"--- FAIL: TestCgo (0.00s)\n"}
{"Time":"2022-08-19T12:42:13.743167948+02:00","Action":"output","Test":"TestCgo","Output":"    cgo_test.go:12: Expecting CGO_ENABLED equal \"\", got \"1\"\n"}
{"Time":"2022-08-19T12:42:13.743197548+02:00","Action":"fail","Test":"TestCgo","Elapsed":0}
{"Time":"2022-08-19T12:42:13.743212794+02:00","Action":"output","Output":"FAIL\n"}
{"Time":"2022-08-19T12:42:13.743499898+02:00","Action":"fail","Elapsed":0.006}
```

### Additional context

I stumbled on this problem while running tests via the Goland IDE. The Goland IDE uses the pair of commands to execute tests:

```
go test -c -o /tmp/Goland/test.bin ./package/path
go tool test2json -t /tmp/Goland/test.bin -test.v -test.paniconexit0
```

We are executing [google/ko](https://github.com/google/ko) in our tests to built test OCI images. KO sets the `CGO_ENABLED=0` by default because the default base image is [distroless](https://github.com/GoogleContainerTools/distroless), which doesn't contain any system libraries.

When I execute tests from Goland IDE, I have effectively `CGO_ENABLED=1` when the test starts. This overrides the KO default value, and the produced image is invalid. The faulty image contains dynamic binary, which can't run on distroless.

When the tests are executed with `go test` directly, all is well.

I think running tests via `go tool test2json -t` shouldn't change the execution environment for the test.

See also: https://github.com/knative-sandbox/control-protocol/pull/204