Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit ec02bad

Browse files
authored
search: set Regexp annotation on patterns for FileContainsPatterns (#61102)
This was the root cause of a bug where searcher and zoekt treated the pattern it received inconsistently. The way file:contains.content() works is it always takes a regex. Additionally we came across a case for cody context not setting inputs as literal. This likely fixes a bug, but I did not test further. This came up when auditing direct creations of query.Pattern. Test Plan: added a unit test which ensures searcher uses a regex for the pattern.
1 parent e748425 commit ec02bad

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

internal/search/codycontext/query_transformer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ func queryStringToKeywordQuery(queryString string) (*keywordQuery, error) {
120120

121121
patternNodes := make([]query.Node, 0, len(transformedPatterns))
122122
for _, p := range transformedPatterns {
123-
patternNodes = append(patternNodes, query.Pattern{Value: p})
123+
node := query.Pattern{Value: p}
124+
node.Annotation.Labels.Set(query.Literal)
125+
patternNodes = append(patternNodes, node)
124126
}
125127

126128
if len(patternNodes) > 0 {

internal/search/job/jobutil/job.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func NewBasicJob(inputs *search.Inputs, b query.Basic) (job.Job, error) {
8484
if len(fileContainsPatterns) > 0 {
8585
newNodes := make([]query.Node, 0, len(fileContainsPatterns)+1)
8686
for _, pat := range fileContainsPatterns {
87-
newNodes = append(newNodes, query.Pattern{Value: pat})
87+
node := query.Pattern{Value: pat}
88+
node.Annotation.Labels.Set(query.Regexp)
89+
newNodes = append(newNodes, node)
8890
}
8991
if b.Pattern != nil {
9092
newNodes = append(newNodes, b.Pattern)

internal/search/job/jobutil/job_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,86 @@ func TestNewPlanJob(t *testing.T) {
852852
(repoOpts.hasFileContent[0].path . a)
853853
(repoOpts.hasFileContent[1].content . b)
854854
(repoNamePatterns . [])))))))
855+
`),
856+
}, {
857+
query: `file:contains.content(a.*b)`,
858+
protocol: search.Streaming,
859+
searchType: query.SearchTypeKeyword,
860+
want: autogold.Expect(`
861+
(LOG
862+
(ALERT
863+
(features . error decoding features)
864+
(protocol . Streaming)
865+
(onSourcegraphDotCom . true)
866+
(query . )
867+
(originalQuery . )
868+
(patternType . keyword)
869+
(TIMEOUT
870+
(timeout . 20s)
871+
(LIMIT
872+
(limit . 10000)
873+
(FILECONTAINSFILTER
874+
(originalPatterns . [])
875+
(filterPatterns . [(?i:a.*b)])
876+
(PARALLEL
877+
(ZOEKTGLOBALTEXTSEARCH
878+
(fileMatchLimit . 10000)
879+
(select . )
880+
(repoScope . [(and branch="HEAD" rawConfig:RcOnlyPublic|RcNoForks|RcNoArchived)])
881+
(includePrivate . true)
882+
(globalZoektQueryRegexps . [(?i)(?-s:a.*b)])
883+
(query . regex:"(?-s:a.*b)")
884+
(type . text))
885+
REPOSCOMPUTEEXCLUDED
886+
NOOP))))))
887+
`),
888+
}, {
889+
query: `repo:foo file:contains.content(a.*b) index:no`,
890+
protocol: search.Streaming,
891+
searchType: query.SearchTypeKeyword,
892+
want: autogold.Expect(`
893+
(LOG
894+
(ALERT
895+
(features . error decoding features)
896+
(protocol . Streaming)
897+
(onSourcegraphDotCom . true)
898+
(query . )
899+
(originalQuery . )
900+
(patternType . keyword)
901+
(TIMEOUT
902+
(timeout . 20s)
903+
(LIMIT
904+
(limit . 10000)
905+
(FILECONTAINSFILTER
906+
(originalPatterns . [])
907+
(filterPatterns . [(?i:a.*b)])
908+
(PARALLEL
909+
(REPOPAGER
910+
(containsRefGlobs . false)
911+
(repoOpts.repoFilters . [foo])
912+
(repoOpts.useIndex . no)
913+
(PARTIALREPOS
914+
(ZOEKTREPOSUBSETTEXTSEARCH
915+
(fileMatchLimit . 10000)
916+
(select . )
917+
(zoektQueryRegexps . [(?i)(?-s:a.*b)])
918+
(query . regex:"(?-s:a.*b)")
919+
(type . text))))
920+
(REPOPAGER
921+
(containsRefGlobs . false)
922+
(repoOpts.repoFilters . [foo])
923+
(repoOpts.useIndex . no)
924+
(PARTIALREPOS
925+
(SEARCHERTEXTSEARCH
926+
(useFullDeadline . true)
927+
(patternInfo . TextPatternInfo{(/a.*b/),filematchlimit:10000})
928+
(numRepos . 0)
929+
(pathRegexps . [(?i)a.*b])
930+
(indexed . false))))
931+
(REPOSCOMPUTEEXCLUDED
932+
(repoOpts.repoFilters . [foo])
933+
(repoOpts.useIndex . no))
934+
NOOP))))))
855935
`),
856936
}, {
857937
query: `repo:contains.file(path:a content:b)`,

0 commit comments

Comments
 (0)