Skip to content

Commit 8f0edcd

Browse files
authored
fix(cli): use os.homedir() for home directory warning check (#25890)
1 parent 04e875c commit 8f0edcd

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

packages/cli/src/utils/userStartupWarnings.test.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import {
1919
} from '@google/gemini-cli-core';
2020

2121
// Mock os.homedir to control the home directory in tests
22-
vi.mock('os', async (importOriginal) => {
22+
vi.mock('node:os', async (importOriginal) => {
2323
const actualOs = await importOriginal<typeof os>();
2424
return {
2525
...actualOs,
26-
homedir: vi.fn(),
26+
homedir: vi.fn(() => actualOs.homedir()),
2727
};
2828
});
2929

@@ -32,7 +32,6 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
3232
await importOriginal<typeof import('@google/gemini-cli-core')>();
3333
return {
3434
...actual,
35-
homedir: () => os.homedir(),
3635
getCompatibilityWarnings: vi.fn().mockReturnValue([]),
3736
isHeadlessMode: vi.fn().mockReturnValue(false),
3837
WarningPriority: {
@@ -66,6 +65,7 @@ describe('getUserStartupWarnings', () => {
6665

6766
afterEach(async () => {
6867
await fs.rm(testRootDir, { recursive: true, force: true });
68+
vi.unstubAllEnvs();
6969
vi.restoreAllMocks();
7070
});
7171

@@ -98,6 +98,54 @@ describe('getUserStartupWarnings', () => {
9898
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
9999
});
100100

101+
it('should not return a warning when running in a subdirectory of home', async () => {
102+
const subDir = path.join(homeDir, 'projects', 'my-app');
103+
await fs.mkdir(subDir, { recursive: true });
104+
const warnings = await getUserStartupWarnings({}, subDir);
105+
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
106+
});
107+
108+
it('should not return a warning when home directory is a symlink and running in a subdirectory', async () => {
109+
const realHome = path.join(testRootDir, 'real-home');
110+
await fs.mkdir(realHome, { recursive: true });
111+
const symlinkedHome = path.join(testRootDir, 'symlinked-home');
112+
await fs.symlink(realHome, symlinkedHome);
113+
vi.mocked(os.homedir).mockReturnValue(symlinkedHome);
114+
115+
const subDir = path.join(symlinkedHome, 'projects');
116+
await fs.mkdir(subDir, { recursive: true });
117+
const warnings = await getUserStartupWarnings({}, subDir);
118+
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
119+
});
120+
121+
it('should return a warning when home directory is a symlink and running in it', async () => {
122+
const realHome = path.join(testRootDir, 'real-home2');
123+
await fs.mkdir(realHome, { recursive: true });
124+
const symlinkedHome = path.join(testRootDir, 'symlinked-home2');
125+
await fs.symlink(realHome, symlinkedHome);
126+
vi.mocked(os.homedir).mockReturnValue(symlinkedHome);
127+
128+
const warnings = await getUserStartupWarnings({}, symlinkedHome);
129+
expect(warnings).toContainEqual(
130+
expect.objectContaining({
131+
id: 'home-directory',
132+
message: expect.stringContaining(
133+
'Warning you are running Gemini CLI in your home directory',
134+
),
135+
priority: WarningPriority.Low,
136+
}),
137+
);
138+
});
139+
140+
it('should not return a warning when GEMINI_CLI_HOME differs from os.homedir', async () => {
141+
const projectDir = path.join(testRootDir, 'project');
142+
await fs.mkdir(projectDir, { recursive: true });
143+
vi.stubEnv('GEMINI_CLI_HOME', projectDir);
144+
145+
const warnings = await getUserStartupWarnings({}, projectDir);
146+
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
147+
});
148+
101149
it('should not return a warning when folder trust is enabled and workspace is trusted', async () => {
102150
vi.mocked(isFolderTrustEnabled).mockReturnValue(true);
103151
vi.mocked(isWorkspaceTrusted).mockReturnValue({

packages/cli/src/utils/userStartupWarnings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
import fs from 'node:fs/promises';
8+
import { homedir as osHomedir } from 'node:os';
89
import path from 'node:path';
910
import process from 'node:process';
1011
import {
11-
homedir,
1212
getCompatibilityWarnings,
1313
WarningPriority,
1414
type StartupWarning,
@@ -39,10 +39,10 @@ const homeDirectoryCheck: WarningCheck = {
3939
try {
4040
const [workspaceRealPath, homeRealPath] = await Promise.all([
4141
fs.realpath(workspaceRoot),
42-
fs.realpath(homedir()),
42+
fs.realpath(osHomedir()),
4343
]);
4444

45-
if (workspaceRealPath === homeRealPath) {
45+
if (path.resolve(workspaceRealPath) === path.resolve(homeRealPath)) {
4646
// If folder trust is enabled and the user trusts the home directory, don't show the warning.
4747
if (
4848
isFolderTrustEnabled(settings) &&

0 commit comments

Comments
 (0)