Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 6 additions & 68 deletions ui/desktop/src/utils/winShims.ts
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';

/**
Expand All @@ -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) => {
Expand All @@ -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
Copy link

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.

Copy link
Collaborator Author

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).

// 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);
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
Loading