-
Notifications
You must be signed in to change notification settings - Fork 10.2k
feat:Add ~/.gemini/system.md fallback for GEMINI_SYSTEM_MD=1|true #15548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
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. |
Summary of ChangesHello @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 Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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.
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 basePromptThis approach consolidates file reading and error handling, making the code safer and more reliable.
There was a problem hiding this comment.
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.
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.tsUse .gemini/system.md in the project directory.
Use ~/.gemini/system.md
Prefer .gemini/system.md in the project directory even when ~/.gemini/system.md exists.
Pre-Merge Checklist