Skip to content

Commit fbb9290

Browse files
committed
Don't overwrite env vars
1 parent 4744211 commit fbb9290

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎go/extractor/registries/registryproxy.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func applyProxyEnvVars(cmd *exec.Cmd) {
238238
// variables to the existing environment to preserve those environment variables.
239239
// If `cmd.Env` is not changed, then the existing environment is also preserved.
240240
if proxy_vars != nil {
241-
cmd.Env = append(os.Environ(), proxy_vars...)
241+
cmd.Env = append(cmd.Environ(), proxy_vars...)
242242
}
243243

244244
slog.Debug(
@@ -315,7 +315,7 @@ func (self ProxiedCmd) CombinedOutput() ([]byte, error) {
315315
}
316316

317317
func (self ProxiedCmd) AppendEnv(kvps ...string) {
318-
self.cmd.Env = append(self.cmd.Env, kvps...)
318+
self.cmd.Env = append(self.cmd.Environ(), kvps...)
319319
}
320320

321321
func (self ProxiedCmd) SetDir(workingDir string) {

‎go/extractor/registries/registryproxy_test.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package registries
22

33
import (
4+
"os/exec"
5+
"strings"
46
"testing"
57
)
68

@@ -131,3 +133,67 @@ func TestParseRegistryConfigsMultipleGoProxyServers(t *testing.T) {
131133
t.Fatalf("Expected `URL` to be `https://goproxy.io`, but got `%s`", second.URL)
132134
}
133135
}
136+
137+
func TestApplyProxyEnvVarsPreservesCommandEnvironment(t *testing.T) {
138+
originalProxyVars := proxy_vars
139+
originalProxyVarsChecked := proxy_vars_checked
140+
defer func() {
141+
proxy_vars = originalProxyVars
142+
proxy_vars_checked = originalProxyVarsChecked
143+
}()
144+
145+
t.Setenv("CODEQL_REGISTRIES_TEST_INHERITED", "inherited")
146+
proxy_vars = []string{"GOPROXY=https://proxy.example.com"}
147+
proxy_vars_checked = true
148+
149+
cmd := Command("go", "version")
150+
cmd.AppendEnv("GOTOOLCHAIN=local")
151+
applyProxyEnvVars(cmd.cmd)
152+
153+
env := make(map[string]string)
154+
for _, entry := range cmd.cmd.Env {
155+
key, value, found := strings.Cut(entry, "=")
156+
if found {
157+
env[key] = value
158+
}
159+
}
160+
161+
expected := map[string]string{
162+
"CODEQL_REGISTRIES_TEST_INHERITED": "inherited",
163+
"GOTOOLCHAIN": "local",
164+
"GOPROXY": "https://proxy.example.com",
165+
}
166+
for key, value := range expected {
167+
if env[key] != value {
168+
t.Errorf("Expected %s=%s, got %q", key, value, env[key])
169+
}
170+
}
171+
}
172+
173+
func TestApplyProxyEnvVarsPreservesInheritedEnvironmentWithoutOverrides(t *testing.T) {
174+
originalProxyVars := proxy_vars
175+
originalProxyVarsChecked := proxy_vars_checked
176+
defer func() {
177+
proxy_vars = originalProxyVars
178+
proxy_vars_checked = originalProxyVarsChecked
179+
}()
180+
181+
t.Setenv("CODEQL_REGISTRIES_TEST_INHERITED", "inherited")
182+
proxy_vars = []string{"GOPROXY=https://proxy.example.com"}
183+
proxy_vars_checked = true
184+
185+
cmd := exec.Command("go", "version")
186+
applyProxyEnvVars(cmd)
187+
188+
env := make(map[string]string)
189+
for _, entry := range cmd.Env {
190+
key, value, found := strings.Cut(entry, "=")
191+
if found {
192+
env[key] = value
193+
}
194+
}
195+
196+
if env["CODEQL_REGISTRIES_TEST_INHERITED"] != "inherited" {
197+
t.Errorf("Expected inherited environment variable to be preserved, got %q", env["CODEQL_REGISTRIES_TEST_INHERITED"])
198+
}
199+
}

0 commit comments

Comments
 (0)