-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: remove setx calls to not permanently edit the windows shell PATH #5821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import os from 'node:os'; | ||
| import { spawn } from 'child_process'; | ||
| import log from './logger'; | ||
|
|
||
| /** | ||
|
|
@@ -21,8 +20,8 @@ export async function ensureWinShims(): Promise<void> { | |
| try { | ||
| await fs.promises.mkdir(tgtDir, { recursive: true }); | ||
|
|
||
| // Only copy the command-line tools, NOT goosed.exe (which should always be used locally) | ||
| const shims = ['uvx.exe', 'npx.cmd', 'install-node.cmd']; | ||
| // Copy command-line tools, NOT goosed.exe (which should always be used locally) | ||
| const shims = ['uvx.exe', 'uv.exe', 'npx.cmd', 'install-node.cmd']; | ||
|
|
||
| await Promise.all( | ||
| shims.map(async (shim) => { | ||
|
|
@@ -39,12 +38,12 @@ export async function ensureWinShims(): Promise<void> { | |
| }) | ||
| ); | ||
|
|
||
| // Prepend to PATH **for this process & all children**. | ||
| // Make sure our bin directory is at the VERY BEGINNING of PATH | ||
| // Prepend to PATH **for this process & all children only**. | ||
| // This does NOT modify the user's permanent system PATH. | ||
| const currentPath = process.env.PATH ?? ''; | ||
| if (!currentPath.toLowerCase().includes(tgtDir.toLowerCase())) { | ||
| process.env.PATH = `${tgtDir}${path.delimiter}${currentPath}`; | ||
| log.info(`Added ${tgtDir} to PATH for current process`); | ||
| log.info(`Added ${tgtDir} to PATH for Goose processes only`); | ||
| } else { | ||
| // If it's already in PATH, make sure it's at the beginning | ||
| const pathParts = currentPath.split(path.delimiter); | ||
|
|
@@ -54,71 +53,10 @@ export async function ensureWinShims(): Promise<void> { | |
| // Remove it from its current position and add to beginning | ||
| pathParts.splice(binDirIndex, 1); | ||
| process.env.PATH = `${tgtDir}${path.delimiter}${pathParts.join(path.delimiter)}`; | ||
| log.info(`Moved ${tgtDir} to beginning of PATH for current process`); | ||
| log.info(`Moved ${tgtDir} to beginning of PATH for Goose processes only`); | ||
| } | ||
| } | ||
|
|
||
| // Optional: Persist PATH for user's external PowerShell/CMD sessions | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "Optional" here says it all: this was probably inserted by llm and barely glanced at. We should definitely not be adding these to the user's PATH. |
||
| await persistPathForUser(tgtDir); | ||
| } catch (error) { | ||
| log.error('Failed to ensure Windows shims:', error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Persist the Goose bin directory to the user's PATH environment variable. | ||
| * Uses only user PATH to avoid overwriting with system PATH and respects setx 1024 char limit. | ||
| */ | ||
| async function persistPathForUser(binDir: string): Promise<void> { | ||
| try { | ||
| const psScript = ` | ||
| $bin = "${binDir.replace(/\\/g, '\\\\')}" | ||
|
|
||
| $userPath = [System.Environment]::GetEnvironmentVariable("Path", "User") | ||
| if ($userPath -eq $null) { $userPath = "" } | ||
|
|
||
| $pathParts = $userPath -split ';' | Where-Object { $_.Trim() -ne "" } | ||
| $binExists = $pathParts | Where-Object { $_ -ieq $bin } | ||
|
|
||
| if (-not $binExists) { | ||
| $newUserPath = if ($userPath -eq "") { $bin } else { "$bin;$userPath" } | ||
|
|
||
| if ($newUserPath.Length -gt 1024) { | ||
| 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 | ||
| } | ||
|
|
||
| setx PATH $newUserPath >$null | ||
| Write-Host "Added Goose bin directory to beginning of user PATH" | ||
| Write-Host "New user PATH length: $($newUserPath.Length) chars" | ||
| } else { | ||
| if ($pathParts[0] -ieq $bin) { | ||
| 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" | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| spawn('powershell', ['-NoProfile', '-NonInteractive', '-Command', psScript], { | ||
| windowsHide: true, | ||
| shell: false, | ||
| }); | ||
|
|
||
| log.info('Attempted to persist Goose bin directory to user PATH'); | ||
| } catch (error) { | ||
| log.warn('Failed to persist PATH for user (non-critical):', error); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment explains it - it injects at the beginning of PATH and as such is the first valid command, as PATH search path is executed from left to right. If it would just append at the end it wouldn't make such problem, but not changing PATH environment variable w/o user consent would be even greater.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, thanks for bringing it up @klonuo (and goose made it clear it was bad, and even when I switched models a few times, as I didn't believe that it would do that!). Its been decades since I was a windows dev and the registry gives me chills (not the good kind).