Skip to content

Commit e415a61

Browse files
vcalasanshHarness
authored andcommitted
feat: [CI-20263]: add safe check & fallback for lower powershell versions (#137)
* e5d7e9 Fix identation * 4d5e88 Fix * 3e4033 Default to using pwsh (old behavior) * a7b72b feat: add safe check & fallback for lower powershell versions
1 parent 6eba019 commit e415a61

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

main.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ func cleanupTempDir() {
114114
}
115115
}
116116

117+
// findPowerShell locates a valid PowerShell executable.
118+
// It prioritizes PowerShell Core (pwsh) over Windows PowerShell (powershell).
119+
func findPowerShell() string {
120+
// Define the candidates to look for
121+
candidates := []string{"pwsh", "powershell", "powershell.exe"}
122+
123+
for _, candidate := range candidates {
124+
if path, err := exec.LookPath(candidate); err == nil {
125+
slog.Debug("Found PowerShell executable", "path", path)
126+
return path
127+
}
128+
}
129+
slog.Warn("no suitable PowerShell executable found (checked: %v). Using pwsh as default", candidates)
130+
return "pwsh"
131+
}
132+
117133
func runGitClone() error {
118134
var err error
119135
// Create a unique temporary subdirectory (keep alive for script reuse in metrics)
@@ -137,11 +153,14 @@ func runGitClone() error {
137153

138154
switch runtime.GOOS {
139155
case "windows":
156+
// Find safe PowerShell executable
157+
psExe := findPowerShell()
140158
scriptPath := filepath.Join(globalTmpDir, "windows", "clone.ps1")
141159
script := fmt.Sprintf(
142160
"$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; %s",
143161
scriptPath)
144-
cmd := exec.CommandContext(ctx, "pwsh", "-Command", script)
162+
163+
cmd := exec.CommandContext(ctx, psExe, "-Command", script)
145164
return runCmds([]*exec.Cmd{cmd}, os.Environ(), workdir, os.Stdout, os.Stderr)
146165

147166
case "linux", "darwin":
@@ -314,9 +333,11 @@ func executeBuildToolScript(workdir string) error {
314333

315334
switch runtime.GOOS {
316335
case "windows":
336+
// Find safe PowerShell executable
337+
psExe := findPowerShell()
317338
// Execute PowerShell script from temp directory (no workspace pollution)
318339
scriptPath := filepath.Join(globalTmpDir, "windows", "get-buildtool-lang.ps1")
319-
cmd = exec.Command("pwsh", "-File", scriptPath, workdir)
340+
cmd = exec.Command(psExe, "-File", scriptPath, workdir)
320341

321342
case "linux", "darwin":
322343
// Execute shell script from temp directory (no workspace pollution)

0 commit comments

Comments
 (0)