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}`))
);