Skip to content

Commit aa24624

Browse files
committed
Only swap path environment variables
1 parent 8bde15e commit aa24624

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

internal/runners/exec/exec.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os/exec"
77
"path/filepath"
88
"runtime"
9-
"sort"
109
"strconv"
1110
"strings"
1211

@@ -185,12 +184,7 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) {
185184
}
186185
}
187186

188-
// Sort the env so our PATH environment variable is interpreted first
189-
envs := osutils.EnvMapToSlice(env)
190-
sort.Slice(envs, func(i, j int) bool {
191-
return envs[i] > envs[j]
192-
})
193-
_, _, err = osutils.ExecuteAndPipeStd(exeTarget, args[1:], envs)
187+
_, _, err = osutils.ExecuteAndPipeStd(exeTarget, args[1:], sortPaths(osutils.EnvMapToSlice(env)))
194188
if eerr, ok := err.(*exec.ExitError); ok {
195189
return errs.Silence(errs.WrapExitCode(eerr, eerr.ExitCode()))
196190
}
@@ -201,6 +195,46 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) {
201195
return nil
202196
}
203197

198+
// sortPaths the env so our PATH environment variable is interpreted first
199+
func sortPaths(env []string) []string {
200+
if runtime.GOOS != "windows" {
201+
return env
202+
}
203+
204+
const (
205+
windowsPathPrefix = "Path="
206+
unixPathPrefix = "PATH="
207+
)
208+
209+
var windowsPathIndex, unixPathIndex = -1, -1
210+
for i, e := range env {
211+
switch {
212+
case strings.HasPrefix(e, windowsPathPrefix):
213+
windowsPathIndex = i
214+
case strings.HasPrefix(e, unixPathPrefix):
215+
unixPathIndex = i
216+
}
217+
if windowsPathIndex != -1 && unixPathIndex != -1 {
218+
break
219+
}
220+
}
221+
222+
if windowsPathIndex == -1 || unixPathIndex == -1 {
223+
return env
224+
}
225+
226+
// Ensure Unix PATH is after Windows PATH
227+
if windowsPathIndex > unixPathIndex {
228+
env[windowsPathIndex], env[unixPathIndex] = env[unixPathIndex], env[windowsPathIndex]
229+
}
230+
231+
for _, e := range env {
232+
fmt.Println(e)
233+
}
234+
235+
return env
236+
}
237+
204238
func projectFromRuntimeDir(cfg projectfile.ConfigGetter, runtimeDir string) string {
205239
projects := projectfile.GetProjectMapping(cfg)
206240
for _, paths := range projects {

0 commit comments

Comments
 (0)