Skip to content

Commit

Permalink
cmd/go: update PWD variable for 'go generate'
Browse files Browse the repository at this point in the history
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
GitHub-Last-Rev: 3ec8da2
GitHub-Pull-Request: #43940
Reviewed-on: https://go-review.googlesource.com/c/go/+/287152
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Baokun Lee <bk@golangcn.org>
  • Loading branch information
qingyunha authored and Bryan C. Mills committed Mar 5, 2021
1 parent 2b0e29f commit f0b6d37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cmd/go/internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/cmd/go/testdata/script/generate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit f0b6d37

Please sign in to comment.