Skip to content

fix(core): handle ENAMETOOLONG in robustRealpath - #26741

Closed
CadanHu wants to merge 1 commit into
google-gemini:mainfrom
CadanHu:fix/enametoolong-robust-realpath
Closed

fix(core): handle ENAMETOOLONG in robustRealpath#26741
CadanHu wants to merge 1 commit into
google-gemini:mainfrom
CadanHu:fix/enametoolong-robust-realpath

Conversation

@CadanHu

@CadanHu CadanHu commented May 9, 2026

Copy link
Copy Markdown

Summary

Why a CLI-layer fix isn't enough

PR #25009 added a length guard in the cli package, but the @-command processor bypasses it. The crash reported in #26368 and #26627 happens after that bypass, inside core. The safeguard therefore needs to live in core to be effective for every input vector.

Test plan

  • Added a regression test that passes a 5000-char string through resolveToRealPath (would previously crash with ENAMETOOLONG)
  • Added a mocked-realpathSync test asserting ENAMETOOLONG is handled like ENOENT/EISDIR
  • npx vitest run packages/core/src/utils/paths.test.ts — 106 passed

@CadanHu
CadanHu requested a review from a team as a code owner May 9, 2026 15:35
@google-cla

google-cla Bot commented May 9, 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.

@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 crash due to unhandled ENAMETOOLONG errors when processing long strings interpreted as paths. By extending the existing error handling logic in the core utility layer, the system now gracefully classifies these inputs as non-paths instead of terminating execution.

Highlights

  • Error Handling Improvement: Updated robustRealpath to treat ENAMETOOLONG errors similarly to ENOENT and EISDIR, preventing CLI crashes when processing excessively long input strings.
  • Regression Testing: Added comprehensive test cases, including a 5000-character string input test and a mocked fs.realpathSync error scenario to ensure stability.
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.

@CadanHu

CadanHu commented May 9, 2026

Copy link
Copy Markdown
Author

Heads up — I should have checked the open PR queue before opening this. There are at least three other open PRs proposing essentially the same fix:

There is also #25980, which takes a different approach (try/catch at the atCommandProcessor layer in the CLI package).

I'll leave this PR open for visibility, but I have no preference — please feel free to close this in favor of any of the above. The crash is reproducible and the fix is straightforward; the main blocker seems to be reviewer bandwidth rather than the change itself. Happy to help review or test whichever PR a maintainer prefers.

@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 updates the robustRealpath utility to handle ENAMETOOLONG errors, ensuring the application can recover when file paths exceed system limits. Feedback was provided regarding a redundant test case that is ineffective due to existing global mocks, suggesting its removal to maintain test suite clarity.

Comment thread packages/core/src/utils/paths.test.ts Outdated
Comment on lines +565 to +568
it('should not throw when input exceeds PATH_MAX (ENAMETOOLONG)', () => {
const longInput = 'a'.repeat(5000);
expect(() => resolveToRealPath(longInput)).not.toThrow();
});

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

This test is ineffective as a regression test because fs.realpathSync is globally mocked at line 28 to return the input string without throwing. As a result, the catch block in robustRealpath is never reached during this test, and it passes even without the bug fix. Since the subsequent test at line 570 correctly uses vi.spyOn to simulate the ENAMETOOLONG error and verify the recovery logic, this test is redundant and should be removed to avoid confusion.

@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 labels May 9, 2026
When a long string (e.g., a pasted multi-line stack trace) is interpreted
as an @path command, it is forwarded to robustRealpath. The underlying
fs.realpathSync / fs.lstatSync calls then throw ENAMETOOLONG (the input
exceeds the system's PATH_MAX), which was unhandled and surfaced as an
unhandled rejection that crashed the CLI.

Treat ENAMETOOLONG the same as ENOENT/EISDIR so the value is gracefully
treated as "not a real path" instead of a fatal error. PR google-gemini#25009 added
a length guard at the CLI layer, but the @-command processor bypasses
that guard, so the safeguard is needed at the core layer as well.

Fixes google-gemini#26368
@CadanHu
CadanHu force-pushed the fix/enametoolong-robust-realpath branch from a99fd79 to 0cd6585 Compare May 9, 2026 15:52
@CadanHu

CadanHu commented May 15, 2026

Copy link
Copy Markdown
Author

Closing this PR — the same fix has landed on main via #27069, which adds both ENAMETOOLONG and ENOTDIR handling to robustRealpath in packages/core/src/utils/paths.ts. The alternative CLI-layer guard from #25980 has also been merged. No further action needed here. Thanks!

@CadanHu CadanHu closed this May 15, 2026
@CadanHu
CadanHu deleted the fix/enametoolong-robust-realpath branch May 15, 2026 09:00
@sripasg sripasg added the size/s A small PR label Jun 2, 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 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.

[Bug] CLI crashes with ENAMETOOLONG when pasting long strings into prompt

2 participants