Skip to content

Commit 437bd90

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/base: rename EnvForDir to AppendPWD
EnvForDir does not immediately evoke “append”, and thus may not prompt the reader to consider the possibility of aliasing bugs (as in issue #38077). To make this behavior more obvious at the call site, rename cmd/go/internal/base.EnvForDir to AppendPWD and swap the order of arguments to a conventional “append” function (similar to those in the strconv package). For #38077 Change-Id: I16f09aa0fa8a269d51f0511eb402a44e2759eb94 Reviewed-on: https://go-review.googlesource.com/c/go/+/225578 Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent bfb1342 commit 437bd90

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

src/cmd/go/internal/base/env.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
package base
66

7-
// EnvForDir returns a modified environment suitable for running in the given
8-
// directory.
9-
// The environment is the supplied base environment but with an updated $PWD, so
10-
// that an os.Getwd in the child will be faster.
11-
func EnvForDir(dir string, base []string) []string {
12-
// Internally we only use rooted paths, so dir is rooted.
13-
// Even if dir is not rooted, no harm done.
7+
// AppendPWD returns the result of appending PWD=dir to the environment base.
8+
//
9+
// The resulting environment makes os.Getwd more efficient for a subprocess
10+
// running in dir.
11+
func AppendPWD(base []string, dir string) []string {
12+
// Internally we only use absolute paths, so dir is absolute.
13+
// Even if dir is not absolute, no harm done.
1414
return append(base, "PWD="+dir)
1515
}

src/cmd/go/internal/get/vcs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
430430

431431
cmd := exec.Command(v.cmd, args...)
432432
cmd.Dir = dir
433-
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
433+
cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir)
434434
if cfg.BuildX {
435435
fmt.Fprintf(os.Stderr, "cd %s\n", dir)
436436
fmt.Fprintf(os.Stderr, "%s %s\n", v.cmd, strings.Join(args, " "))

src/cmd/go/internal/test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
11571157

11581158
cmd := exec.Command(args[0], args[1:]...)
11591159
cmd.Dir = a.Package.Dir
1160-
cmd.Env = base.EnvForDir(cmd.Dir, cfg.OrigEnv[:len(cfg.OrigEnv):len(cfg.OrigEnv)])
1160+
cmd.Env = base.AppendPWD(cfg.OrigEnv[:len(cfg.OrigEnv):len(cfg.OrigEnv)], cmd.Dir)
11611161
cmd.Stdout = stdout
11621162
cmd.Stderr = stdout
11631163

src/cmd/go/internal/work/buildid.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (b *Builder) toolID(name string) string {
185185

186186
cmdline := str.StringList(cfg.BuildToolexec, path, "-V=full")
187187
cmd := exec.Command(cmdline[0], cmdline[1:]...)
188-
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
188+
cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir)
189189
var stdout, stderr bytes.Buffer
190190
cmd.Stdout = &stdout
191191
cmd.Stderr = &stderr
@@ -244,7 +244,7 @@ func (b *Builder) gccgoToolID(name, language string) (string, error) {
244244
// compile an empty file on standard input.
245245
cmdline := str.StringList(cfg.BuildToolexec, name, "-###", "-x", language, "-c", "-")
246246
cmd := exec.Command(cmdline[0], cmdline[1:]...)
247-
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
247+
cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir)
248248
// Force untranslated output so that we see the string "version".
249249
cmd.Env = append(cmd.Env, "LC_ALL=C")
250250
out, err := cmd.CombinedOutput()

src/cmd/go/internal/work/exec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ func (b *Builder) runOut(a *Action, dir string, env []string, cmdargs ...interfa
19251925
cleanup := passLongArgsInResponseFiles(cmd)
19261926
defer cleanup()
19271927
cmd.Dir = dir
1928-
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
1928+
cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir)
19291929
cmd.Env = append(cmd.Env, env...)
19301930
start := time.Now()
19311931
err := cmd.Run()
@@ -2381,7 +2381,7 @@ func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
23812381
}
23822382
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
23832383
cmd.Dir = b.WorkDir
2384-
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
2384+
cmd.Env = base.AppendPWD(os.Environ(), cmd.Dir)
23852385
cmd.Env = append(cmd.Env, "LC_ALL=C")
23862386
out, _ := cmd.CombinedOutput()
23872387
// GCC says "unrecognized command line option".

0 commit comments

Comments
 (0)