fix(cli): respect .env override for GOOGLE_CLOUD_PROJECT (#26288)

This commit is contained in:
David Pierce
2026-05-01 16:49:45 +00:00
committed by GitHub
parent 8943640a71
commit 9cb48020e1
2 changed files with 37 additions and 11 deletions
+26
View File
@@ -3294,6 +3294,32 @@ MALICIOUS_VAR=allowed-because-trusted
expect(process.env['GOOGLE_CLOUD_PROJECT']).toBe('my-vertex-project');
});
it('should respect .env override for GOOGLE_CLOUD_PROJECT in Cloud Shell when auth type is vertex-ai', () => {
vi.stubEnv('CLOUD_SHELL', 'true');
vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'my-vertex-project');
process.argv = ['node', 'gemini', '-s', 'prompt'];
vi.mocked(isWorkspaceTrusted).mockReturnValue({
isTrusted: true,
source: 'file',
});
// Mock .env file to override the shell project
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(
'GOOGLE_CLOUD_PROJECT=env-vertex-project',
);
loadEnvironment(
createMockSettings({
tools: { sandbox: false },
security: { auth: { selectedType: AuthType.USE_VERTEX_AI } },
}).merged,
MOCK_WORKSPACE_DIR,
);
expect(process.env['GOOGLE_CLOUD_PROJECT']).toBe('env-vertex-project');
});
it('should clear cloudshell-gca when switching to Vertex AI without an original project', () => {
process.env['CLOUD_SHELL'] = 'true';
process.argv = ['node', 'gemini', '-s', 'prompt'];
+11 -11
View File
@@ -553,15 +553,6 @@ export function setUpCloudShellEnvironment(
// However, if the user has explicitly selected Vertex AI auth, they intend
// to use their own GCP project, so we restore the original value and skip
// the Cloud Shell override to respect their .env settings.
if (selectedAuthType === AuthType.USE_VERTEX_AI) {
const saved = process.env[USER_GCP_PROJECT];
if (saved !== undefined) {
process.env['GOOGLE_CLOUD_PROJECT'] = saved;
} else if (process.env['GOOGLE_CLOUD_PROJECT'] === 'cloudshell-gca') {
delete process.env['GOOGLE_CLOUD_PROJECT'];
}
return;
}
// Save the user's original value before overwriting, so it can be restored
// if the user later switches to Vertex AI (even after a process restart).
@@ -572,7 +563,11 @@ export function setUpCloudShellEnvironment(
}
}
let value = 'cloudshell-gca';
let value: string | undefined = 'cloudshell-gca';
if (selectedAuthType === AuthType.USE_VERTEX_AI) {
value = process.env[USER_GCP_PROJECT];
}
if (envFilePath && fs.existsSync(envFilePath)) {
const envFileContent = fs.readFileSync(envFilePath);
@@ -585,7 +580,12 @@ export function setUpCloudShellEnvironment(
}
}
}
process.env['GOOGLE_CLOUD_PROJECT'] = value;
if (value !== undefined) {
process.env['GOOGLE_CLOUD_PROJECT'] = value;
} else if (process.env['GOOGLE_CLOUD_PROJECT'] === 'cloudshell-gca') {
delete process.env['GOOGLE_CLOUD_PROJECT'];
}
}
export function loadEnvironment(