Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLS: escape backslashes in query string #6585

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion agent/src/cli/command-bench/strategy-chat-nls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import { graphqlClient, isError } from '@sourcegraph/cody-shared'
import { escapeNLSQuery } from '../../../../vscode/src/chat/chat-view/handlers/SearchHandler'
import { version } from '../../../package.json'
import type { CodyBenchOptions } from './command-bench'
import {
Expand Down Expand Up @@ -75,7 +76,7 @@ async function runNLSSearch(examples: Example[]): Promise<ExampleOutput[]> {
const repoNames = targetRepoRevs.map(repoRev => repoRev.repoName)
const repoFilter = 'repo:' + repoNames.join('|')

const fullQuery = `${repoFilter} content:"${query.replaceAll('"', '\\"')}"`
const fullQuery = `${repoFilter} content:"${escapeNLSQuery(query)}"`
const resultsResp = await graphqlClient.nlsSearchQuery({
query: fullQuery,
})
Expand Down
15 changes: 12 additions & 3 deletions vscode/src/chat/chat-view/handlers/SearchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ export class SearchHandler implements AgentHandler {
? 'boost:relevant.repos()'
: ''

const query = `content:"${inputTextWithoutContextChips.replaceAll(
'"',
'\\"'
const query = `content:"${escapeNLSQuery(
inputTextWithoutContextChips
)}" ${currentRepoBoost} ${myProjectsBoost} ${scopes.length ? `(${scopes.join(' OR ')})` : ''}`

try {
Expand Down Expand Up @@ -75,6 +74,16 @@ export class SearchHandler implements AgentHandler {
}
}

export function escapeNLSQuery(query: string): string {
return (
query
// first escape backslashes
.replaceAll('\\', '\\\\')
// then escape quotes
.replaceAll('"', '\\"')
)
}

async function getSearchScopesFromMentions(mentions: ContextItem[]): Promise<string[]> {
const validMentions = mentions.reduce(
(groups, mention) => {
Expand Down
16 changes: 16 additions & 0 deletions vscode/src/chat/chat-view/handlers/__tests__/SearchHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'
import { escapeNLSQuery } from '../SearchHandler'

describe('escapeNLSQuery', () => {
it('escapes backslashes', () => {
expect(escapeNLSQuery('path\\to\\file')).toBe('path\\\\to\\\\file')
})

it('escapes double quotes', () => {
expect(escapeNLSQuery(`say "hello"`)).toBe(`say \\"hello\\"`)
})

it('escapes escaped quotes', () => {
expect(escapeNLSQuery(`c:\\path\\"file"`)).toBe(`c:\\\\path\\\\\\"file\\"`)
})
})
Loading