Skip to content

Commit 1fe5107

Browse files
committed
Promote path
1 parent aa24624 commit 1fe5107

File tree

2 files changed

+25
-41
lines changed

2 files changed

+25
-41
lines changed

internal/runners/exec/exec.go

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) {
184184
}
185185
}
186186

187-
_, _, err = osutils.ExecuteAndPipeStd(exeTarget, args[1:], sortPaths(osutils.EnvMapToSlice(env)))
187+
_, _, err = osutils.ExecuteAndPipeStd(exeTarget, args[1:], osutils.EnvMapToSlice(env))
188188
if eerr, ok := err.(*exec.ExitError); ok {
189189
return errs.Silence(errs.WrapExitCode(eerr, eerr.ExitCode()))
190190
}
@@ -195,46 +195,6 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) {
195195
return nil
196196
}
197197

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-
238198
func projectFromRuntimeDir(cfg projectfile.ConfigGetter, runtimeDir string) string {
239199
projects := projectfile.GetProjectMapping(cfg)
240200
for _, paths := range projects {

pkg/runtime/runtime.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"maps"
55
"os"
66
"path/filepath"
7+
"runtime"
78

89
"github.com/ActiveState/cli/internal/errs"
910
"github.com/ActiveState/cli/internal/fileutils"
@@ -165,9 +166,32 @@ func (r *Runtime) getEnv(inherit bool) (map[string]string, map[string]string, er
165166
execVars["PATH"] += string(os.PathListSeparator) + vars["PATH"]
166167
}
167168

169+
promotePath(vars)
170+
promotePath(execVars)
171+
168172
return vars, execVars, nil
169173
}
170174

175+
func promotePath(env map[string]string) {
176+
if runtime.GOOS != "windows" {
177+
return
178+
}
179+
180+
PATH, exists := env["PATH"]
181+
if !exists {
182+
return
183+
}
184+
185+
// If Path exists, prepend PATH values to it
186+
Path, pathExists := env["Path"]
187+
if !pathExists {
188+
return
189+
}
190+
191+
env["Path"] = PATH + string(os.PathListSeparator) + Path
192+
delete(env, "PATH")
193+
}
194+
171195
func (r *Runtime) Env(inherit bool) Environment {
172196
if inherit {
173197
return r.envInherit

0 commit comments

Comments
 (0)