diff --git a/src/cmd/dist/exec.go b/src/cmd/dist/exec.go index 67305530ae83f..43f503cb6c8b1 100644 --- a/src/cmd/dist/exec.go +++ b/src/cmd/dist/exec.go @@ -17,23 +17,11 @@ func setDir(cmd *exec.Cmd, dir string) { } // setEnv sets cmd.Env so that key = value. -// -// It first removes any existing values for key, so it is safe to call -// even from within cmdbootstrap. func setEnv(cmd *exec.Cmd, key, value string) { kv := key + "=" + value if cmd.Env == nil { cmd.Env = os.Environ() } - - prefix := kv[:len(key)+1] - for i, entry := range cmd.Env { - if strings.HasPrefix(entry, prefix) { - cmd.Env[i] = kv - return - } - } - cmd.Env = append(cmd.Env, kv) } @@ -44,10 +32,13 @@ func unsetEnv(cmd *exec.Cmd, key string) { } prefix := key + "=" - for i, entry := range cmd.Env { + newEnv := []string{} + for _, entry := range cmd.Env { if strings.HasPrefix(entry, prefix) { - cmd.Env = append(cmd.Env[:i], cmd.Env[i+1:]...) - return + continue } + newEnv = append(newEnv, entry) + // key may appear multiple times, so keep going. } + cmd.Env = newEnv }