Skip to content

fix(cli): prevent infinite loop in ghost text wrapping when inputWidth is narrower than a codepoint#27747

Open
sridhar-3009 wants to merge 1 commit into
google-gemini:mainfrom
sridhar-3009:fix/cli-freeze-ghost-text-zero-width-19985
Open

fix(cli): prevent infinite loop in ghost text wrapping when inputWidth is narrower than a codepoint#27747
sridhar-3009 wants to merge 1 commit into
google-gemini:mainfrom
sridhar-3009:fix/cli-freeze-ghost-text-zero-width-19985

Conversation

@sridhar-3009

Copy link
Copy Markdown

Summary

Fixes #19985 — the CLI freezes when an @filename:line completion is active and the terminal is too narrow to display a wide character (e.g. an emoji rendered at width 2 in a 1-column window).

Root cause: getGhostTextLines in InputPrompt.tsx contains a while (stringWidth(wordToProcess) > inputWidth) loop that splits an oversized word into lines. An inner for-loop scans codepoints and advances splitIndex only when partWidth + charWidth <= inputWidth. When inputWidth is 0 — or smaller than a single codepoint's rendered width — no codepoint ever fits, so splitIndex stays 0. cpSlice(wordToProcess, 0) then returns the full word unchanged, and the outer while loops forever, freezing the renderer.

Fix: After the inner scan, if splitIndex is still 0 and the word is non-empty, force-advance by one codepoint. This guarantees the loop always makes progress and terminates.

// When inputWidth is narrower than a single codepoint, nothing
// fits and splitIndex stays 0, causing an infinite loop. Force-
// advance by one codepoint so the loop always terminates.
if (splitIndex === 0 && wordCP.length > 0) {
  part = wordCP[0];
  splitIndex = 1;
}

Test plan

  • Added regression test does not freeze when ghost text contains wide chars and inputWidth is narrow in InputPrompt.test.tsx: sets inputWidth=1 with an emoji ghost text (😀, width 2), which would have hung the test runner before this fix
  • pnpm test in packages/cli — existing suite passes

…h is narrower than a codepoint

When `inputWidth` is 0 or smaller than a wide character's rendered
width (e.g. an emoji rendered at width 2 in a 1-column terminal), the
inner for-loop in `getGhostTextLines` could not fit any codepoint so
`splitIndex` stayed 0. `cpSlice(word, 0)` then returned the full word
unchanged, trapping the outer while-loop forever and freezing the CLI.

Force-advance by one codepoint when `splitIndex` is still 0 after the
inner scan, so the loop always makes progress and terminates.

Fixes google-gemini#19985
@sridhar-3009 sridhar-3009 requested a review from a team as a code owner June 8, 2026 15:34
@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 a critical issue where the CLI would freeze when rendering ghost text in extremely narrow terminal windows. By ensuring the text wrapping logic always makes progress even when individual characters exceed the available display width, the fix guarantees that the rendering loop terminates correctly.

Highlights

  • Infinite Loop Prevention: Added a safety check in the ghost text wrapping logic to force-advance by one codepoint when the input width is too narrow to fit any characters, preventing an infinite loop.
  • Regression Testing: Implemented a new test case in InputPrompt.test.tsx that simulates a narrow terminal window with wide characters to ensure the CLI remains responsive.
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 the 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 counterproductive. 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.

@google-cla

google-cla Bot commented Jun 8, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the size/s A small PR label Jun 8, 2026
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 44
  • Additions: +44
  • Deletions: -0
  • Files changed: 2

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

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.

Code Review

This pull request fixes an infinite loop bug in InputPrompt that occurs when wrapping ghost text containing wide characters (such as emojis) when the inputWidth is narrower than the character's rendered width. It resolves this by force-advancing the loop by one codepoint if no characters fit. A regression test has also been added to verify this behavior. I have no feedback to provide as there are no review comments.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. 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 Jun 8, 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! priority/p2 Important but can be addressed in a future release. size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI hangs/freezes when using @filename:line or @filename:range syntax

1 participant