`go-generate` fails on `GOPATH is not set`
go-generate doesn't work
$ pants go-generate ::
13:56:18.02 [INFO] Completed: Process `go generate` directives in file: pkg/something.go
13:56:18.02 [ERROR] 1 Exception encountered:
Engine traceback:
in `go-generate` goal
ProcessExecutionFailure: Process 'Process `go generate` directives in file: pkg/something.go' failed with exit code 1.
stdout:
stderr:
2024/10/09 13:56:18 Loading input failed: GOPATH is not set
Use `--keep-sandboxes=on_failure` to preserve the process chroot for inspection.
Pants version
Tested 2.22, 2.21 and 2.23
OS Both Linux and Mac
Small reproduction repo https://github.com/rdeknijf/pants-repro-gogen
Sorry, forgot some info. I did of course check the GOPATH setting. It's really set. I've tried several ways. Pants seems to ignore it.
From my initial investigation, the more fundamental issue appears to be that mockgen assumes it is being run inside the actual source tree, which is not the case with Pants. The Pants implementation of go generate runs the code inside the Pants execution sandbox with only the sources for the applicable Go package available.
I verified that GOPATH is not set when running go generate by adding //go:generate /bin/sh -c "env > envs.txt" and running go generate. GOPATH was not set in the output. Thus, GOPATH is likely being inferred based on the source tree setup seen by mockgen.