PrependBinDirToEnv takes an env slice and ensures the tools bin directory is prepended to the PATH entry. This allows installed tools to find other installed tools (e.g., npx finding node).
(env []string)
| 38 | // is prepended to the PATH entry. This allows installed tools to find |
| 39 | // other installed tools (e.g., npx finding node). |
| 40 | func PrependBinDirToEnv(env []string) []string { |
| 41 | binDir := BinDir() |
| 42 | result := make([]string, 0, len(env)) |
| 43 | found := false |
| 44 | |
| 45 | for _, e := range env { |
| 46 | if existing, ok := strings.CutPrefix(e, "PATH="); ok { |
| 47 | result = append(result, "PATH="+binDir+string(os.PathListSeparator)+existing) |
| 48 | found = true |
| 49 | } else { |
| 50 | result = append(result, e) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if !found { |
| 55 | result = append(result, "PATH="+binDir) |
| 56 | } |
| 57 | |
| 58 | return result |
| 59 | } |