Repository: go
Issue: `cmd/go`: missing diagnostic when the default `GOPATH` conflicts with an explicit `GOROOT`
Labels: help wanted,NeedsFix,GoCommand

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.20.4 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/nickz/.cache/go-build"
GOENV="/home/nickz/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/nickz/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nickz/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/nickz/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/nickz/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/test/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build373437021=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

It used to be possible to have `go run` inside `//go:generate` lines. It is very handy since a project can require the specific generator version via `go.mod`.

Following examples by `cilium/ebpf`, I created a `main.go` file

```go
package main

import (
	_ "github.com/cilium/ebpf"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go
```

...and invoked `go generate ./...`.

(I'm using a specific generator in my bug report, but it looks like a generic issue affecting anyone using `go run` inside `go:generate` technique. Therefore I believe that the specific generator being used is irrelevant.)

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on go.dev/play is best.
-->



### What did you expect to see?

The generator to complain about missing arguments.


### What did you see instead?

```
$ go generate ./...
go: module cache not found: neither GOMODCACHE nor GOPATH is set
cmd/test/main.go:10: running "env": exit status 1
```

### Further info

If I invoke `go run` directly, everything works as expected.
```
$ go run github.com/cilium/ebpf/cmd/bpf2go
Error: missing package, are you running via go generate?
exit status 1

$ GOPACKAGE=main go run github.com/cilium/ebpf/cmd/bpf2go
Error: expected at least two arguments
exit status 1
```
It's clear that the generator gets executed and complains as expected.

However, if I set `GOROOT` (as `go generate` does):

```
$ GOROOT=/home/nickz/go GOPACKAGE=main go run github.com/cilium/ebpf/cmd/bpf2go
go: module cache not found: neither GOMODCACHE nor GOPATH is set
```

Changing `go:generate` line as below also "fixes" the problem:
```
//go:generate env --unset=GOROOT go run github.com/cilium/ebpf/cmd/bpf2go
```
```
$ go generate ./...
Error: expected at least two arguments
exit status 1
```

I believe that I have the most boring go setup possible. `$ env | grep GO` yields nothing.