@@ -71,6 +71,8 @@ type Cmd struct {
71
71
// environment.
72
72
// If Env contains duplicate environment keys, only the last
73
73
// value in the slice for each duplicate key is used.
74
+ // As a special case on Windows, SYSTEMROOT is always added if
75
+ // missing and not explicitly set to the empty string.
74
76
Env []string
75
77
76
78
// Dir specifies the working directory of the command.
@@ -412,7 +414,7 @@ func (c *Cmd) Start() error {
412
414
c .Process , err = os .StartProcess (c .Path , c .argv (), & os.ProcAttr {
413
415
Dir : c .Dir ,
414
416
Files : c .childFiles ,
415
- Env : dedupEnv (c .envv ()),
417
+ Env : addCriticalEnv ( dedupEnv (c .envv () )),
416
418
Sys : c .SysProcAttr ,
417
419
})
418
420
if err != nil {
@@ -756,3 +758,24 @@ func dedupEnvCase(caseInsensitive bool, env []string) []string {
756
758
}
757
759
return out
758
760
}
761
+
762
+ // addCriticalEnv adds any critical environment variables that are required
763
+ // (or at least almost always required) on the operating system.
764
+ // Currently this is only used for Windows.
765
+ func addCriticalEnv (env []string ) []string {
766
+ if runtime .GOOS != "windows" {
767
+ return env
768
+ }
769
+ for _ , kv := range env {
770
+ eq := strings .Index (kv , "=" )
771
+ if eq < 0 {
772
+ continue
773
+ }
774
+ k := kv [:eq ]
775
+ if strings .EqualFold (k , "SYSTEMROOT" ) {
776
+ // We already have it.
777
+ return env
778
+ }
779
+ }
780
+ return append (env , "SYSTEMROOT=" + os .Getenv ("SYSTEMROOT" ))
781
+ }
0 commit comments