From f11414a424fb6a440e74d0faabfa254ea75d82c7 Mon Sep 17 00:00:00 2001 From: Seth Troisi Date: Mon, 9 Jun 2025 13:54:11 -0700 Subject: [PATCH] Use GOOGLE_API_KEY as default if both GEMINI and GOOGLE set (#777) --- .eslintignore | 1 + packages/cli/src/config/config.test.ts | 50 ++++++++++++++++++++++++-- packages/cli/src/config/config.ts | 7 +++- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..51179a4d --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +packages/server/dist diff --git a/packages/cli/src/config/config.test.ts b/packages/cli/src/config/config.test.ts index 26964761..1859943b 100644 --- a/packages/cli/src/config/config.test.ts +++ b/packages/cli/src/config/config.test.ts @@ -36,8 +36,8 @@ vi.mock('@gemini-cli/core', async () => { loadEnvironment: vi.fn(), Config: vi.fn((params) => ({ // Mock the config object and its methods - getApiKey: () => params.apiKey, - getModel: () => params.model, + getApiKey: () => params.contentGeneratorConfig.apiKey, + getModel: () => params.contentGeneratorConfig.model, getSandbox: () => params.sandbox, getTargetDir: () => params.targetDir, getDebugMode: () => params.debugMode, @@ -51,7 +51,7 @@ vi.mock('@gemini-cli/core', async () => { getUserAgent: () => params.userAgent, getUserMemory: () => params.userMemory, getGeminiMdFileCount: () => params.geminiMdFileCount, - getVertexAI: () => params.vertexai, + getVertexAI: () => params.contentGeneratorConfig.vertexai, getShowMemoryUsage: () => params.showMemoryUsage, // Added for the test getTelemetry: () => params.telemetry, // Add any other methods that are called on the config object @@ -175,6 +175,50 @@ describe('loadCliConfig telemetry', () => { }); }); +describe('API Key Handling', () => { + const originalEnv = { ...process.env }; + const originalArgv = process.argv; + + beforeEach(() => { + vi.resetAllMocks(); + process.argv = ['node', 'script.js']; + }); + + afterEach(() => { + process.env = originalEnv; + process.argv = originalArgv; + }); + + it('should use GEMINI_API_KEY from env', async () => { + process.env.GEMINI_API_KEY = 'gemini-key'; + delete process.env.GOOGLE_API_KEY; + + const settings: Settings = {}; + const result = await loadCliConfig(settings, []); + expect(result.getApiKey()).toBe('gemini-key'); + }); + + it('should use GOOGLE_API_KEY and warn when both GOOGLE_API_KEY and GEMINI_API_KEY are set', async () => { + const consoleWarnSpy = vi + .spyOn(console, 'warn') + .mockImplementation(() => {}); + + process.env.GEMINI_API_KEY = 'gemini-key'; + process.env.GOOGLE_API_KEY = 'google-key'; + + const settings: Settings = {}; + const result = await loadCliConfig(settings, []); + + expect(consoleWarnSpy).toHaveBeenCalledWith( + '[WARN]', + 'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.', + ); + expect(result.getApiKey()).toBe('google-key'); + + consoleWarnSpy.mockRestore(); + }); +}); + describe('Hierarchical Memory Loading (config.ts) - Placeholder Suite', () => { beforeEach(() => { vi.resetAllMocks(); diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 1cff28f8..83cfb296 100644 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -188,6 +188,11 @@ async function createContentGeneratorConfig( const hasVertexProjectLocationConfig = !!googleCloudProject && !!googleCloudLocation; + if (hasGeminiApiKey && hasGoogleApiKey) { + logger.warn( + 'Both GEMINI_API_KEY and GOOGLE_API_KEY are set. Using GOOGLE_API_KEY.', + ); + } if (!hasGeminiApiKey && !hasGoogleApiKey && !hasVertexProjectLocationConfig) { logger.error( 'No valid API authentication configuration found. Please set ONE of the following combinations in your environment variables or .env file:\n' + @@ -203,7 +208,7 @@ async function createContentGeneratorConfig( const config: ContentGeneratorConfig = { model: argv.model || DEFAULT_GEMINI_MODEL, - apiKey: geminiApiKey || googleApiKey || '', + apiKey: googleApiKey || geminiApiKey || '', vertexai: hasGeminiApiKey ? false : undefined, };