mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-03 09:50:40 -07:00
fix(core): handle URI-encoded workspace paths in IdeClient (#17476)
Co-authored-by: Shreya Keshive <shreyakeshive@google.com>
This commit is contained in:
@@ -4,8 +4,23 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import { escapePath, unescapePath, isSubpath, shortenPath } from './paths.js';
|
||||
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import {
|
||||
escapePath,
|
||||
unescapePath,
|
||||
isSubpath,
|
||||
shortenPath,
|
||||
resolveToRealPath,
|
||||
} from './paths.js';
|
||||
|
||||
vi.mock('node:fs', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof fs>();
|
||||
return {
|
||||
...(actual as object),
|
||||
realpathSync: (p: string) => p,
|
||||
};
|
||||
});
|
||||
|
||||
describe('escapePath', () => {
|
||||
it.each([
|
||||
@@ -472,3 +487,42 @@ describe('shortenPath', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveToRealPath', () => {
|
||||
it.each([
|
||||
{
|
||||
description:
|
||||
'should return path as-is if no special characters or protocol',
|
||||
input: '/simple/path',
|
||||
expected: '/simple/path',
|
||||
},
|
||||
{
|
||||
description: 'should remove file:// protocol',
|
||||
input: 'file:///path/to/file',
|
||||
expected: '/path/to/file',
|
||||
},
|
||||
{
|
||||
description: 'should decode URI components',
|
||||
input: '/path/to/some%20folder',
|
||||
expected: '/path/to/some folder',
|
||||
},
|
||||
{
|
||||
description: 'should handle both file protocol and encoding',
|
||||
input: 'file:///path/to/My%20Project',
|
||||
expected: '/path/to/My Project',
|
||||
},
|
||||
])('$description', ({ input, expected }) => {
|
||||
expect(resolveToRealPath(input)).toBe(expected);
|
||||
});
|
||||
|
||||
it('should return decoded path even if fs.realpathSync fails', () => {
|
||||
vi.spyOn(fs, 'realpathSync').mockImplementationOnce(() => {
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
const input = 'file:///path/to/New%20Project';
|
||||
const expected = '/path/to/New Project';
|
||||
|
||||
expect(resolveToRealPath(input)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,8 @@ import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import process from 'node:process';
|
||||
import * as crypto from 'node:crypto';
|
||||
import * as fs from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
export const GEMINI_DIR = '.gemini';
|
||||
export const GOOGLE_ACCOUNTS_FILENAME = 'google_accounts.json';
|
||||
@@ -343,3 +345,34 @@ export function isSubpath(parentPath: string, childPath: string): boolean {
|
||||
!pathModule.isAbsolute(relative)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a path to its real path, sanitizing it first.
|
||||
* - Removes 'file://' protocol if present.
|
||||
* - Decodes URI components (e.g. %20 -> space).
|
||||
* - Resolves symbolic links using fs.realpathSync.
|
||||
*
|
||||
* @param pathStr The path string to resolve.
|
||||
* @returns The resolved real path.
|
||||
*/
|
||||
export function resolveToRealPath(path: string): string {
|
||||
let resolvedPath = path;
|
||||
|
||||
try {
|
||||
if (resolvedPath.startsWith('file://')) {
|
||||
resolvedPath = fileURLToPath(resolvedPath);
|
||||
}
|
||||
|
||||
resolvedPath = decodeURIComponent(resolvedPath);
|
||||
} catch (_e) {
|
||||
// Ignore error (e.g. malformed URI), keep path from previous step
|
||||
}
|
||||
|
||||
try {
|
||||
return fs.realpathSync(resolvedPath);
|
||||
} catch (_e) {
|
||||
// If realpathSync fails, it might be because the path doesn't exist.
|
||||
// In that case, we can fall back to the path processed.
|
||||
return resolvedPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user