gemini-cli - ✅(Solved) Fix [bug] Unable to remove directories from workspace context after adding them [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#26180Fetched 2026-04-30 06:44:51
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×1labeled ×1

Fix Action

Fix / Workaround

  1. Stale directories — If an added directory is later deleted or renamed by the user or another process, it remains in the workspace context indefinitely. There is no mechanism to clean it up.
  2. Accidental inclusions — If a user mistakenly adds the wrong directory, the only workaround is to restart the entire CLI session.
  3. Scope creep — Users who want to dynamically narrow their working set (e.g., after finishing work on a sub-project) cannot do so without restarting.

PR fix notes

PR #26179: fix(directory): allow removing invalid or unwanted workspace directories

Description (problem / solution / changelog)

Summary

Fixes the inability to remove directories from the workspace context at runtime. Previously, once a directory was added (via --include-directories or /directory add), there was no way to remove it if it later became invalid (deleted, renamed, or inaccessible) or if the user simply wanted to narrow the working scope. This left stale or erroneous paths permanently in the session with no workaround other than restarting the CLI.

This change introduces the missing /directory remove sub-command so users can clean up their workspace on the fly.

Impact: Users can now remove stale or mistakenly added workspace directories by running /directory remove <path>[,<path>...].

Details

Problem

  • After adding a directory to the workspace, it could never be removed during the same session.
  • If the directory was later deleted or renamed, it remained in the workspace context and could cause confusion or errors.
  • Users had to restart the CLI entirely just to correct an accidental or obsolete inclusion.

Fix

  • Introduces /directory remove <paths> — a new sub-command under /directory that accepts one or more comma-separated directory paths.
  • Tab completion — Only suggests directories that are currently in the workspace, making it easy to discover removable paths.
  • CWD protection — The current working directory (targetDir) is protected from removal to prevent accidentally orphaning the session.
  • Sandbox restriction — The command is blocked in restrictive sandbox profiles, consistent with the existing add behavior.
  • Batching & feedback — Supports removing multiple directories in one call; reports success, not-found, and protected-path errors separately.

Core changes

  • Added WorkspaceContext.removeDirectories() in packages/core/src/utils/workspaceContext.ts:
    • Resolves relative paths and symlinks before matching.
    • Returns { removed: string[], notFound: string[] }.
    • Emits a single onDirectoriesChanged event only when at least one directory is actually removed.
  • Added batchRemoveDirectories() helper in packages/cli/src/ui/utils/directoryUtils.ts to bridge UI concerns with the core API.

Tests

  • 76 new / updated tests across two test files, all passing:
    • packages/core/src/utils/workspaceContext.test.ts — 6 tests covering removal, relative-path resolution, mixed results, event emission, and CWD removal.
    • packages/cli/src/ui/commands/directoryCommand.test.tsx — 10 tests covering sandbox restriction, empty input, CWD protection, single/multiple removals, not-found handling, and tab completion.

Related Issues

<!-- No linked issue — this is a standalone feature addition. -->

https://github.com/google-gemini/gemini-cli/issues/26180

How to Validate

  1. Start a Gemini CLI session with multiple directories:
    npx gemini-cli --include-directories /path/to/project1,/path/to/project2
  2. List current directories to confirm they are loaded:
    /directory show
  3. Remove one directory:
    /directory remove /path/to/project2
    Expect: Info message confirming removal.
  4. Attempt to remove the CWD:
    /directory remove /path/to/project1
    Expect: Error message protecting the current working directory.
  5. Try tab completion on /directory remove <TAB> — only currently-added directories should appear.
  6. Run unit tests:
    npx vitest run packages/cli/src/ui/commands/directoryCommand.test.tsx packages/core/src/utils/workspaceContext.test.ts
    Expect: 76 tests passing.

Pre-Merge Checklist

<!-- Check all that apply before requesting review or merging. -->
  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any) — None; this is a purely additive feature.
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

Changed files

  • packages/cli/src/ui/commands/directoryCommand.test.tsx (modified, +180/-0)
  • packages/cli/src/ui/commands/directoryCommand.tsx (modified, +146/-0)
  • packages/cli/src/ui/utils/directoryUtils.ts (modified, +20/-0)
  • packages/core/src/utils/workspaceContext.test.ts (modified, +82/-0)
  • packages/core/src/utils/workspaceContext.ts (modified, +40/-0)

Code Example

> /about
# paste output here
RAW_BUFFERClick to expand / collapse

What happened?

Once a directory is added to the workspace (either via --include-directories on startup or via the /directory add command), there is no way to remove it during the same session. This causes several practical problems:

  1. Stale directories — If an added directory is later deleted or renamed by the user or another process, it remains in the workspace context indefinitely. There is no mechanism to clean it up.
  2. Accidental inclusions — If a user mistakenly adds the wrong directory, the only workaround is to restart the entire CLI session.
  3. Scope creep — Users who want to dynamically narrow their working set (e.g., after finishing work on a sub-project) cannot do so without restarting.

The /directory command currently only supports add and show sub-commands. Attempting to remove a directory by any other means has no effect.

What did you expect to happen?

There should be a /directory remove <path> sub-command (or equivalent mechanism) that allows users to remove one or more directories from the current workspace context at runtime, without requiring a session restart.

Ideally it should:

  • Accept comma-separated paths for batch removal.
  • Provide tab-completion scoped to currently-added directories.
  • Protect the current working directory from accidental removal.
  • Report which paths were successfully removed and which were not found.

Client information

<details> <summary>Client Information</summary>

Run gemini to enter the interactive CLI, then run the /about command.

> /about
# paste output here
</details>

Login information

No response

Anything else we need to know?

No response

extent analysis

TL;DR

To address the issue, consider adding a /directory remove sub-command to allow users to dynamically remove directories from the workspace context.

Guidance

  • Implement a /directory remove <path> sub-command to enable removal of directories at runtime.
  • Design the command to accept comma-separated paths for batch removal and provide tab-completion scoped to currently-added directories.
  • Ensure the command protects the current working directory from accidental removal and reports the outcome of the removal operation for each path.
  • Consider adding input validation to handle cases where a directory is not found or cannot be removed.

Example

/directory remove /path/to/directory1, /path/to/directory2

This example illustrates how the proposed /directory remove command could be used to remove multiple directories at once.

Notes

The implementation details of the /directory remove command are not specified, and its integration with the existing /directory command and workspace context management will require careful consideration.

Recommendation

Apply workaround: Implement the proposed /directory remove sub-command to provide users with a way to dynamically manage their workspace context without requiring a session restart. This approach directly addresses the reported issues and provides a more flexible and user-friendly experience.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

gemini-cli - ✅(Solved) Fix [bug] Unable to remove directories from workspace context after adding them [1 pull requests, 1 participants]