fix(cli): harden prompt trimming and improve version detection#23828
fix(cli): harden prompt trimming and improve version detection#23828chrisjcthomas wants to merge 4 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello, 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 enhances the CLI's robustness and improves its display and versioning mechanisms. It hardens user input handling by ensuring prompt values are always strings before processing, refines the display of array-based settings by serializing them to JSON, and improves version detection by preferring the main repository's package version when running from a development environment. Highlights
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. Footnotes
|
|
@jkcinouye I split the non-docs fixes out of #21679 into this separate PR so they can be reviewed independently. If you have a chance, I’d appreciate a look. |
There was a problem hiding this comment.
Code Review
This pull request refactors the version lookup logic to traverse up the directory tree, prioritizing the main CLI package version, and adds robust handling for null/undefined input values in UI components. It also enhances the settings display utility to correctly format arrays as JSON strings, with a corresponding new test case. A review comment points out that the new version lookup implementation in packages/core/src/utils/version.ts is inefficient due to redundant directory scanning and suggests a more performant and maintainable approach using asynchronous file system operations.
Note: Security Review did not run due to the size of the PR.
| let currentDir = __dirname; | ||
| let bestVersion = 'unknown'; | ||
|
|
||
| while (true) { | ||
| const pkgJson = await getPackageJson(currentDir); | ||
| if (pkgJson?.version) { | ||
| bestVersion = pkgJson.version; | ||
| if ( | ||
| pkgJson.name === '@google/gemini-cli' || | ||
| pkgJson.name === 'gemini-cli' | ||
| ) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const parentDir = path.dirname(currentDir); | ||
| if (parentDir === currentDir) { | ||
| break; | ||
| } | ||
| currentDir = parentDir; | ||
| } | ||
|
|
||
| return bestVersion; |
There was a problem hiding this comment.
This implementation is inefficient because it combines a while loop that traverses up the directory tree with a call to getPackageJson, which also searches up the tree using read-package-up. This results in redundant directory scanning in each iteration.
While the result is cached after the first run, this implementation is difficult to maintain and reason about. A more efficient approach would be to perform a single, explicit upward traversal. For example, using Node's fs.promises module to asynchronously read package.json from currentDir directly within the loop would make the logic clearer and more performant, and avoid blocking the event loop.
References
- Use asynchronous file system operations (e.g.,
fs.promises.readFile) instead of synchronous ones (e.g.,fs.readFileSync) to avoid blocking the event loop.
|
@g-samroberts adding you here as well since you had context on #21679. This is the split code-only follow-up from that PR; if you have a chance, I’d appreciate a look. |
|
Addressed the version lookup review note in the latest push. The implementation now performs a single explicit upward traversal with async package.json reads instead of combining an outer traversal with getPackageJson()'s own upward search. Also updated the focused unit test coverage for the source-version preference and cache behavior. |
Summary
submittedValuebefore trimming in the CLI input flowTesting
npx vitest run src/utils/version.test.tsnpx vitest run src/utils/settingsUtils.test.ts