From 139cc7b97cb3d9b59d3533b53c1305b6720ce2d4 Mon Sep 17 00:00:00 2001 From: Sehoon Shon Date: Tue, 24 Mar 2026 11:58:41 -0400 Subject: [PATCH] perf(cli): optimize --version startup time (#23671) --- packages/cli/index.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/cli/index.ts b/packages/cli/index.ts index 5444fe1b74..fa6537d7bf 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -6,12 +6,19 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { main } from './src/gemini.js'; -import { FatalError, writeToStderr } from '@google/gemini-cli-core'; -import { runExitCleanup } from './src/utils/cleanup.js'; +// --- Fast Path for Version --- +// We check for version flags at the very top to avoid loading any heavy dependencies. +// process.env.CLI_VERSION is defined during the build process by esbuild. +if (process.argv.includes('--version') || process.argv.includes('-v')) { + console.log(process.env['CLI_VERSION'] || 'unknown'); + process.exit(0); +} // --- Global Entry Point --- +let writeToStderrFn: (message: string) => void = (msg) => + process.stderr.write(msg); + // Suppress known race condition error in node-pty on Windows // Tracking bug: https://github.com/microsoft/node-pty/issues/827 process.on('uncaughtException', (error) => { @@ -28,13 +35,22 @@ process.on('uncaughtException', (error) => { // For other errors, we rely on the default behavior, but since we attached a listener, // we must manually replicate it. if (error instanceof Error) { - writeToStderr(error.stack + '\n'); + writeToStderrFn(error.stack + '\n'); } else { - writeToStderr(String(error) + '\n'); + writeToStderrFn(String(error) + '\n'); } process.exit(1); }); +const [{ main }, { FatalError, writeToStderr }, { runExitCleanup }] = + await Promise.all([ + import('./src/gemini.js'), + import('@google/gemini-cli-core'), + import('./src/utils/cleanup.js'), + ]); + +writeToStderrFn = writeToStderr; + main().catch(async (error) => { // Set a timeout to force exit if cleanup hangs const cleanupTimeout = setTimeout(() => {