diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 6800e1c7d2..586bc1a7ca 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -542,6 +542,9 @@ // The line number of the directive in the source file. // $GOPACKAGE // The name of the package of the file containing the directive. +// $GOROOT +// The GOROOT directory for the 'go' command that invoked the +// generator, containing the Go toolchain and standard library. // $DOLLAR // A dollar sign. // diff --git a/src/cmd/go/internal/generate/generate.go b/src/cmd/go/internal/generate/generate.go index 0021bcc75a..a46f4f8908 100644 --- a/src/cmd/go/internal/generate/generate.go +++ b/src/cmd/go/internal/generate/generate.go @@ -84,6 +84,9 @@ Go generate sets several variables when it runs the generator: The line number of the directive in the source file. $GOPACKAGE The name of the package of the file containing the directive. + $GOROOT + The GOROOT directory for the 'go' command that invoked the + generator, containing the Go toolchain and standard library. $DOLLAR A dollar sign. @@ -326,6 +329,7 @@ func isGoGenerate(buf []byte) bool { // single go:generate command. func (g *Generator) setEnv() { g.env = []string{ + "GOROOT=" + cfg.GOROOT, "GOARCH=" + cfg.BuildContext.GOARCH, "GOOS=" + cfg.BuildContext.GOOS, "GOFILE=" + g.file, diff --git a/src/cmd/go/testdata/script/build_trimpath_goroot.txt b/src/cmd/go/testdata/script/build_trimpath_goroot.txt index 91e5107e58..a26cfd23be 100644 --- a/src/cmd/go/testdata/script/build_trimpath_goroot.txt +++ b/src/cmd/go/testdata/script/build_trimpath_goroot.txt @@ -62,6 +62,11 @@ stderr 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WO stdout '^GOROOT $' stdout 'cannot find package "runtime" in any of:\n\t\(\$GOROOT not set\)\n\t'$WORK${/}gopath${/}src${/}runtime' \(from \$GOPATH\)$' +env GOFLAGS=-trimpath +go generate . +stdout '^GOROOT '$TESTGO_GOROOT'$' +stdout '^runtime '$TESTGO_GOROOT${/}src${/}runtime'$' + -- go.mod -- module example @@ -69,6 +74,8 @@ go 1.19 -- main.go -- package main +//go:generate go run . + import ( "fmt" "go/build"