From f0b6d3753f57bf37487b127f968920743c401ed9 Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Fri, 5 Mar 2021 04:01:00 +0000 Subject: [PATCH] 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 GitHub-Last-Rev: 3ec8da265a2777d1dcbcea00f107b8f5905f3640 GitHub-Pull-Request: golang/go#43940 Reviewed-on: https://go-review.googlesource.com/c/go/+/287152 Reviewed-by: Bryan C. Mills Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot Trust: Baokun Lee --- src/cmd/go/internal/generate/generate.go | 1 + src/cmd/go/testdata/script/generate.txt | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/generate/generate.go b/src/cmd/go/internal/generate/generate.go index a48311d51b0..97df229b315 100644 --- a/src/cmd/go/internal/generate/generate.go +++ b/src/cmd/go/internal/generate/generate.go @@ -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 diff --git a/src/cmd/go/testdata/script/generate.txt b/src/cmd/go/testdata/script/generate.txt index c3c563e5f4a..73f5bbd57a9 100644 --- a/src/cmd/go/testdata/script/generate.txt +++ b/src/cmd/go/testdata/script/generate.txt @@ -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 \ No newline at end of file +//go:generate echo $GOPACKAGE +-- generate/env_pwd.go -- +package p + +//go:generate echo $PWD