Skip to content

Commit

Permalink
Promote path
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Oct 18, 2024
1 parent aa24624 commit 1fe5107
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
42 changes: 1 addition & 41 deletions internal/runners/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) {
}
}

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

// sortPaths the env so our PATH environment variable is interpreted first
func sortPaths(env []string) []string {
if runtime.GOOS != "windows" {
return env
}

const (
windowsPathPrefix = "Path="
unixPathPrefix = "PATH="
)

var windowsPathIndex, unixPathIndex = -1, -1
for i, e := range env {
switch {
case strings.HasPrefix(e, windowsPathPrefix):
windowsPathIndex = i
case strings.HasPrefix(e, unixPathPrefix):
unixPathIndex = i
}
if windowsPathIndex != -1 && unixPathIndex != -1 {
break
}
}

if windowsPathIndex == -1 || unixPathIndex == -1 {
return env
}

// Ensure Unix PATH is after Windows PATH
if windowsPathIndex > unixPathIndex {
env[windowsPathIndex], env[unixPathIndex] = env[unixPathIndex], env[windowsPathIndex]
}

for _, e := range env {
fmt.Println(e)
}

return env
}

func projectFromRuntimeDir(cfg projectfile.ConfigGetter, runtimeDir string) string {
projects := projectfile.GetProjectMapping(cfg)
for _, paths := range projects {
Expand Down
24 changes: 24 additions & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"maps"
"os"
"path/filepath"
"runtime"

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

promotePath(vars)
promotePath(execVars)

return vars, execVars, nil
}

func promotePath(env map[string]string) {
if runtime.GOOS != "windows" {
return
}

PATH, exists := env["PATH"]
if !exists {
return
}

// If Path exists, prepend PATH values to it
Path, pathExists := env["Path"]
if !pathExists {
return
}

env["Path"] = PATH + string(os.PathListSeparator) + Path
delete(env, "PATH")
}

func (r *Runtime) Env(inherit bool) Environment {
if inherit {
return r.envInherit
Expand Down

0 comments on commit 1fe5107

Please sign in to comment.