diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2019-04-29 16:36:21 +0000 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2019-04-29 19:08:13 +0000 |
commit | 06c9ccdfc7b9e39e0f609c00bddd1b39a0385a37 (patch) | |
tree | 378fb3f7fb58e300a834e1b956409592786a89f6 /src/os/exec/exec.go | |
parent | 8c1f78524e421ac01e35e8805dd7a45bf98c2a79 (diff) | |
download | go-06c9ccdfc7b9e39e0f609c00bddd1b39a0385a37.tar.gz go-06c9ccdfc7b9e39e0f609c00bddd1b39a0385a37.zip |
os/exec: always set SYSTEMROOT on Windows if not listed in Cmd.Env
Fixes #25210
Change-Id: If27b61776154dae9b9b67bf4e4f5faa785d98105
Reviewed-on: https://go-review.googlesource.com/c/go/+/174318
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/os/exec/exec.go')
-rw-r--r-- | src/os/exec/exec.go | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index d481cf7798..9a9265b667 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -71,6 +71,8 @@ type Cmd struct { // environment. // If Env contains duplicate environment keys, only the last // value in the slice for each duplicate key is used. + // As a special case on Windows, SYSTEMROOT is always added if + // missing and not explicitly set to the empty string. Env []string // Dir specifies the working directory of the command. @@ -412,7 +414,7 @@ func (c *Cmd) Start() error { c.Process, err = os.StartProcess(c.Path, c.argv(), &os.ProcAttr{ Dir: c.Dir, Files: c.childFiles, - Env: dedupEnv(c.envv()), + Env: addCriticalEnv(dedupEnv(c.envv())), Sys: c.SysProcAttr, }) if err != nil { @@ -756,3 +758,24 @@ func dedupEnvCase(caseInsensitive bool, env []string) []string { } return out } + +// addCriticalEnv adds any critical environment variables that are required +// (or at least almost always required) on the operating system. +// Currently this is only used for Windows. +func addCriticalEnv(env []string) []string { + if runtime.GOOS != "windows" { + return env + } + for _, kv := range env { + eq := strings.Index(kv, "=") + if eq < 0 { + continue + } + k := kv[:eq] + if strings.EqualFold(k, "SYSTEMROOT") { + // We already have it. + return env + } + } + return append(env, "SYSTEMROOT="+os.Getenv("SYSTEMROOT")) +} |