Skip to content

Comments

feat(cli): include /dir add directories in @ autocomplete suggestions#19246

Merged
jacob314 merged 1 commit intogoogle-gemini:mainfrom
jasmeetsb:feature/at-tag-added-dirs
Feb 18, 2026
Merged

feat(cli): include /dir add directories in @ autocomplete suggestions#19246
jacob314 merged 1 commit intogoogle-gemini:mainfrom
jasmeetsb:feature/at-tag-added-dirs

Conversation

@jasmeetsb
Copy link
Contributor

@jasmeetsb jasmeetsb commented Feb 17, 2026

Files from directories added with /dir add were not appearing in @ autocomplete suggestions because useAtCompletion only indexed the current working directory. Replace the single FileSearch instance with a Map of instances (one per workspace directory), listen to onDirectoriesChanged for mid-session updates, and show non-cwd results as absolute paths.

Fixes #18579

Summary

@ autocomplete only searched the current working directory. After /dir add ../other-project/, files from that directory never appeared in suggestions. This PR makes useAtCompletion index all workspace directories and merge results.

Details

  • Replaced single FileSearch ref with Map<string, FileSearch> keyed by directory path
  • Added initEpoch ref to guard against stale async completions after directory changes
  • Added onDirectoriesChanged listener to re-initialize search when directories are added/removed mid-session
  • Added second abort check after resource/agent search to prevent stale results
  • 3 new tests: multi-dir suggestions, mid-session dir add, same-filename deduplication

Related Issues

Fixes #18579

Testing

  • All 20 useAtCompletion tests pass (17 existing + 3 new)
  • Full preflight (npm run preflight) passed: 648 test files, 10,233 tests passed across all packages
  • Pre-commit hooks (prettier + eslint) passed on all staged files

How to Validate

  1. npm run build && npm start
  2. In a Gemini CLI session, type @ followed by a filename from your cwd — suggestions should appear as before
  3. Run /dir add /some/other/directory (use a real directory with files)
  4. Type @ followed by a filename that exists in the added directory — it should now appear with its absolute path

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@jasmeetsb jasmeetsb requested a review from a team as a code owner February 17, 2026 09:55
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jasmeetsb, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the @ autocomplete feature in the CLI by enabling it to search across all directories within the workspace, not just the current working directory. This resolves an issue where files from directories added via /dir add were not discoverable, improving the overall usability and consistency of file suggestions for users working with multi-directory projects.

Highlights

  • Multi-directory Autocomplete: Extended the @ autocomplete functionality to include files from directories added using /dir add, resolving an issue where these files were not discoverable.
  • Refactored File Search: Replaced the single FileSearch instance with a Map of instances, allowing the system to index and search across multiple workspace directories simultaneously.
  • Dynamic Directory Updates: Implemented a listener for onDirectoriesChanged to dynamically update the indexed directories during a session, ensuring autocomplete remains current as the workspace changes.
  • Stale Result Prevention: Introduced an initEpoch reference to prevent stale search results and added a second abort check after resource/agent searches to ensure the latest data is displayed.
  • New Test Coverage: Included three new tests to validate multi-directory suggestions, mid-session directory additions, and correct deduplication of same-named files from different directories.
Changelog
  • packages/cli/src/ui/hooks/useAtCompletion.test.ts
    • Added new test cases to verify multi-directory workspace support for file suggestions.
    • Included a test to confirm that newly added directories are picked up dynamically via onDirectoriesChanged.
    • Added a test to ensure correct handling and display of same-named files from different directories.
  • packages/cli/src/ui/hooks/useAtCompletion.ts
    • Replaced the single fileSearch useRef with fileSearchMap (a Map<string, FileSearch>) to manage multiple FileSearch instances, one for each workspace directory.
    • Introduced initEpoch to track initialization cycles and prevent race conditions with async operations.
    • Added a resetFileSearchState function to clear searchers and increment initEpoch upon directory changes or cwd/config updates.
    • Implemented a useEffect hook to subscribe to onDirectoriesChanged events from the workspace context, triggering resetFileSearchState when directories change.
    • Modified the initialize worker to create and manage FileSearch instances for all workspace directories, pruning outdated searchers.
    • Updated the search worker to aggregate results from all FileSearch instances in fileSearchMap and format non-CWD results as absolute paths.
    • Added an additional controller.signal.aborted check after resource/agent searches to prevent displaying stale results.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively extends the @ autocomplete functionality to include files from all workspace directories, not just the current working directory. The implementation correctly handles adding and removing directories during a session by listening to onDirectoriesChanged and re-initializing the file searchers. The use of an initEpoch and AbortController makes the asynchronous logic robust against race conditions. The new tests are comprehensive and validate the multi-directory support. I've identified a couple of areas in the initialize function where the code seems to be written for an incremental update strategy, while the overall design uses a full-reset approach. This leads to redundant code and potential performance issues, which have been highlighted in the comments with reference to best practices for object initialization. My comments suggest removing this redundant logic to improve clarity and align the code with its actual behavior.

