mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-17 17:41:24 -07:00
Introduce GEMINI_CLI_HOME for strict test isolation (#15907)
This commit is contained in:
@@ -5,12 +5,11 @@
|
||||
*/
|
||||
|
||||
import { exec, execSync, spawn, type ChildProcess } from 'node:child_process';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { quote, parse } from 'shell-quote';
|
||||
import { USER_SETTINGS_DIR } from '../config/settings.js';
|
||||
import { promisify } from 'node:util';
|
||||
import type { Config, SandboxConfig } from '@google/gemini-cli-core';
|
||||
import {
|
||||
@@ -18,6 +17,7 @@ import {
|
||||
debugLogger,
|
||||
FatalSandboxError,
|
||||
GEMINI_DIR,
|
||||
homedir,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { ConsolePatcher } from '../ui/utils/ConsolePatcher.js';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
@@ -82,7 +82,7 @@ export async function start_sandbox(
|
||||
'-D',
|
||||
`TMP_DIR=${fs.realpathSync(os.tmpdir())}`,
|
||||
'-D',
|
||||
`HOME_DIR=${fs.realpathSync(os.homedir())}`,
|
||||
`HOME_DIR=${fs.realpathSync(homedir())}`,
|
||||
'-D',
|
||||
`CACHE_DIR=${fs.realpathSync((await execAsync('getconf DARWIN_USER_CACHE_DIR')).stdout.trim())}`,
|
||||
];
|
||||
@@ -288,18 +288,23 @@ export async function start_sandbox(
|
||||
|
||||
// mount user settings directory inside container, after creating if missing
|
||||
// note user/home changes inside sandbox and we mount at BOTH paths for consistency
|
||||
const userSettingsDirOnHost = USER_SETTINGS_DIR;
|
||||
const userHomeDirOnHost = homedir();
|
||||
const userSettingsDirInSandbox = getContainerPath(
|
||||
`/home/node/${GEMINI_DIR}`,
|
||||
);
|
||||
if (!fs.existsSync(userSettingsDirOnHost)) {
|
||||
fs.mkdirSync(userSettingsDirOnHost);
|
||||
if (!fs.existsSync(userHomeDirOnHost)) {
|
||||
fs.mkdirSync(userHomeDirOnHost, { recursive: true });
|
||||
}
|
||||
const userSettingsDirOnHost = path.join(userHomeDirOnHost, GEMINI_DIR);
|
||||
if (!fs.existsSync(userSettingsDirOnHost)) {
|
||||
fs.mkdirSync(userSettingsDirOnHost, { recursive: true });
|
||||
}
|
||||
|
||||
args.push(
|
||||
'--volume',
|
||||
`${userSettingsDirOnHost}:${userSettingsDirInSandbox}`,
|
||||
);
|
||||
if (userSettingsDirInSandbox !== userSettingsDirOnHost) {
|
||||
if (userSettingsDirInSandbox !== getContainerPath(userSettingsDirOnHost)) {
|
||||
args.push(
|
||||
'--volume',
|
||||
`${userSettingsDirOnHost}:${getContainerPath(userSettingsDirOnHost)}`,
|
||||
@@ -309,8 +314,16 @@ export async function start_sandbox(
|
||||
// mount os.tmpdir() as os.tmpdir() inside container
|
||||
args.push('--volume', `${os.tmpdir()}:${getContainerPath(os.tmpdir())}`);
|
||||
|
||||
// mount homedir() as homedir() inside container
|
||||
if (userHomeDirOnHost !== os.homedir()) {
|
||||
args.push(
|
||||
'--volume',
|
||||
`${userHomeDirOnHost}:${getContainerPath(userHomeDirOnHost)}`,
|
||||
);
|
||||
}
|
||||
|
||||
// mount gcloud config directory if it exists
|
||||
const gcloudConfigDir = path.join(os.homedir(), '.config', 'gcloud');
|
||||
const gcloudConfigDir = path.join(homedir(), '.config', 'gcloud');
|
||||
if (fs.existsSync(gcloudConfigDir)) {
|
||||
args.push(
|
||||
'--volume',
|
||||
@@ -585,7 +598,7 @@ export async function start_sandbox(
|
||||
// necessary on Linux to ensure the user exists within the
|
||||
// container's /etc/passwd file, which is required by os.userInfo().
|
||||
const username = 'gemini';
|
||||
const homeDir = getContainerPath(os.homedir());
|
||||
const homeDir = getContainerPath(homedir());
|
||||
|
||||
const setupUserCommands = [
|
||||
// Use -f with groupadd to avoid errors if the group already exists.
|
||||
@@ -606,7 +619,7 @@ export async function start_sandbox(
|
||||
// We still need userFlag for the simpler proxy container, which does not have this issue.
|
||||
userFlag = `--user ${uid}:${gid}`;
|
||||
// When forcing a UID in the sandbox, $HOME can be reset to '/', so we copy $HOME as well.
|
||||
args.push('--env', `HOME=${os.homedir()}`);
|
||||
args.push('--env', `HOME=${homedir()}`);
|
||||
}
|
||||
|
||||
// push container image name
|
||||
|
||||
Reference in New Issue
Block a user