mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-04 00:44:05 -07:00
fix(settings/env): Ensure that loadEnvironment is always called with settings. (#7313)
This commit is contained in:
@@ -10,18 +10,18 @@ import { validateAuthMethod } from './auth.js';
|
|||||||
|
|
||||||
vi.mock('./settings.js', () => ({
|
vi.mock('./settings.js', () => ({
|
||||||
loadEnvironment: vi.fn(),
|
loadEnvironment: vi.fn(),
|
||||||
|
loadSettings: vi.fn().mockReturnValue({
|
||||||
|
merged: vi.fn().mockReturnValue({}),
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('validateAuthMethod', () => {
|
describe('validateAuthMethod', () => {
|
||||||
const originalEnv = process.env;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
process.env = {};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
process.env = originalEnv;
|
vi.unstubAllEnvs();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return null for LOGIN_WITH_GOOGLE', () => {
|
it('should return null for LOGIN_WITH_GOOGLE', () => {
|
||||||
@@ -34,7 +34,7 @@ describe('validateAuthMethod', () => {
|
|||||||
|
|
||||||
describe('USE_GEMINI', () => {
|
describe('USE_GEMINI', () => {
|
||||||
it('should return null if GEMINI_API_KEY is set', () => {
|
it('should return null if GEMINI_API_KEY is set', () => {
|
||||||
process.env['GEMINI_API_KEY'] = 'test-key';
|
vi.stubEnv('GEMINI_API_KEY', 'test-key');
|
||||||
expect(validateAuthMethod(AuthType.USE_GEMINI)).toBeNull();
|
expect(validateAuthMethod(AuthType.USE_GEMINI)).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -47,13 +47,13 @@ describe('validateAuthMethod', () => {
|
|||||||
|
|
||||||
describe('USE_VERTEX_AI', () => {
|
describe('USE_VERTEX_AI', () => {
|
||||||
it('should return null if GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION are set', () => {
|
it('should return null if GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION are set', () => {
|
||||||
process.env['GOOGLE_CLOUD_PROJECT'] = 'test-project';
|
vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'test-project');
|
||||||
process.env['GOOGLE_CLOUD_LOCATION'] = 'test-location';
|
vi.stubEnv('GOOGLE_CLOUD_LOCATION', 'test-location');
|
||||||
expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull();
|
expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return null if GOOGLE_API_KEY is set', () => {
|
it('should return null if GOOGLE_API_KEY is set', () => {
|
||||||
process.env['GOOGLE_API_KEY'] = 'test-api-key';
|
vi.stubEnv('GOOGLE_API_KEY', 'test-api-key');
|
||||||
expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull();
|
expect(validateAuthMethod(AuthType.USE_VERTEX_AI)).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { AuthType } from '@google/gemini-cli-core';
|
import { AuthType } from '@google/gemini-cli-core';
|
||||||
import { loadEnvironment } from './settings.js';
|
import { loadEnvironment, loadSettings } from './settings.js';
|
||||||
|
|
||||||
export const validateAuthMethod = (authMethod: string): string | null => {
|
export function validateAuthMethod(authMethod: string): string | null {
|
||||||
loadEnvironment();
|
loadEnvironment(loadSettings(process.cwd()).merged);
|
||||||
if (
|
if (
|
||||||
authMethod === AuthType.LOGIN_WITH_GOOGLE ||
|
authMethod === AuthType.LOGIN_WITH_GOOGLE ||
|
||||||
authMethod === AuthType.CLOUD_SHELL
|
authMethod === AuthType.CLOUD_SHELL
|
||||||
@@ -40,4 +40,4 @@ export const validateAuthMethod = (authMethod: string): string | null => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 'Invalid auth method selected.';
|
return 'Invalid auth method selected.';
|
||||||
};
|
}
|
||||||
|
|||||||
@@ -553,7 +553,7 @@ export function setUpCloudShellEnvironment(envFilePath: string | null): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadEnvironment(settings?: Settings): void {
|
export function loadEnvironment(settings: Settings): void {
|
||||||
const envFilePath = findEnvFile(process.cwd());
|
const envFilePath = findEnvFile(process.cwd());
|
||||||
|
|
||||||
// Cloud Shell environment variable handling
|
// Cloud Shell environment variable handling
|
||||||
@@ -561,28 +561,6 @@ export function loadEnvironment(settings?: Settings): void {
|
|||||||
setUpCloudShellEnvironment(envFilePath);
|
setUpCloudShellEnvironment(envFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no settings provided, try to load workspace settings for exclusions
|
|
||||||
let resolvedSettings = settings;
|
|
||||||
if (!resolvedSettings) {
|
|
||||||
const workspaceSettingsPath = new Storage(
|
|
||||||
process.cwd(),
|
|
||||||
).getWorkspaceSettingsPath();
|
|
||||||
try {
|
|
||||||
if (fs.existsSync(workspaceSettingsPath)) {
|
|
||||||
const workspaceContent = fs.readFileSync(
|
|
||||||
workspaceSettingsPath,
|
|
||||||
'utf-8',
|
|
||||||
);
|
|
||||||
const parsedWorkspaceSettings = JSON.parse(
|
|
||||||
stripJsonComments(workspaceContent),
|
|
||||||
) as Settings;
|
|
||||||
resolvedSettings = resolveEnvVarsInObject(parsedWorkspaceSettings);
|
|
||||||
}
|
|
||||||
} catch (_e) {
|
|
||||||
// Ignore errors loading workspace settings
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (envFilePath) {
|
if (envFilePath) {
|
||||||
// Manually parse and load environment variables to handle exclusions correctly.
|
// Manually parse and load environment variables to handle exclusions correctly.
|
||||||
// This avoids modifying environment variables that were already set from the shell.
|
// This avoids modifying environment variables that were already set from the shell.
|
||||||
@@ -591,8 +569,7 @@ export function loadEnvironment(settings?: Settings): void {
|
|||||||
const parsedEnv = dotenv.parse(envFileContent);
|
const parsedEnv = dotenv.parse(envFileContent);
|
||||||
|
|
||||||
const excludedVars =
|
const excludedVars =
|
||||||
resolvedSettings?.advanced?.excludedEnvVars ||
|
settings?.advanced?.excludedEnvVars || DEFAULT_EXCLUDED_ENV_VARS;
|
||||||
DEFAULT_EXCLUDED_ENV_VARS;
|
|
||||||
const isProjectEnvFile = !envFilePath.includes(GEMINI_DIR);
|
const isProjectEnvFile = !envFilePath.includes(GEMINI_DIR);
|
||||||
|
|
||||||
for (const key in parsedEnv) {
|
for (const key in parsedEnv) {
|
||||||
|
|||||||
Reference in New Issue
Block a user