mirror of
https://github.com/golang/go
synced 2024-11-17 19:04:47 -07:00
cmd/go/internal/base: remove MergeEnvLists
This internally exported function allowed merging environment variable lists, and was mostly a convenience for the rest of cmd/go/internal. It seems to date all the way back to 2013. However, since CL 37586 in early 2017, os/exec has already taken care of deduplicating environment variable lists. Thus, it's unnecessary for cmd/go to take care of that before calling exec.Cmd.Start. Moreover, because os/exec will deduplicate the list in any case, we're adding extra work in all these scenarios. Finally, remove an unnecessary addition of GOROOT= in internal/tool. cfg.OrigEnv may not have the correct GOROOT set up, but os.Environ does; cmd/go's main function makes sure of that. Change-Id: I1f92f65fb927dc15bc7b0397cfd1a572b6337bb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/164703 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
5f40351708
commit
2f02daaa46
@ -4,34 +4,12 @@
|
||||
|
||||
package base
|
||||
|
||||
import "strings"
|
||||
|
||||
// EnvForDir returns a copy of the environment
|
||||
// suitable for running in the given directory.
|
||||
// The environment is the current process's environment
|
||||
// but with an updated $PWD, so that an os.Getwd in the
|
||||
// child will be faster.
|
||||
// EnvForDir returns a modified environment suitable for running in the given
|
||||
// directory.
|
||||
// The environment is the supplied base environment but with an updated $PWD, so
|
||||
// that an os.Getwd in the child will be faster.
|
||||
func EnvForDir(dir string, base []string) []string {
|
||||
// Internally we only use rooted paths, so dir is rooted.
|
||||
// Even if dir is not rooted, no harm done.
|
||||
return MergeEnvLists([]string{"PWD=" + dir}, base)
|
||||
}
|
||||
|
||||
// MergeEnvLists merges the two environment lists such that
|
||||
// variables with the same name in "in" replace those in "out".
|
||||
// This always returns a newly allocated slice.
|
||||
func MergeEnvLists(in, out []string) []string {
|
||||
out = append([]string(nil), out...)
|
||||
NextVar:
|
||||
for _, inkv := range in {
|
||||
k := strings.SplitAfterN(inkv, "=", 2)[0]
|
||||
for i, outkv := range out {
|
||||
if strings.HasPrefix(outkv, k) {
|
||||
out[i] = inkv
|
||||
continue NextVar
|
||||
}
|
||||
}
|
||||
out = append(out, inkv)
|
||||
}
|
||||
return out
|
||||
return append(base, "PWD="+dir)
|
||||
}
|
||||
|
@ -428,7 +428,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.MergeEnvLists(g.env, cfg.OrigEnv)
|
||||
cmd.Env = append(cfg.OrigEnv, g.env...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
g.errorf("running %q: %s", words[0], err)
|
||||
|
@ -83,8 +83,6 @@ func runTool(cmd *base.Command, args []string) {
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
Stderr: os.Stderr,
|
||||
// Set $GOROOT, mainly for go tool dist.
|
||||
Env: base.MergeEnvLists([]string{"GOROOT=" + cfg.GOROOT}, os.Environ()),
|
||||
}
|
||||
err := toolCmd.Run()
|
||||
if err != nil {
|
||||
|
@ -1903,7 +1903,8 @@ func (b *Builder) runOut(dir string, env []string, cmdargs ...interface{}) ([]by
|
||||
cleanup := passLongArgsInResponseFiles(cmd)
|
||||
defer cleanup()
|
||||
cmd.Dir = dir
|
||||
cmd.Env = base.MergeEnvLists(env, base.EnvForDir(cmd.Dir, os.Environ()))
|
||||
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
|
||||
cmd.Env = append(cmd.Env, env...)
|
||||
err := cmd.Run()
|
||||
|
||||
// err can be something like 'exit status 1'.
|
||||
@ -2327,7 +2328,8 @@ func (b *Builder) gccSupportsFlag(compiler []string, flag string) bool {
|
||||
}
|
||||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
||||
cmd.Dir = b.WorkDir
|
||||
cmd.Env = base.MergeEnvLists([]string{"LC_ALL=C"}, base.EnvForDir(cmd.Dir, os.Environ()))
|
||||
cmd.Env = base.EnvForDir(cmd.Dir, os.Environ())
|
||||
cmd.Env = append(cmd.Env, "LC_ALL=C")
|
||||
out, _ := cmd.CombinedOutput()
|
||||
// GCC says "unrecognized command line option".
|
||||
// clang says "unknown argument".
|
||||
|
Loading…
Reference in New Issue
Block a user