From d99ff0382116bb472b9f92ddf23cb22bf145bbcd Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 9 Mar 2022 17:17:40 -0500 Subject: [PATCH] go/build: set PWD for go subcommands MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Since these commands already include an explicit Env field, they will not be fixed automatically by proposal #50599. Change-Id: Ia8157a71cf0cfe208bdc0da9aef54be3d26c795f Reviewed-on: https://go-review.googlesource.com/c/go/+/391804 Trust: Bryan Mills Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Daniel Martí Trust: Daniel Martí --- src/go/build/build.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/go/build/build.go b/src/go/build/build.go index baf76e6b7f..c1d044e55a 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -1186,6 +1186,13 @@ func (ctxt *Context) importGo(p *Package, path, srcDir string, mode ImportMode) "GOPATH="+ctxt.GOPATH, "CGO_ENABLED="+cgo, ) + if cmd.Dir != "" { + // If possible, set PWD: if an error occurs and PWD includes a symlink, we + // want the error to refer to Dir, not some other name for it. + if abs, err := filepath.Abs(cmd.Dir); err == nil { + cmd.Env = append(cmd.Env, "PWD="+abs) + } + } if err := cmd.Run(); err != nil { return fmt.Errorf("go/build: go list %s: %v\n%s\n", path, err, stderr.String()) -- 2.50.0