@@ -6,6 +6,7 @@ import type {
66 FileItem ,
77 FileFinder as FileFinderType ,
88 GrepCursor ,
9+ GrepResult ,
910 MixedItem ,
1011 MixedSearchResult ,
1112 Result ,
@@ -298,13 +299,13 @@ const QUERY_SAFE_DIRECTORY = /^[^\s*?[\]{}!\\]+\/$/;
298299/**
299300 * fff applies `.gitignore` files but not `.git/info/exclude` or `core.excludesFile`, where tools
300301 * commonly hide nested worktrees. Directories git excludes through those sources become
301- * root-anchored `!dir/**` constraints appended to every query.
302+ * root-anchored `!dir/**` query constraints .
302303 */
303- const loadExclusionConstraints = Effect . fn ( "WorkspaceSearchIndex.loadExclusionConstraints " ) (
304+ const loadExcludedDirectories = Effect . fn ( "WorkspaceSearchIndex.loadExcludedDirectories " ) (
304305 function * ( vcsProcess : VcsProcess . VcsProcess [ "Service" ] , cwd : string ) {
305306 const runGit = ( args : ReadonlyArray < string > , stdin ?: string ) =>
306307 vcsProcess . run ( {
307- operation : "WorkspaceSearchIndex.loadExclusionConstraints " ,
308+ operation : "WorkspaceSearchIndex.loadExcludedDirectories " ,
308309 command : "git" ,
309310 args : [ "-c" , "core.fsmonitor=false" , ...args ] ,
310311 cwd,
@@ -323,27 +324,27 @@ const loadExclusionConstraints = Effect.fn("WorkspaceSearchIndex.loadExclusionCo
323324 "--no-empty-directory" ,
324325 "-z" ,
325326 ] ) ;
326- if ( listed . exitCode !== 0 ) return "" ;
327+ if ( listed . exitCode !== 0 ) return [ ] ;
327328 const directories = listed . stdout
328329 . split ( "\0" )
329330 . filter ( ( entry ) => QUERY_SAFE_DIRECTORY . test ( entry ) ) ;
330- if ( directories . length === 0 ) return "" ;
331+ if ( directories . length === 0 ) return [ ] ;
331332 const classified = yield * runGit (
332333 [ "check-ignore" , "--verbose" , "-z" , "--stdin" ] ,
333334 `${ directories . join ( "\0" ) } \0` ,
334335 ) ;
335- if ( classified . exitCode !== 0 ) return "" ;
336+ if ( classified . exitCode !== 0 ) return [ ] ;
336337 // Records are `source NUL line NUL pattern NUL path NUL`.
337338 const fields = classified . stdout . split ( "\0" ) ;
338- const constraints : string [ ] = [ ] ;
339+ const excluded : string [ ] = [ ] ;
339340 for ( let index = 0 ; index + 3 < fields . length ; index += 4 ) {
340341 if ( ! GITIGNORE_SOURCE . test ( fields [ index ] ! ) ) {
341- constraints . push ( `! ${ fields [ index + 3 ] } **` ) ;
342+ excluded . push ( fields [ index + 3 ] ! ) ;
342343 }
343344 }
344- return constraints . join ( " " ) ;
345+ return excluded ;
345346 } ,
346- Effect . orElseSucceed ( ( ) => "" ) ,
347+ Effect . orElseSucceed ( ( ) : ReadonlyArray < string > => [ ] ) ,
347348) ;
348349
349350const createFinder = Effect . fn ( "WorkspaceSearchIndex.createFinder" ) ( function * (
@@ -433,14 +434,20 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
433434 const hasIncompleteInitialScan = ( ) => initialScanTimedOut && finder . isScanning ( ) ;
434435
435436 const vcsProcess = yield * VcsProcess . VcsProcess ;
436- let exclusionConstraints = yield * loadExclusionConstraints ( vcsProcess , cwd ) ;
437+ let excludedDirectories = yield * loadExcludedDirectories ( vcsProcess , cwd ) ;
438+ const exclusionConstraints = ( ) =>
439+ [ GITLINK_CONSTRAINT , ...excludedDirectories . map ( ( directory ) => `!${ directory } **` ) ] . join ( " " ) ;
437440 const withExclusions = ( query : string ) =>
438- [ query , GITLINK_CONSTRAINT , exclusionConstraints ] . filter ( Boolean ) . join ( " " ) ;
441+ [ query , exclusionConstraints ( ) ] . filter ( Boolean ) . join ( " " ) ;
442+ const isExcludedPath = ( path : string ) =>
443+ path === ".git" ||
444+ path . endsWith ( "/.git" ) ||
445+ excludedDirectories . some ( ( directory ) => path . startsWith ( directory ) ) ;
439446
440447 const runSearch = Effect . fn ( "WorkspaceSearchIndex.runSearch" ) ( function * < A > (
441448 query : string ,
442449 pageSize : number ,
443- operation : "directorySearch" | "fileSearch" | "grep" | "mixedSearch" ,
450+ operation : "directorySearch" | "fileSearch" | "grep" | "mixedSearch" | "multiGrep" ,
444451 execute : ( ) => Result < A > ,
445452 ) : Effect . fn . Return < A , WorkspaceSearchIndexSearchFailed > {
446453 const result = yield * Effect . try ( {
@@ -493,7 +500,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
493500 cause,
494501 } ) ,
495502 ) ;
496- exclusionConstraints = yield * loadExclusionConstraints ( vcsProcess , cwd ) ;
503+ excludedDirectories = yield * loadExcludedDirectories ( vcsProcess , cwd ) ;
497504 } ) ;
498505
499506 const list : WorkspaceSearchIndex [ "Service" ] [ "list" ] = Effect . fn ( "WorkspaceSearchIndex.list" ) (
@@ -546,6 +553,10 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
546553 "WorkspaceSearchIndex.searchContents" ,
547554 ) ( function * ( input ) {
548555 const { searchQuery, regexMode } = buildContentSearchQuery ( input ) ;
556+ // fff reads a lone token as the pattern but parses constraint syntax (`src/`, `*.ts`, `!x`)
557+ // once other tokens follow, so exclusions must not share a single-token query.
558+ const pattern = searchQuery . trim ( ) ;
559+ const singleToken = ! / \s / . test ( pattern ) ;
549560 const deadline = performance . now ( ) + CONTENT_SEARCH_TIME_BUDGET_MS ;
550561 // Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing.
551562 const rawPageSize = input . wholeWord
@@ -557,18 +568,33 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (
557568
558569 do {
559570 const remainingTimeBudgetMs = Math . max ( 1 , Math . ceil ( deadline - performance . now ( ) ) ) ;
560- const result = yield * runSearch ( input . query , input . limit , "grep" , ( ) =>
561- finder . grep ( withExclusions ( searchQuery ) , {
562- mode : regexMode ? "regex" : "plain" ,
563- smartCase : ! input . caseSensitive && ! regexMode ,
564- maxMatchesPerFile : Math . min ( CONTENT_SEARCH_MAX_MATCHES_PER_FILE , rawPageSize ) ,
565- pageSize : rawPageSize ,
566- cursor : nextCursor ,
567- timeBudgetMs : remainingTimeBudgetMs ,
568- } ) ,
569- ) ;
571+ const options = {
572+ smartCase : ! input . caseSensitive && ! regexMode ,
573+ maxMatchesPerFile : Math . min ( CONTENT_SEARCH_MAX_MATCHES_PER_FILE , rawPageSize ) ,
574+ pageSize : rawPageSize ,
575+ cursor : nextCursor ,
576+ timeBudgetMs : remainingTimeBudgetMs ,
577+ } ;
578+ // Plain tokens keep exclusions native via multiGrep's separate constraints; regex has no such
579+ // API, so a lone regex token runs unconstrained and excluded paths are dropped below.
580+ const result : GrepResult =
581+ singleToken && ! regexMode
582+ ? yield * runSearch ( input . query , input . limit , "multiGrep" , ( ) =>
583+ finder . multiGrep ( {
584+ ...options ,
585+ patterns : [ pattern ] ,
586+ constraints : exclusionConstraints ( ) ,
587+ } ) ,
588+ )
589+ : yield * runSearch ( input . query , input . limit , "grep" , ( ) =>
590+ finder . grep ( singleToken ? searchQuery : withExclusions ( searchQuery ) , {
591+ ...options ,
592+ mode : regexMode ? "regex" : "plain" ,
593+ } ) ,
594+ ) ;
570595
571596 for ( const match of result . items ) {
597+ if ( isExcludedPath ( toPosixPath ( match . relativePath ) ) ) continue ;
572598 const matchRanges = mapContentMatchRanges ( match . lineContent , match . matchRanges ) . filter (
573599 ( range ) => ! input . wholeWord || isWholeWordRange ( match . lineContent , range ) ,
574600 ) ;
0 commit comments