]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/base: remove MergeEnvLists
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 3 Mar 2019 17:24:57 +0000 (17:24 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 13 Mar 2019 13:51:03 +0000 (13:51 +0000)
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>
src/cmd/go/internal/base/env.go
src/cmd/go/internal/generate/generate.go
src/cmd/go/internal/tool/tool.go
src/cmd/go/internal/work/exec.go

index fcade9d84e228400547fe3d9532b42ad8b655d32..077295e0effcee5656192c2a39a0f4824be49f0b 100644 (file)
@@ -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)
 }
index 124dbc05f5d8f3fa6eee4ce6e7ee0b40ce9fce8f..23e2ecc224e5b30eb0b054ee89312f503f763c01 100644 (file)
@@ -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)
index edcf93513d89e341374baf6db395036bd362750d..930eecb63f1c4955c842e6e7d10a936e77783254 100644 (file)
@@ -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 {
index e53ef6cdd35725ea29decdf17fd99937571ec2e8..3310beb709ab22abee05b4ba134195f7e16b02e3 100644 (file)
@@ -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".