Skip to content

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

Merged
scidomino merged 1 commit intogoogle-gemini:mainfrom
TirthNaik-99:fix/home-dir-warning-subdirectory
May 4, 2026
Merged

fix(cli): use os.homedir() for home directory warning check#25890
scidomino merged 1 commit intogoogle-gemini:mainfrom
TirthNaik-99:fix/home-dir-warning-subdirectory

Conversation

@TirthNaik-99
Copy link
Copy Markdown
Contributor

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

@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)

@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
@Adib234 Adib234 self-assigned this May 4, 2026
Copy link
Copy Markdown
Collaborator

@scidomino scidomino left a comment

Choose a reason for hiding this comment

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

You have check failures. Also:

In Node.js, path.normalize() does not strip trailing slashes (e.g., path.normalize('/a/b/c/') !== path.normalize('/a/b/c')). If the goal is robust path comparison that ignores trailing slashes, using path.resolve(pathA) === path.resolve(pathB) or path.relative(pathA, pathB) === '' would be the accurate way to achieve this. Given fs.realpath rarely leaves trailing slashes anyway (except for root directories), it may not be a functional bug in practice, but the logic/comment doesn't match the reality of Node.js's path module.

@TirthNaik-99 TirthNaik-99 requested review from a team as code owners May 4, 2026 21:44
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 4, 2026

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@TirthNaik-99
Copy link
Copy Markdown
Contributor Author

Thanks for the review @scidomino! I've addressed your feedback:

  • Replaced path.normalize() with path.resolve() which correctly strips trailing slashes
  • The vi.stubEnv() and vi.unstubAllEnvs() changes from the earlier bot review are also included

Note: The branch has picked up extra merge commits from syncing with main : the actual changes are only in 2 files (userStartupWarnings.ts and userStartupWarnings.test.ts). Happy to squash or rebase if you'd prefer a cleaner history.

@scidomino scidomino force-pushed the fix/home-dir-warning-subdirectory branch from 23954d5 to 0c7f5e5 Compare May 4, 2026 21:51
@scidomino
Copy link
Copy Markdown
Collaborator

No worries. I rebased it for you. Reviewing it now.

@scidomino
Copy link
Copy Markdown
Collaborator

Something's wrong with the commit signature since it's failing the cla check. Maybe squash everything into one commit and resign it.

@TirthNaik-99 TirthNaik-99 force-pushed the fix/home-dir-warning-subdirectory branch 2 times, most recently from e248a34 to a8b6e42 Compare May 4, 2026 22:17
@TirthNaik-99
Copy link
Copy Markdown
Contributor Author

@googlebot I signed it.

@scidomino
Copy link
Copy Markdown
Collaborator

I know you signed it but we're still getting an error and we can't proceed until it's resolved: Screenshot 2026-05-04 at 3 27 02 PM

The specific error:
Screenshot 2026-05-04 at 3 27 48 PM

It doesn't make sense to me but it looks like maybe you didn't sign with the right credentials.

@TirthNaik-99 TirthNaik-99 force-pushed the fix/home-dir-warning-subdirectory branch from a8b6e42 to 7889e57 Compare May 4, 2026 22:32
Use Node's native os.homedir() instead of the core homedir() helper
which respects GEMINI_CLI_HOME. Apply path.resolve() for robust
trailing-slash-safe comparison. Add test coverage for subdirectories,
symlinked home directories, and GEMINI_CLI_HOME override scenarios.

Fixes google-gemini#22309
@TirthNaik-99 TirthNaik-99 force-pushed the fix/home-dir-warning-subdirectory branch from 7889e57 to d4e2f27 Compare May 4, 2026 22:33
@TirthNaik-99
Copy link
Copy Markdown
Contributor Author

@googlebot I signed it.

@scidomino scidomino enabled auto-merge May 4, 2026 23:12
@scidomino scidomino added this pull request to the merge queue May 4, 2026
Merged via the queue into google-gemini:main with commit 8f0edcd May 4, 2026
27 checks passed
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?

3 participants