1
0
mirror of https://github.com/golang/go synced 2024-11-22 23:50:03 -07:00

cmd/go: update PWD variable for 'go generate'

Most subprocess invocations in the go command use base.AppendPWD to
append an accurate value of PWD to the command's environment, which can
speed up calls like os.Getwd and also help to provide less-confusing
output from scripts. Update `go generate` to do so.

Fixes #43862

Change-Id: I3b756f1532b2d922f7d74fd86414d5567a0122c0
This commit is contained in:
Tao Qingyun 2021-01-27 10:39:57 +08:00
parent f3c86ba07c
commit 01293bf553
2 changed files with 11 additions and 2 deletions

View File

@ -334,6 +334,7 @@ func (g *Generator) setEnv() {
"GOPACKAGE=" + g.pkg,
"DOLLAR=" + "$",
}
g.env = base.AppendPWD(g.env, g.dir)
}
// split breaks the line into words, evaluating quoted
@ -448,7 +449,7 @@ func (g *Generator) exec(words []string) {
cmd.Stderr = os.Stderr
// Run the command in the package directory.
cmd.Dir = g.dir
cmd.Env = base.AppendPWD(str.StringList(cfg.OrigEnv, g.env), cmd.Dir)
cmd.Env = str.StringList(cfg.OrigEnv, g.env)
err := cmd.Run()
if err != nil {
g.errorf("running %q: %s", words[0], err)

View File

@ -26,6 +26,10 @@ stdout 'yes' # flag.go should select yes
go generate './generate/env_test.go'
stdout 'main_test'
# Test go generate provides the right "$PWD"
go generate './generate/env_pwd.go'
stdout $WORK'/gopath/src/generate'
-- echo.go --
package main
@ -88,4 +92,8 @@ package p
-- generate/env_test.go --
package main_test
//go:generate echo $GOPACKAGE
//go:generate echo $GOPACKAGE
-- generate/env_pwd.go --
package p
//go:generate echo $PWD