feat(ide): fallback to GEMINI_CLI_IDE_AUTH_TOKEN env var (#14843)

This commit is contained in:
Shreya Keshive
2025-12-09 14:41:32 -05:00
committed by GitHub
parent 364b12e2fa
commit 3f5f030d01
2 changed files with 35 additions and 3 deletions

View File

@@ -59,6 +59,7 @@ describe('IdeClient', () => {
delete process.env['GEMINI_CLI_IDE_SERVER_PORT'];
delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_COMMAND'];
delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_ARGS'];
delete process.env['GEMINI_CLI_IDE_AUTH_TOKEN'];
// Mock dependencies
vi.spyOn(process, 'cwd').mockReturnValue('/test/workspace/sub-dir');
@@ -923,5 +924,35 @@ describe('IdeClient', () => {
IDEConnectionStatus.Connected,
);
});
it('should connect with an auth token from environment variable if config file is missing', async () => {
vi.mocked(fs.promises.readFile).mockRejectedValue(
new Error('File not found'),
);
(
vi.mocked(fs.promises.readdir) as Mock<
(path: fs.PathLike) => Promise<string[]>
>
).mockResolvedValue([]);
process.env['GEMINI_CLI_IDE_SERVER_PORT'] = '9090';
process.env['GEMINI_CLI_IDE_AUTH_TOKEN'] = 'env-auth-token';
const ideClient = await IdeClient.getInstance();
await ideClient.connect();
expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
new URL('http://127.0.0.1:9090/mcp'),
expect.objectContaining({
requestInit: {
headers: {
Authorization: 'Bearer env-auth-token',
},
},
}),
);
expect(ideClient.getConnectionStatus().status).toBe(
IDEConnectionStatus.Connected,
);
});
});
});

View File

@@ -151,9 +151,10 @@ export class IdeClient {
this.setState(IDEConnectionStatus.Connecting);
this.connectionConfig = await this.getConnectionConfigFromFile();
if (this.connectionConfig?.authToken) {
this.authToken = this.connectionConfig.authToken;
}
this.authToken =
this.connectionConfig?.authToken ??
process.env['GEMINI_CLI_IDE_AUTH_TOKEN'];
const workspacePath =
this.connectionConfig?.workspacePath ??
process.env['GEMINI_CLI_IDE_WORKSPACE_PATH'];