mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-07-09 09:30:41 -07:00
fix: handle ENAMETOOLONG gracefully during path resolution
This fix was recovered from a timed-out bot run. It addresses issue #26979 where the CLI would crash if a user provided an extremely long path string in an @ command (e.g. @/aaa...a). Changes: - Updated 'robustRealpath' in 'packages/core/src/utils/paths.ts' to catch and gracefully handle 'ENAMETOOLONG' and 'EINVAL' errors from fs.realpathSync and fs.lstatSync. - Added a defensive try-catch block to 'checkPermissions' in 'packages/cli/src/ui/hooks/atCommandProcessor.ts' to prevent long path strings from crashing the CLI during @ command parsing. - Added regression unit tests to verify the fix.
This commit is contained in:
@@ -602,6 +602,19 @@ describe('resolveToRealPath', () => {
|
||||
/Infinite recursion detected/,
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle ENAMETOOLONG gracefully', () => {
|
||||
const longPath = path.resolve('/' + 'a'.repeat(5000));
|
||||
|
||||
vi.spyOn(fs, 'realpathSync').mockImplementation(() => {
|
||||
const err = new Error('ENAMETOOLONG') as NodeJS.ErrnoException;
|
||||
err.code = 'ENAMETOOLONG';
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Should return the path itself if realpathSync fails with ENAMETOOLONG
|
||||
expect(resolveToRealPath(longPath)).toBe(longPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeRelative', () => {
|
||||
|
||||
@@ -440,7 +440,10 @@ function robustRealpath(p: string, visited = new Set<string>()): string {
|
||||
e &&
|
||||
typeof e === 'object' &&
|
||||
'code' in e &&
|
||||
(e.code === 'ENOENT' || e.code === 'EISDIR')
|
||||
(e.code === 'ENOENT' ||
|
||||
e.code === 'EISDIR' ||
|
||||
e.code === 'ENAMETOOLONG' ||
|
||||
e.code === 'EINVAL')
|
||||
) {
|
||||
try {
|
||||
const stat = fs.lstatSync(p);
|
||||
@@ -457,7 +460,10 @@ function robustRealpath(p: string, visited = new Set<string>()): string {
|
||||
lstatError &&
|
||||
typeof lstatError === 'object' &&
|
||||
'code' in lstatError &&
|
||||
(lstatError.code === 'ENOENT' || lstatError.code === 'EISDIR')
|
||||
(lstatError.code === 'ENOENT' ||
|
||||
lstatError.code === 'EISDIR' ||
|
||||
lstatError.code === 'ENAMETOOLONG' ||
|
||||
lstatError.code === 'EINVAL')
|
||||
)
|
||||
) {
|
||||
throw lstatError;
|
||||
|
||||
Reference in New Issue
Block a user