From 7d3b4a450010c847d7416ba5992fc14ea93ede95 Mon Sep 17 00:00:00 2001 From: Shelley Pham Date: Fri, 13 Mar 2026 16:23:54 -0700 Subject: [PATCH] fix: index only the user-selected file/dir from @ autocomplete (#1388) Co-authored-by: Letta Code --- src/cli/components/FileAutocomplete.tsx | 11 ++++++++++- src/cli/helpers/fileSearch.ts | 16 +--------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/cli/components/FileAutocomplete.tsx b/src/cli/components/FileAutocomplete.tsx index 041ce11..000cb08 100644 --- a/src/cli/components/FileAutocomplete.tsx +++ b/src/cli/components/FileAutocomplete.tsx @@ -1,4 +1,5 @@ import { useEffect, useRef, useState } from "react"; +import { addEntriesToCache } from "../helpers/fileIndex"; import { searchFiles } from "../helpers/fileSearch"; import { useAutocompleteNavigation } from "../hooks/useAutocompleteNavigation"; import { AutocompleteBox, AutocompleteItem } from "./Autocomplete"; @@ -66,7 +67,15 @@ export function FileAutocomplete({ const { selectedIndex } = useAutocompleteNavigation({ matches, maxVisible: 10, - onSelect: onSelect ? (item) => onSelect(item.path) : undefined, + onSelect: onSelect + ? (item) => { + // Index only the selected item, not all search results + if (item.type === "file" || item.type === "dir") { + addEntriesToCache([{ path: item.path, type: item.type }]); + } + onSelect(item.path); + } + : undefined, manageActiveState: false, // We manage active state manually due to async loading }); diff --git a/src/cli/helpers/fileSearch.ts b/src/cli/helpers/fileSearch.ts index 4be1fa5..4f99ca4 100644 --- a/src/cli/helpers/fileSearch.ts +++ b/src/cli/helpers/fileSearch.ts @@ -1,12 +1,7 @@ import { readdirSync, statSync } from "node:fs"; import { join, relative, resolve } from "node:path"; import { debugLog } from "../../utils/debug"; -import { - addEntriesToCache, - ensureFileIndex, - type FileMatch, - searchFileIndex, -} from "./fileIndex"; +import { ensureFileIndex, type FileMatch, searchFileIndex } from "./fileIndex"; import { shouldHardExcludeEntry } from "./fileSearchConfig"; export function debounce unknown>( @@ -171,8 +166,6 @@ export async function searchFiles( } if (!indexSearchSucceeded || results.length === 0) { - const diskResultsBefore = results.length; - if (effectiveDeep) { // Deep search: recursively search subdirectories. // Use a shallower depth limit when searching outside the project directory @@ -225,13 +218,6 @@ export async function searchFiles( } catch {} } } - - // If the index was working but just didn't have these files (created - // externally), add the newly found entries so future searches hit the - // cache instead of falling back to disk again. - if (indexSearchSucceeded && results.length > diskResultsBefore) { - addEntriesToCache(results.slice(diskResultsBefore)); - } } // Only sort when the disk scan ran — its results come in arbitrary readdir