Skip to content

Commit

Permalink
Cody: Escape Windows path separator in fast file finder path pattern (#…
Browse files Browse the repository at this point in the history
…52754)

This is breaking the Cody extension on Windows.

Fixes #52730

## Test plan

```
cd client/cody
pnpm test:unit
```
  • Loading branch information
dominiccooney committed Jun 1, 2023
1 parent 70b5f58 commit 035b9ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion client/cody/src/chat/fastFileFinder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { filePathContains } from './fastFileFinder'
import { filePathContains, makeTrimRegex } from './fastFileFinder'

describe('makeTrimRegex', () => {
it('should not fail using the Windows path separator', () => {
expect(makeTrimRegex('\\').test('foo\\*')).toBe(true)
expect(makeTrimRegex('\\').test('foo\\*\\bar')).toBe(false)
})
it('should trim leading and trailing path separators and wildcards', () => {
expect('**\\foo\\bar'.replace(makeTrimRegex('\\'), '')).toBe('foo\\bar')
expect('//foo/bar/**'.replace(makeTrimRegex('/'), '')).toBe('foo/bar')
})
})

describe('filePathContains', () => {
it('should handle exact matches', () => {
Expand Down
9 changes: 8 additions & 1 deletion client/cody/src/chat/fastFileFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ export async function fastFilesExist(
return processRgOutput(rgOutput, filePaths)
}

export function makeTrimRegex(sep: string): RegExp {
if (sep === '\\') {
sep = '\\\\' // Regex escape this character
}
return new RegExp(`(^[*${sep}]+)|([*${sep}]+$)`, 'g')
}

// Regex to match '**', '*' or path.sep at the start (^) or end ($) of the string.
const trimRegex = new RegExp(`^([*${path.sep}]*)|([*${path.sep}]*)$`, 'g')
const trimRegex = makeTrimRegex(path.sep)
/**
* Constructs a search pattern for the 'rg' tool.
*
Expand Down

0 comments on commit 035b9ba

Please sign in to comment.