Skip to content

Conversation

@facelezzzz
Copy link

@facelezzzz facelezzzz commented Dec 25, 2025

Summary

This PR allows setting GEMINI_SYSTEM_MD=1 or true so that when .gemini/system.md does not exist in the project root, the system will fall back to $HOME/.gemini/system.md.

Details

Although GEMINI_SYSTEM_MD can be used to specify an explicit path, once a path is set, .gemini/system.md inside certain repositories will no longer take effect, which is inconvenient for our team.

What we want instead is:
- Use .gemini/system.md if it exists in the project
- Otherwise, fall back to $HOME/.gemini/system.md
-
This allows our team to keep a consistent global setup while still easily enabling repository-specific .gemini/system.md files when needed.
The behavior is intuitive and provides good flexibility.

Related Issues

#15549

How to Validate

Run test at packages/core/src/core/prompts.test.ts

Use .gemini/system.md in the project directory.

(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % echo 'YOU ANSWER "I am just a robot" NO MATTER WHAT USER ASK.' >> .gemini/system.md
(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % GEMINI_SYSTEM_MD=1 npm start
gemini_feature_p1

Use ~/.gemini/system.md

(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % rm .gemini/system.md                                                               
(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % echo 'YOU ANSWER "I am just a human" NO MATTER WHAT USER ASK.' >> ~/.gemini/system.md
(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % GEMINI_SYSTEM_MD=1 npm start
gemini_f_p2

Prefer .gemini/system.md in the project directory even when ~/.gemini/system.md exists.

(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % echo 'YOU ANSWER "I am just a human" NO MATTER WHAT USER ASK.' >> ~/.gemini/system.md
(base) wangboxue@wangboxuedeMacBook-Air gemini-cli % echo 'YOU ANSWER "I am just a robot" NO MATTER WHAT USER ASK.' >> .gemini/system.md 
gemini_f_p3

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

@facelezzzz facelezzzz requested a review from a team as a code owner December 25, 2025 08:11
@google-cla
Copy link

google-cla bot commented Dec 25, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @facelezzzz, 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 introduces a significant improvement to how the GEMINI_SYSTEM_MD environment variable is handled, providing a more robust and flexible system prompt resolution mechanism. It enables a fallback to a user's global system prompt configuration (~/.gemini/system.md) if a project-specific one (.gemini/system.md) is not present, streamlining workflow for teams and individual developers.

Highlights

  • Enhanced GEMINI_SYSTEM_MD Behavior: When GEMINI_SYSTEM_MD is set to 1 or true, the system now first checks for .gemini/system.md in the project root.
  • Global System Prompt Fallback: If the project-specific .gemini/system.md is not found, the system will automatically fall back to using ~/.gemini/system.md in the user's home directory.
  • Improved Flexibility: This change allows teams to maintain a consistent global system prompt setup while still providing the flexibility for individual repositories to define their own specific system prompts when needed.
  • Documentation Updates: Relevant documentation files (docs/cli/configuration.md, docs/cli/system-prompt.md, docs/get-started/configuration.md) have been updated to reflect this new fallback behavior.
  • New Test Case: A new test has been added in packages/core/src/core/prompts.test.ts to ensure the fallback mechanism works as expected.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
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 introduces a fallback mechanism to use a global system prompt from ~/.gemini/system.md when the project-specific one is not found. The implementation is logically correct and includes corresponding documentation updates and tests. However, the core logic relies on fs.existsSync() before reading the file, which introduces a Time-of-Check to Time-of-Use (TOCTOU) race condition. This could lead to an unhandled exception if the file is removed between the check and the read operation. I've provided a high-severity comment with a recommendation to refactor the logic to avoid this anti-pattern by attempting to read the files directly and handling any 'file not found' errors.

Comment on lines 115 to 131
if (!fs.existsSync(systemMdPath)) {
throw new Error(`missing system prompt file '${systemMdPath}'`);
if (!systemMdResolution.isSwitch) {
throw new Error(`missing system prompt file '${systemMdPath}'`);
}
const globalSystemMdPath = getGlobalSystemMdPath();
if (!globalSystemMdPath) {
throw new Error(
`missing system prompt file '${systemMdPath}' (failed to resolve home directory)`,
);
}
if (!fs.existsSync(globalSystemMdPath)) {
throw new Error(
`missing system prompt file '${systemMdPath}' (also checked '${globalSystemMdPath}')`,
);
}
systemMdPath = globalSystemMdPath;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The use of fs.existsSync() followed by a later fs.readFileSync() introduces a Time-of-Check to Time-of-Use (TOCTOU) race condition. If the file is deleted between these two calls, the application will crash with an unhandled error. This is a known anti-pattern in Node.js.

It's more robust to attempt to read the file directly and handle the ENOENT (file not found) error. This avoids the race condition entirely.

I recommend refactoring this logic to attempt reading the files directly instead of checking for existence first. Here's a conceptual example of how this could be structured:

function tryReadFile(path: string): string | null {
  try {
    return fs.readFileSync(path, 'utf8');
  } catch (error) {
    if (error.code === 'ENOENT') {
      return null; // File not found, which is an expected case here.
    }
    throw error; // For other errors (e.g., permissions), re-throw.
  }
}

// Inside getCoreSystemPrompt...
let systemPromptContent: string | null = null;

if (systemMdResolution.isSwitch) {
  systemPromptContent = tryReadFile(systemMdPath);
  if (systemPromptContent === null) {
    const globalPath = getGlobalSystemMdPath();
    if (globalPath) {
      systemPromptContent = tryReadFile(globalPath);
    }
  }
  if (systemPromptContent === null) {
    // throw error that neither file was found
  }
} else {
  systemPromptContent = tryReadFile(systemMdPath);
  if (systemPromptContent === null) {
    // throw error that custom file was not found
  }
}

// Then use systemPromptContent to set basePrompt

This approach consolidates file reading and error handling, making the code safer and more reliable.

Copy link
Author

Choose a reason for hiding this comment

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

This issue is not caused by the current commit and was already present in earlier versions.

@facelezzzz facelezzzz changed the title Add ~/.gemini/system.md fallback for GEMINI_SYSTEM_MD=1|true feat:Add ~/.gemini/system.md fallback for GEMINI_SYSTEM_MD=1|true Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant