File size: 6,689 Bytes
84aa3bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { UpdateObject } from '../ui/utils/updateCheck.js';
import type { LoadedSettings } from '../config/settings.js';
import { getInstallationInfo, PackageManager } from './installationInfo.js';
import { updateEventEmitter } from './updateEventEmitter.js';
import { MessageType, type HistoryItem } from '../ui/types.js';
import { spawnWrapper } from './spawnWrapper.js';
import type { spawn } from 'node:child_process';
import {
debugLogger,
getChannelFromVersion,
RELEASE_CHANNEL_STABILITY,
} from '@google/gemini-cli-core';
let _updateInProgress = false;
/** @internal */
export function _setUpdateStateForTesting(value: boolean) {
_updateInProgress = value;
}
export function isUpdateInProgress() {
return _updateInProgress;
}
/**
* Returns a promise that resolves when the update process completes or times out.
*/
export async function waitForUpdateCompletion(
timeoutMs = 30000,
): Promise<void> {
if (!_updateInProgress) {
return;
}
debugLogger.log(
'\nGemini CLI is waiting for a background update to complete before restarting...',
);
return new Promise((resolve) => {
// Re-check the condition inside the promise executor to avoid a race condition.
// If the update finished between the initial check and now, resolve immediately.
if (!_updateInProgress) {
resolve();
return;
}
const timer = setTimeout(cleanup, timeoutMs);
function cleanup() {
clearTimeout(timer);
updateEventEmitter.off('update-success', cleanup);
updateEventEmitter.off('update-failed', cleanup);
resolve();
}
updateEventEmitter.once('update-success', cleanup);
updateEventEmitter.once('update-failed', cleanup);
});
}
export function handleAutoUpdate(
info: UpdateObject | null,
settings: LoadedSettings,
projectRoot: string,
isSandboxEnabled: boolean,
spawnFn: typeof spawn = spawnWrapper,
) {
if (!info) {
return;
}
if (isSandboxEnabled) {
updateEventEmitter.emit('update-info', {
message: `${info.message}\nAutomatic update is not available in sandbox mode.`,
});
return;
}
if (!settings.merged.general.enableAutoUpdateNotification) {
return;
}
const installationInfo = getInstallationInfo(
projectRoot,
settings.merged.general.enableAutoUpdate,
);
if (
[
PackageManager.NPX,
PackageManager.PNPX,
PackageManager.BUNX,
PackageManager.BINARY,
].includes(installationInfo.packageManager)
) {
return;
}
let combinedMessage = info.message;
if (installationInfo.updateMessage) {
combinedMessage += `\n${installationInfo.updateMessage}`;
}
if (
!installationInfo.updateCommand ||
!settings.merged.general.enableAutoUpdate
) {
updateEventEmitter.emit('update-received', {
...info,
message: combinedMessage,
isUpdating: false,
});
return;
}
updateEventEmitter.emit('update-received', {
...info,
message: combinedMessage,
isUpdating: true,
});
if (_updateInProgress) {
return;
}
const currentVersion = info.update.current;
if (!currentVersion) {
debugLogger.warn(
'Update check: current version is missing. Skipping automatic update for safety.',
);
return;
}
const currentChannel = getChannelFromVersion(currentVersion);
const targetChannel = getChannelFromVersion(info.update.latest);
// Defense-in-depth: prevent updates to a less stable channel
if (
RELEASE_CHANNEL_STABILITY[targetChannel] <
RELEASE_CHANNEL_STABILITY[currentChannel]
) {
return;
}
const isNightly = info.update.latest.includes('nightly');
const updateCommand = installationInfo.updateCommand.replace(
'@latest',
isNightly ? '@nightly' : `@${info.update.latest}`,
);
const updateProcess = spawnFn(updateCommand, {
stdio: 'ignore',
shell: true,
detached: true,
});
_updateInProgress = true;
// Un-reference the child process to allow the parent to exit independently.
updateProcess.unref();
updateProcess.on('close', (code) => {
_updateInProgress = false;
if (code === 0) {
updateEventEmitter.emit('update-success', {
message:
'Update successful! The new version will be used on your next run.',
});
} else {
updateEventEmitter.emit('update-failed', {
message: `Automatic update failed. Please try updating manually:\n\n${updateCommand}`,
});
}
});
updateProcess.on('error', (err) => {
_updateInProgress = false;
updateEventEmitter.emit('update-failed', {
message: `Automatic update failed. Please try updating manually. (error: ${err.message})\n\n${updateCommand}`,
});
});
return updateProcess;
}
export function setUpdateHandler(
addItem: (item: Omit<HistoryItem, 'id'>, timestamp: number) => void,
setUpdateInfo: (info: UpdateObject | null) => void,
) {
let successfullyInstalled = false;
const handleUpdateReceived = (info: UpdateObject) => {
setUpdateInfo(info);
const savedMessage = info.message;
setTimeout(() => {
if (!successfullyInstalled) {
addItem(
{
type: MessageType.INFO,
text: savedMessage,
},
Date.now(),
);
}
setUpdateInfo(null);
}, 60000);
};
const handleUpdateFailed = (data?: { message: string }) => {
setUpdateInfo(null);
addItem(
{
type: MessageType.ERROR,
text:
data?.message ||
`Automatic update failed. Please try updating manually`,
},
Date.now(),
);
};
const handleUpdateSuccess = () => {
successfullyInstalled = true;
setUpdateInfo(null);
addItem(
{
type: MessageType.INFO,
text: `Update successful! The new version will be used on your next run.`,
},
Date.now(),
);
};
const handleUpdateInfo = (data: { message: string }) => {
addItem(
{
type: MessageType.INFO,
text: data.message,
},
Date.now(),
);
};
updateEventEmitter.on('update-received', handleUpdateReceived);
updateEventEmitter.on('update-failed', handleUpdateFailed);
updateEventEmitter.on('update-success', handleUpdateSuccess);
updateEventEmitter.on('update-info', handleUpdateInfo);
return () => {
updateEventEmitter.off('update-received', handleUpdateReceived);
updateEventEmitter.off('update-failed', handleUpdateFailed);
updateEventEmitter.off('update-success', handleUpdateSuccess);
updateEventEmitter.off('update-info', handleUpdateInfo);
};
}
|