Guard updater service refresh against missing invocation cwd (#45486)

* Update: capture a stable cwd for service refresh env

* Test: cover service refresh when cwd disappears
This commit is contained in:
Vincent Koc
2026-03-13 18:09:01 -04:00
committed by GitHub
parent fac754041c
commit 65f92fd839
2 changed files with 67 additions and 2 deletions

View File

@@ -124,9 +124,17 @@ function formatCommandFailure(stdout: string, stderr: string): string {
return detail.split("\n").slice(-3).join("\n");
}
function tryResolveInvocationCwd(): string | undefined {
try {
return process.cwd();
} catch {
return undefined;
}
}
function resolveServiceRefreshEnv(
env: NodeJS.ProcessEnv,
invocationCwd: string = process.cwd(),
invocationCwd?: string,
): NodeJS.ProcessEnv {
const resolvedEnv: NodeJS.ProcessEnv = { ...env };
for (const key of SERVICE_REFRESH_PATH_ENV_KEYS) {
@@ -138,6 +146,10 @@ function resolveServiceRefreshEnv(
resolvedEnv[key] = rawValue;
continue;
}
if (!invocationCwd) {
resolvedEnv[key] = rawValue;
continue;
}
resolvedEnv[key] = path.resolve(invocationCwd, rawValue);
}
return resolvedEnv;
@@ -205,6 +217,7 @@ function printDryRunPreview(preview: UpdateDryRunPreview, jsonMode: boolean): vo
async function refreshGatewayServiceEnv(params: {
result: UpdateRunResult;
jsonMode: boolean;
invocationCwd?: string;
}): Promise<void> {
const args = ["gateway", "install", "--force"];
if (params.jsonMode) {
@@ -217,7 +230,7 @@ async function refreshGatewayServiceEnv(params: {
}
const res = await runCommandWithTimeout([resolveNodeRunner(), candidate, ...args], {
cwd: params.result.root,
env: resolveServiceRefreshEnv(process.env),
env: resolveServiceRefreshEnv(process.env, params.invocationCwd),
timeoutMs: SERVICE_REFRESH_TIMEOUT_MS,
});
if (res.code === 0) {
@@ -547,6 +560,7 @@ async function maybeRestartService(params: {
refreshServiceEnv: boolean;
gatewayPort: number;
restartScriptPath?: string | null;
invocationCwd?: string;
}): Promise<void> {
if (params.shouldRestart) {
if (!params.opts.json) {
@@ -562,6 +576,7 @@ async function maybeRestartService(params: {
await refreshGatewayServiceEnv({
result: params.result,
jsonMode: Boolean(params.opts.json),
invocationCwd: params.invocationCwd,
});
} catch (err) {
if (!params.opts.json) {
@@ -667,6 +682,7 @@ async function maybeRestartService(params: {
export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
suppressDeprecations();
const invocationCwd = tryResolveInvocationCwd();
const timeoutMs = parseTimeoutMsOrExit(opts.timeout);
const shouldRestart = opts.restart !== false;
@@ -949,6 +965,7 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
refreshServiceEnv: refreshGatewayServiceEnv,
gatewayPort,
restartScriptPath,
invocationCwd,
});
if (!opts.json) {