Skip to content

Commit bdf8eef

Browse files
cursoragentmsukkari
andcommitted
fix: trim whitespace from prefix expression values in query parser
This fixes an issue where whitespace between a prefix keyword and its value (e.g., 'repo: localhost:12030/testrepo' with a space after 'repo:') would cause the value to include the leading space, resulting in failed searches. The grammar captures text from the start of the prefix keyword to the end of the value, so any whitespace between them was included. By trimming the extracted value, we ensure proper matching. Also adds test cases for: - Repos with ports in URLs (e.g., localhost:12030/testrepo) - Multiple repos with ports in reposet filters - Combined repoNamesFilter and repoNamesFilterRegexp with ports Co-authored-by: michael <michael@sourcebot.dev>
1 parent dacbe5d commit bdf8eef

3 files changed

Lines changed: 89 additions & 2 deletions

File tree

packages/queryLanguage/test/prefixes.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,51 @@ Program(ParenExpr(PrefixExpr(FileExpr)))
334334

335335
Program(ParenExpr(AndExpr(PrefixExpr(FileExpr),PrefixExpr(LangExpr))))
336336

337+
# Repo with port in URL
338+
339+
repo:localhost:12030/testrepo
340+
341+
==>
342+
343+
Program(PrefixExpr(RepoExpr))
344+
345+
# Repo short form with port in URL
346+
347+
r:myserver:3000/project
348+
349+
==>
350+
351+
Program(PrefixExpr(RepoExpr))
352+
353+
# RepoSet with port in URL
354+
355+
reposet:localhost:12030/testrepo
356+
357+
==>
358+
359+
Program(PrefixExpr(RepoSetExpr))
360+
361+
# RepoSet with multiple repos with ports
362+
363+
reposet:myserver:12030/repo1,localhost:3000/repo2
364+
365+
==>
366+
367+
Program(PrefixExpr(RepoSetExpr))
368+
369+
# Term and repo with port
370+
371+
test.cpp repo:localhost:12030/testrepo
372+
373+
==>
374+
375+
Program(AndExpr(Term,PrefixExpr(RepoExpr)))
376+
377+
# Term and reposet with port
378+
379+
test.cpp reposet:myserver:12030/testrepo
380+
381+
==>
382+
383+
Program(AndExpr(Term,PrefixExpr(RepoSetExpr)))
384+

packages/web/src/features/chat/utils.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,41 @@ test('buildSearchQuery handles query with special characters', () => {
511511
});
512512

513513
expect(result).toBe('console.log("hello world") reposet:test-repo');
514+
});
515+
516+
test('buildSearchQuery handles repoNamesFilter with port in URL', () => {
517+
const result = buildSearchQuery({
518+
query: 'test.cpp',
519+
repoNamesFilter: ['localhost:12030/testrepo']
520+
});
521+
522+
expect(result).toBe('test.cpp reposet:localhost:12030/testrepo');
523+
});
524+
525+
test('buildSearchQuery handles multiple repoNamesFilter with ports', () => {
526+
const result = buildSearchQuery({
527+
query: 'function test',
528+
repoNamesFilter: ['myserver:12030/repo1', 'localhost:3000/repo2']
529+
});
530+
531+
expect(result).toBe('function test reposet:myserver:12030/repo1,localhost:3000/repo2');
532+
});
533+
534+
test('buildSearchQuery handles repoNamesFilterRegexp with port in URL', () => {
535+
const result = buildSearchQuery({
536+
query: 'test.cpp',
537+
repoNamesFilterRegexp: ['localhost:12030/testrepo']
538+
});
539+
540+
expect(result).toBe('test.cpp ( repo:localhost:12030/testrepo )');
541+
});
542+
543+
test('buildSearchQuery combines repoNamesFilter and repoNamesFilterRegexp with ports', () => {
544+
const result = buildSearchQuery({
545+
query: 'test.cpp',
546+
repoNamesFilter: ['myserver:12030/testrepo'],
547+
repoNamesFilterRegexp: ['localhost:12030/other']
548+
});
549+
550+
expect(result).toBe('test.cpp reposet:myserver:12030/testrepo ( repo:localhost:12030/other )');
514551
});

packages/web/src/features/search/parser.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ const transformTreeToIR = async ({
223223
throw new Error(`${prefixNode.type.name} missing colon`);
224224
}
225225

226-
// Get the value part after the colon and remove quotes if present
227-
const value = fullText.substring(colonIndex + 1).replace(/^"|"$/g, '');
226+
// Get the value part after the colon, remove quotes if present, and trim whitespace.
227+
// Trimming is necessary because the grammar may capture spaces between the prefix
228+
// keyword and the value (e.g., "repo: localhost" instead of "repo:localhost").
229+
const value = fullText.substring(colonIndex + 1).replace(/^"|"$/g, '').trim();
228230

229231
switch (prefixTypeId) {
230232
case FileExpr:

0 commit comments

Comments
 (0)