diff --git a/packages/cli/src/core/auth.test.ts b/packages/cli/src/core/auth.test.ts index f28e826f49..5db9cd5449 100644 --- a/packages/cli/src/core/auth.test.ts +++ b/packages/cli/src/core/auth.test.ts @@ -9,6 +9,7 @@ import { performInitialAuth } from './auth.js'; import { type Config, ValidationRequiredError, + ProjectIdRequiredError, AuthType, } from '@google/gemini-cli-core'; @@ -116,4 +117,22 @@ describe('auth', () => { AuthType.LOGIN_WITH_GOOGLE, ); }); + + it('should return ProjectIdRequiredError message without "Failed to login" prefix', async () => { + const projectIdError = new ProjectIdRequiredError(); + vi.mocked(mockConfig.refreshAuth).mockRejectedValue(projectIdError); + const result = await performInitialAuth( + mockConfig, + AuthType.LOGIN_WITH_GOOGLE, + ); + expect(result).toEqual({ + authError: + 'This account requires setting the GOOGLE_CLOUD_PROJECT or GOOGLE_CLOUD_PROJECT_ID env var. See https://goo.gle/gemini-cli-auth-docs#workspace-gca', + accountSuspensionInfo: null, + }); + expect(result.authError).not.toContain('Failed to login'); + expect(mockConfig.refreshAuth).toHaveBeenCalledWith( + AuthType.LOGIN_WITH_GOOGLE, + ); + }); }); diff --git a/packages/cli/src/core/auth.ts b/packages/cli/src/core/auth.ts index f49fdecf76..f0b8015013 100644 --- a/packages/cli/src/core/auth.ts +++ b/packages/cli/src/core/auth.ts @@ -10,6 +10,7 @@ import { getErrorMessage, ValidationRequiredError, isAccountSuspendedError, + ProjectIdRequiredError, } from '@google/gemini-cli-core'; import type { AccountSuspensionInfo } from '../ui/contexts/UIStateContext.js'; @@ -54,6 +55,14 @@ export async function performInitialAuth( }, }; } + if (e instanceof ProjectIdRequiredError) { + // OAuth succeeded but account setup requires project ID + // Show the error message directly without "Failed to login" prefix + return { + authError: getErrorMessage(e), + accountSuspensionInfo: null, + }; + } return { authError: `Failed to login. Message: ${getErrorMessage(e)}`, accountSuspensionInfo: null, diff --git a/packages/cli/src/ui/AppContainer.tsx b/packages/cli/src/ui/AppContainer.tsx index d656169c51..a51a12bf1d 100644 --- a/packages/cli/src/ui/AppContainer.tsx +++ b/packages/cli/src/ui/AppContainer.tsx @@ -80,6 +80,7 @@ import { type ConsentRequestPayload, type AgentsDiscoveredPayload, ChangeAuthRequestedError, + ProjectIdRequiredError, CoreToolCallStatus, generateSteeringAckMessage, buildUserSteeringHintPrompt, @@ -771,6 +772,12 @@ export const AppContainer = (props: AppContainerProps) => { if (e instanceof ChangeAuthRequestedError) { return; } + if (e instanceof ProjectIdRequiredError) { + // OAuth succeeded but account setup requires project ID + // Show the error message directly without "Failed to authenticate" prefix + onAuthError(getErrorMessage(e)); + return; + } onAuthError( `Failed to authenticate: ${e instanceof Error ? e.message : String(e)}`, ); diff --git a/packages/cli/src/ui/auth/useAuth.test.tsx b/packages/cli/src/ui/auth/useAuth.test.tsx index 36d9aeec4f..20a02ffb21 100644 --- a/packages/cli/src/ui/auth/useAuth.test.tsx +++ b/packages/cli/src/ui/auth/useAuth.test.tsx @@ -15,7 +15,11 @@ import { } from 'vitest'; import { renderHook } from '../../test-utils/render.js'; import { useAuthCommand, validateAuthMethodWithSettings } from './useAuth.js'; -import { AuthType, type Config } from '@google/gemini-cli-core'; +import { + AuthType, + type Config, + ProjectIdRequiredError, +} from '@google/gemini-cli-core'; import { AuthState } from '../types.js'; import type { LoadedSettings } from '../../config/settings.js'; import { waitFor } from '../../test-utils/async.js'; @@ -288,5 +292,21 @@ describe('useAuth', () => { expect(result.current.authState).toBe(AuthState.Updating); }); }); + + it('should handle ProjectIdRequiredError without "Failed to login" prefix', async () => { + const projectIdError = new ProjectIdRequiredError(); + (mockConfig.refreshAuth as Mock).mockRejectedValue(projectIdError); + const { result } = renderHook(() => + useAuthCommand(createSettings(AuthType.LOGIN_WITH_GOOGLE), mockConfig), + ); + + await waitFor(() => { + expect(result.current.authError).toBe( + 'This account requires setting the GOOGLE_CLOUD_PROJECT or GOOGLE_CLOUD_PROJECT_ID env var. See https://goo.gle/gemini-cli-auth-docs#workspace-gca', + ); + expect(result.current.authError).not.toContain('Failed to login'); + expect(result.current.authState).toBe(AuthState.Updating); + }); + }); }); }); diff --git a/packages/cli/src/ui/auth/useAuth.ts b/packages/cli/src/ui/auth/useAuth.ts index 3faec2d5a8..afd438bb00 100644 --- a/packages/cli/src/ui/auth/useAuth.ts +++ b/packages/cli/src/ui/auth/useAuth.ts @@ -12,6 +12,7 @@ import { loadApiKey, debugLogger, isAccountSuspendedError, + ProjectIdRequiredError, } from '@google/gemini-cli-core'; import { getErrorMessage } from '@google/gemini-cli-core'; import { AuthState } from '../types.js'; @@ -143,6 +144,10 @@ export const useAuthCommand = ( appealUrl: suspendedError.appealUrl, appealLinkText: suspendedError.appealLinkText, }); + } else if (e instanceof ProjectIdRequiredError) { + // OAuth succeeded but account setup requires project ID + // Show the error message directly without "Failed to login" prefix + onAuthError(getErrorMessage(e)); } else { onAuthError(`Failed to login. Message: ${getErrorMessage(e)}`); }