const initPromises: Array<Promise<void>> = [];

for (const dir of directories) {
if (fileSearchMap.current.has(dir)) continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This check appears to be redundant. The resetFileSearchState function, which is called before initialize is triggered (e.g., from onDirectoriesChanged or changes to cwd/config), clears the fileSearchMap. Therefore, this condition will always be false. Removing this check would simplify the code and make its behavior clearer, reflecting the full re-initialization strategy that is currently implemented. This also has performance implications, as it suggests an incremental update that isn't happening, potentially masking the inefficiency of re-initializing all searchers on every change.

References
  1. When initializing an object that requires fetching remote settings, prefer to gather all settings first, then perform a single initialization. Avoid creating and initializing a temporary object just to fetch settings, as this can lead to resource leaks and unnecessary overhead. This aligns with the principle of performing a single, clean initialization after gathering all necessary settings, avoiding redundant logic that suggests an incremental update strategy and can lead to unnecessary overhead and confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is not redundant. There are two distinct reset paths, both of which exist in the original upstream code (not introduced by this PR):

  1. Hard reset (resetFileSearchState()) - clears the map. Triggered by cwd/config changes and onDirectoriesChanged.
  2. Soft reset (dispatch({ type: 'RESET' }) alone, lines 248/253) - resets reducer state to IDLE but preserves the search cache. Triggered when the user exits @ mode (clears the @ prefix or disables completion).

The original single-FileSearch code had the same pattern: the [cwd, config] effect dispatched RESET but the soft-reset paths (disabled/pattern-null) also only dispatched RESET neither path nulled out the fileSearch ref. The ref was reused on re-entry, avoiding a redundant filesystem crawl.

Our has(dir) check preserves this existing caching behavior for the multi-directory case. After a soft reset, the map retains cached FileSearch instances. When the user re-enters @ mode, has(dir) skips re-initialization for already-indexed directories. This is the same cache optimization the original code relied on.

Comment on lines 311 to 317
// Prune searchers for directories removed between getDirectories() and init completion
const dirSet = new Set(directories);
for (const key of fileSearchMap.current.keys()) {
if (!dirSet.has(key)) {
fileSearchMap.current.delete(key);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This pruning logic is unnecessary given the current implementation. The initEpoch mechanism correctly prevents stale FileSearch instances from being added to the map, and resetFileSearchState clears the map before a new initialization begins. At this point in the code, fileSearchMap.current will only contain instances created during this initialize call, so dirSet.has(key) will always be true. Removing this block simplifies the code and removes confusion about how stale searchers are handled.

References
  1. When initializing an object that requires fetching remote settings, prefer to gather all settings first, then perform a single initialization. Avoid creating and initializing a temporary object just to fetch settings, as this can lead to resource leaks and unnecessary overhead. This aligns with the principle of performing a single, clean initialization after gathering all necessary settings, avoiding redundant logic that suggests an incremental update strategy and can lead to unnecessary overhead and confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed in the latest push.

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 17, 2026
Files from directories added with `/dir add` were not appearing in @
autocomplete suggestions because useAtCompletion only indexed the current
working directory. Replace the single FileSearch instance with a Map of
instances (one per workspace directory), listen to onDirectoriesChanged
for mid-session updates, and show non-cwd results as absolute paths.

Fixes google-gemini#18579
@jasmeetsb jasmeetsb force-pushed the feature/at-tag-added-dirs branch from e170397 to f39dbdc Compare February 17, 2026 18:45
Copy link
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jacob314 jacob314 enabled auto-merge February 18, 2026 22:24
@jacob314 jacob314 added this pull request to the merge queue Feb 18, 2026
Merged via the queue into google-gemini:main with commit 012392a Feb 18, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Once a new directory is added with "add directory ../foo/", its files should be addressable with @ tag

2 participants