cmd/go: set GOROOT explicitly for 'go generate' subprocesses

Code generators may reasonably expect to find the GOROOT for which the
code is being generated.

If the generator invokes 'go run' (which ought to be reasonable to do)
and the user has set 'GOFLAGS=trimpath' (which also ought to be
reasonable), then either 'go generate' or 'go run' needs to set GOROOT
explicitly.

I would argue that it is more appropriate for 'go generate' to set
GOROOT than for 'go run' to do so, since a user may reasonably invoke
'go run' to reproduce a user-reported bug in a standalone Go program,
but should not invoke 'go generate' except to regenerate code for a Go
package.

Updates #51461.

Change-Id: Iceba233b4eebd57c40cf5dcd4af9031d210dc9d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/399157
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
3 files changed
tree: 63da9196bb2e4913d7514a85ad083f3980940682
  1. .github/
  2. api/
  3. doc/
  4. lib/
  5. misc/
  6. src/
  7. test/
  8. .gitattributes
  9. .gitignore
  10. AUTHORS
  11. codereview.cfg
  12. CONTRIBUTING.md
  13. CONTRIBUTORS
  14. LICENSE
  15. PATENTS
  16. README.md
  17. SECURITY.md
README.md

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://golang.org/wiki/Questions for a list of places to ask questions about the Go language.