Skip to content

fix(cli): use os.homedir() for home directory warning check#25890

Open
TirthNaik-99 wants to merge 5 commits intogoogle-gemini:mainfrom
TirthNaik-99:fix/home-dir-warning-subdirectory
Open

fix(cli): use os.homedir() for home directory warning check#25890
TirthNaik-99 wants to merge 5 commits intogoogle-gemini:mainfrom
TirthNaik-99:fix/home-dir-warning-subdirectory

Conversation

@TirthNaik-99
Copy link
Copy Markdown

The home directory warning incorrectly used the core homedir() helper
which respects the GEMINI_CLI_HOME environment variable. When
GEMINI_CLI_HOME is set to a non-home directory, the warning could fire
in subdirectories or miss the actual home directory entirely.

Switch to Node's native os.homedir() so the check always compares
against the real OS home directory, and normalize both resolved paths
before comparison to handle trailing-slash and separator edge cases.

Add test coverage for subdirectories, symlinked home directories, and
GEMINI_CLI_HOME override scenarios.

Fixes #22309

Summary

Fix home directory warning triggering incorrectly in subdirectories by using os.homedir() instead of the core homedir() helper, and normalizing paths before comparison.

Details

  • The core homedir() from @google/gemini-cli-core respects the GEMINI_CLI_HOME env var, which is intended for config directory resolution — not for checking if the user is literally in their OS home directory. Replaced with Node's native os.homedir().
  • Added path.normalize() on both fs.realpath() outputs before the === comparison to handle trailing-slash and separator edge cases across platforms.
  • Added 4 new test cases: subdirectory of home, symlinked home + subdirectory, symlinked home (exact match), and GEMINI_CLI_HOME override scenario.

Related Issues

Fixes #22309

How to Validate

  1. Run the unit tests: npx vitest run packages/cli/src/utils/userStartupWarnings.test.ts - All 13 tests should pass.
  2. Run the type check: npx tsc --noEmit -p packages/cli/tsconfig.json
  3. Run the build: npm run build
  4. Manual validation:
    • Run gemini from your home directory (cd ~) — warning should appear
    • Run gemini from a subdirectory (cd ~/some-project) — warning should NOT appear

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

The home directory warning incorrectly used the core homedir() helper
which respects the GEMINI_CLI_HOME environment variable. When
GEMINI_CLI_HOME is set to a non-home directory, the warning could fire
in subdirectories or miss the actual home directory entirely.

Switch to Node's native os.homedir() so the check always compares
against the real OS home directory, and normalize both resolved paths
before comparison to handle trailing-slash and separator edge cases.

Add test coverage for subdirectories, symlinked home directories, and
GEMINI_CLI_HOME override scenarios.

Fixes google-gemini#22309
@TirthNaik-99 TirthNaik-99 requested a review from a team as a code owner April 23, 2026 22:40
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where the Gemini CLI incorrectly triggers a home directory warning when running in subdirectories. By switching to the native OS home directory resolution and adding path normalization, the CLI now accurately distinguishes between the actual home directory and its subdirectories, improving the user experience and preventing unnecessary warnings.

Highlights

  • Home Directory Detection: Replaced the core homedir() helper with Node's native os.homedir() to ensure the warning check correctly identifies the OS home directory regardless of GEMINI_CLI_HOME overrides.
  • Path Normalization: Implemented path.normalize() on both workspace and home directory paths to prevent false positives caused by trailing slashes or platform-specific separator differences.
  • Test Coverage: Added comprehensive test cases covering subdirectories, symlinked home directories, and environment variable overrides to ensure robust warning behavior.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the home directory detection logic in userStartupWarnings.ts by switching to node:os and applying path normalization to handle symlinks and subdirectories correctly. It also adds several test cases to verify these scenarios. One review comment identifies a violation of the repository's testing style guide, specifically recommending the use of vi.stubEnv() instead of direct process.env modification to prevent test leakage.

Comment on lines +141 to +151
await fs.mkdir(projectDir, { recursive: true });
const originalEnv = process.env['GEMINI_CLI_HOME'];
process.env['GEMINI_CLI_HOME'] = projectDir;

const warnings = await getUserStartupWarnings({}, projectDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();

if (originalEnv === undefined) {
delete process.env['GEMINI_CLI_HOME'];
} else {
process.env['GEMINI_CLI_HOME'] = originalEnv;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Directly modifying process.env is discouraged in this repository as it can lead to test leakage and is less reliable than using built-in Vitest utilities. Per the repository style guide, you should use vi.stubEnv() to set environment variables. Please also ensure that vi.unstubAllEnvs() is added to the afterEach block (around line 65) to properly clean up the environment after each test.

Suggested change
await fs.mkdir(projectDir, { recursive: true });
const originalEnv = process.env['GEMINI_CLI_HOME'];
process.env['GEMINI_CLI_HOME'] = projectDir;
const warnings = await getUserStartupWarnings({}, projectDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
if (originalEnv === undefined) {
delete process.env['GEMINI_CLI_HOME'];
} else {
process.env['GEMINI_CLI_HOME'] = originalEnv;
vi.stubEnv('GEMINI_CLI_HOME', projectDir);
const warnings = await getUserStartupWarnings({}, projectDir);
expect(warnings.find((w) => w.id === 'home-directory')).toBeUndefined();
References
  1. When testing code that depends on environment variables, use vi.stubEnv('NAME', 'value') in beforeEach and vi.unstubAllEnvs() in afterEach. Avoid modifying process.env directly as it can lead to test leakage and is less reliable. (link)

…tion

Address review feedback: use vi.stubEnv() for environment variable
manipulation in tests to prevent test leakage, per repository style guide.
@gemini-cli gemini-cli Bot added area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Home Dir Warning - Even when in subfolder of home dir?

2 participants