fix Windows Env Vars (#3282)

This commit is contained in:
Max Novich
2025-07-07 12:51:57 -07:00
committed by GitHub
parent 36b517379b
commit e700bdbb3a
+34 -22
View File
@@ -66,36 +66,48 @@ export async function ensureWinShims(): Promise<void> {
} }
/** /**
* Optionally persist the Goose bin directory to the user's PATH environment variable * Persist the Goose bin directory to the user's PATH environment variable.
* This allows users to run uvx, npx, goosed from external PowerShell/CMD sessions * Uses only user PATH to avoid overwriting with system PATH and respects setx 1024 char limit.
*/ */
async function persistPathForUser(binDir: string): Promise<void> { async function persistPathForUser(binDir: string): Promise<void> {
try { try {
const psScript = ` const psScript = `
$bin = "${binDir.replace(/\\/g, '\\\\')}" $bin = "${binDir.replace(/\\/g, '\\\\')}"
if (-not ($Env:Path -split ';' | Where-Object { $_ -ieq $bin })) {
# Add to beginning of PATH for priority $userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
setx PATH "$bin;$Env:Path" >$null if ($userPath -eq $null) { $userPath = "" }
Write-Host "Added Goose bin directory to beginning of user PATH"
} else { $pathParts = $userPath -split ';' | Where-Object { $_.Trim() -ne "" }
# If already in PATH, ensure it's at the beginning $binExists = $pathParts | Where-Object { $_ -ieq $bin }
$pathParts = $Env:Path -split ';'
$binIndex = 0 if (-not $binExists) {
for ($i = 0; $i -lt $pathParts.Count; $i++) { $newUserPath = if ($userPath -eq "") { $bin } else { "$bin;$userPath" }
if ($pathParts[$i] -ieq $bin) {
$binIndex = $i if ($newUserPath.Length -gt 1024) {
break Write-Warning "Cannot add to PATH: would exceed 1024 character limit for setx ($($newUserPath.Length) chars)"
} Write-Host "Current user PATH length: $($userPath.Length) chars"
Write-Host "Consider using system PATH or cleaning up existing PATH entries"
return
} }
if ($binIndex -gt 0) { setx PATH $newUserPath >$null
# Remove from current position and add to beginning Write-Host "Added Goose bin directory to beginning of user PATH"
$pathParts = @($pathParts[$binIndex]) + @($pathParts | Where-Object { $_ -ine $bin }) Write-Host "New user PATH length: $($newUserPath.Length) chars"
$newPath = $pathParts -join ';' } else {
setx PATH $newPath >$null if ($pathParts[0] -ieq $bin) {
Write-Host "Moved Goose bin directory to beginning of user PATH"
} else {
Write-Host "Goose bin directory already at beginning of user PATH" Write-Host "Goose bin directory already at beginning of user PATH"
} else {
$filteredParts = $pathParts | Where-Object { $_ -ine $bin }
$newUserPath = @($bin) + $filteredParts -join ';'
if ($newUserPath.Length -gt 1024) {
Write-Warning "Cannot reorder PATH: would exceed 1024 character limit for setx ($($newUserPath.Length) chars)"
return
}
setx PATH $newUserPath >$null
Write-Host "Moved Goose bin directory to beginning of user PATH"
Write-Host "New user PATH length: $($newUserPath.Length) chars"
} }
} }
`; `;