fix(coding-agent): fix Windows self-update

Fixes #3857
This commit is contained in:
Armin Ronacher
2026-04-28 10:03:00 +02:00
parent 946bee1946
commit 9848b3145a
3 changed files with 45 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
import { spawnSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import { existsSync, readFileSync, realpathSync } from "fs";
import { homedir } from "os";
import { dirname, join, resolve, sep } from "path";
import { fileURLToPath } from "url";
@@ -94,6 +94,9 @@ function readCommandOutput(command: string, args: string[]): string | undefined
encoding: "utf-8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 2000,
// Windows package managers are commonly .cmd shims. Use the shell so Node can execute them;
// command and args are fixed literals from getGlobalPackageRoots(), not user input.
shell: process.platform === "win32",
});
if (result.status !== 0) return undefined;
const stdout = result.stdout.trim();
@@ -128,18 +131,32 @@ function getGlobalPackageRoots(method: InstallMethod): string[] {
}
}
function isManagedByGlobalPackageManager(method: InstallMethod): boolean {
let packageDir = resolve(getPackageDir());
function normalizeExistingPathForComparison(path: string): string | undefined {
const resolvedPath = resolve(path);
if (!existsSync(resolvedPath)) {
return undefined;
}
let normalizedPath: string;
try {
normalizedPath = realpathSync(resolvedPath);
} catch {
return undefined;
}
if (process.platform === "win32") {
packageDir = packageDir.toLowerCase();
normalizedPath = normalizedPath.toLowerCase();
}
return normalizedPath;
}
function isManagedByGlobalPackageManager(method: InstallMethod): boolean {
const packageDir = normalizeExistingPathForComparison(getPackageDir());
if (!packageDir) {
return false;
}
return getGlobalPackageRoots(method).some((root) => {
let normalizedRoot = resolve(root);
if (process.platform === "win32") {
normalizedRoot = normalizedRoot.toLowerCase();
}
const normalizedRoot = normalizeExistingPathForComparison(root);
return (
existsSync(normalizedRoot) &&
normalizedRoot !== undefined &&
(packageDir === normalizedRoot ||
packageDir.startsWith(normalizedRoot.endsWith(sep) ? normalizedRoot : `${normalizedRoot}${sep}`))
);

View File

@@ -271,6 +271,12 @@ function printSelfUpdateUnavailable(): void {
}
}
function printSelfUpdateFallback(): void {
const command = getSelfUpdateCommand(PACKAGE_NAME);
if (!command) return;
console.error(chalk.dim(`If this keeps failing, run this command yourself: ${command.display}`));
}
async function runSelfUpdate(): Promise<void> {
const command = getSelfUpdateCommand(PACKAGE_NAME);
if (!command) {
@@ -281,7 +287,9 @@ async function runSelfUpdate(): Promise<void> {
console.log(chalk.dim(`Updating ${APP_NAME} with ${command.display}...`));
await new Promise<void>((resolve, reject) => {
const child = spawn(command.command, command.args, { stdio: "inherit" });
// Windows package managers are commonly .cmd shims. Use the shell so Node can execute them;
// command and args come from getSelfUpdateCommandForMethod(), not user input.
const child = spawn(command.command, command.args, { stdio: "inherit", shell: process.platform === "win32" });
child.on("error", (error) => {
reject(error);
});
@@ -451,7 +459,15 @@ export async function handlePackageCommand(args: string[]): Promise<boolean> {
}
if (updateTargetIncludesSelf(target)) {
if (canSelfUpdate()) {
await runSelfUpdate();
try {
await runSelfUpdate();
} catch (error: unknown) {
const message = error instanceof Error ? error.message : "Unknown package command error";
console.error(chalk.red(`Error: ${message}`));
printSelfUpdateFallback();
process.exitCode = 1;
return true;
}
console.log(chalk.green(`Updated ${APP_NAME}`));
} else {
printSelfUpdateUnavailable();