diff --git a/packages/cli/src/ui/components/shared/text-buffer.test.ts b/packages/cli/src/ui/components/shared/text-buffer.test.ts index 85754f9f62..51fa728c91 100644 --- a/packages/cli/src/ui/components/shared/text-buffer.test.ts +++ b/packages/cli/src/ui/components/shared/text-buffer.test.ts @@ -1023,7 +1023,7 @@ describe('useTextBuffer', () => { useTextBuffer({ viewport, escapePastedPaths: true }), ); act(() => result.current.insert(filePath, { paste: true })); - expect(getBufferState(result).text).toBe(`@${filePath} `); + expect(getBufferState(result).text).toBe(`@${escapePath(filePath)} `); }); it('should not prepend @ to an invalid file path on insert', () => { @@ -1042,7 +1042,7 @@ describe('useTextBuffer', () => { ); const quotedPath = `'${filePath}'`; act(() => result.current.insert(quotedPath, { paste: true })); - expect(getBufferState(result).text).toBe(`@${filePath} `); + expect(getBufferState(result).text).toBe(`@${escapePath(filePath)} `); }); it('should not prepend @ to short text that is not a path', () => { @@ -1063,9 +1063,11 @@ describe('useTextBuffer', () => { const { result } = renderHook(() => useTextBuffer({ viewport, escapePastedPaths: true }), ); - const filePaths = `${file1} ${file2}`; + const filePaths = `${escapePath(file1)} ${escapePath(file2)}`; act(() => result.current.insert(filePaths, { paste: true })); - expect(getBufferState(result).text).toBe(`@${file1} @${file2} `); + expect(getBufferState(result).text).toBe( + `@${escapePath(file1)} @${escapePath(file2)} `, + ); }); it('should handle multiple paths with escaped spaces', () => { @@ -1077,13 +1079,12 @@ describe('useTextBuffer', () => { const { result } = renderHook(() => useTextBuffer({ viewport, escapePastedPaths: true }), ); - // Construct escaped path string: "/path/to/my\ file.txt /path/to/other.txt" - const filePaths = `${escapePath(file1)} ${file2}`; + const filePaths = `${escapePath(file1)} ${escapePath(file2)}`; act(() => result.current.insert(filePaths, { paste: true })); expect(getBufferState(result).text).toBe( - `@${escapePath(file1)} @${file2} `, + `@${escapePath(file1)} @${escapePath(file2)} `, ); }); diff --git a/packages/cli/src/ui/hooks/atCommandProcessor.test.ts b/packages/cli/src/ui/hooks/atCommandProcessor.test.ts index 76848ea821..4a6af6f9a1 100644 --- a/packages/cli/src/ui/hooks/atCommandProcessor.test.ts +++ b/packages/cli/src/ui/hooks/atCommandProcessor.test.ts @@ -290,8 +290,8 @@ describe('handleAtCommand', () => { path.join(testRootDir, 'path', 'to', 'my file.txt'), fileContent, ); - const escapedpath = path.join(testRootDir, 'path', 'to', 'my\\ file.txt'); - const query = `@${escapedpath}`; + + const query = `@${core.escapePath(filePath)}`; const result = await handleAtCommand({ query, @@ -960,8 +960,8 @@ describe('handleAtCommand', () => { path.join(testRootDir, 'spaced file.txt'), fileContent, ); - const escapedPath = path.join(testRootDir, 'spaced\\ file.txt'); - const query = `Check @${escapedPath}, it has spaces.`; + + const query = `Check @${core.escapePath(filePath)}, it has spaces.`; const result = await handleAtCommand({ query, diff --git a/packages/cli/src/ui/hooks/useAtCompletion.test.ts b/packages/cli/src/ui/hooks/useAtCompletion.test.ts index 7c41b7593d..472ae7f053 100644 --- a/packages/cli/src/ui/hooks/useAtCompletion.test.ts +++ b/packages/cli/src/ui/hooks/useAtCompletion.test.ts @@ -13,6 +13,7 @@ import type { Config, FileSearch } from '@google/gemini-cli-core'; import { FileSearchFactory, FileDiscoveryService, + escapePath, } from '@google/gemini-cli-core'; import type { FileSystemStructure } from '@google/gemini-cli-test-utils'; import { createTmpDir, cleanupTmpDir } from '@google/gemini-cli-test-utils'; @@ -90,7 +91,7 @@ describe('useAtCompletion', () => { 'src/', 'src/components/', 'file.txt', - 'src/components/Button\\ with\\ spaces.tsx', + `${escapePath('src/components/Button with spaces.tsx')}`, 'src/components/Button.tsx', 'src/index.js', ]); diff --git a/packages/core/src/utils/paths.ts b/packages/core/src/utils/paths.ts index 6c3236606d..8fbafca56f 100644 --- a/packages/core/src/utils/paths.ts +++ b/packages/core/src/utils/paths.ts @@ -277,8 +277,8 @@ export function makeRelative( */ export function escapePath(filePath: string): string { if (process.platform === 'win32') { - // Windows: Double quote if it contains space or special chars - if (/[\s()[\]{};|&^$!@%`'~]/.test(filePath)) { + // Windows: Double quote if it contains special chars + if (/[\s&()[\]{}^=;!'+,`~%$@#]/.test(filePath)) { return `"${filePath}"`; } return filePath;