[Feature Request] Preserve CWD context across multiple repository operations
Bug Description
Stop force-resetting CWD when working in multiple repos.
Environment Info
- Platform: darwin
- Terminal: iTerm.app
- Version: 2.0.76
- Feedback ID:
Bug Report: Bash Tool Forcibly Resets CWD After Every Command
Summary
The Bash tool resets the current working directory (cwd) to the original session directory after every command execution, even when commands explicitly change directories. This makes multi-repository workflows unnecessarily difficult, verbose, and dangerous.
Environment
- Tool: Claude Code CLI (Bash tool)
- OS: macOS (Darwin)
- Session Context: Working across multiple git repositories
Expected Behavior
When executing cd /path/to/repo, subsequent Bash commands should:
- Execute in
/path/to/repo - Maintain that working directory until explicitly changed
- Allow natural shell workflow patterns
Actual Behavior
After every Bash command that changes directory, the tool resets cwd to the original session directory:
# Command 1
cd /path/to/project-repo && git status
# Output: Shows correct status
# Message: "Shell cwd was reset to /path/to/web-repo"
# Command 2 (NEW command)
git add .github/
# This executes in /path/to/web-repo
# NOT in /path/to/project-repo where I just was
Impact
1. Verbose and Repetitive Code
Every single command must include cd /full/path &&:
# Required pattern (repetitive)
cd /path/to/project-repo && git status
cd /path/to/project-repo && git add .
cd /path/to/project-repo && git commit -m "..."
cd /path/to/project-repo && git push
2. Dangerous - Wrong Repository Execution
Easy to forget the cd prefix and execute commands in the wrong repository:
# Intended: commit in project-repo
# Actual: commits in web-repo because cwd was reset
git commit -m "Add feature infrastructure"
This can cause:
- Commits in wrong repositories
- File modifications in wrong locations
- Data loss or corruption
- Security issues (wrong secrets file, wrong .env)
3. Breaks Tool Composition
Code review agents and other tools that expect to operate in the current directory context fail because they're looking in the original directory, not where the user is actively working.
Example:
code-reviewer agent: "I don't see the files you mentioned..."
# Because it's looking in web-repo
# But the staged files are in project-repo
4. Violates Principle of Least Surprise
Standard shell behavior maintains cwd across commands. Forcibly resetting violates user expectations and standard POSIX shell semantics.
Reproduction Steps
- Start Claude Code CLI session in
/path/to/repo-a - Execute:
cd /path/to/repo-b && git status - Execute:
git status(without cd prefix) - Observe: Second command executes in
/path/to/repo-a, not/path/to/repo-b
Proposed Solutions
Option 1: Persistent Shell Session (Preferred)
Maintain a persistent shell session that preserves cwd across Bash tool invocations:
- First
cdchanges working directory - Subsequent commands execute in that directory
- Allow explicit
cdto change again - Standard shell behavior
Option 2: Explicit Session Management
Provide explicit session control:
# Start new shell session in specific directory
Bash(command="git status", cwd="/path/to/repo")
# Or create named sessions
Bash(command="git status", session="project-session")
Option 3: Warning Without Reset
Keep current behavior but:
- Warn when cwd differs from command execution path
- Don't silently reset
- Let user explicitly reset if desired
Workaround Used
Currently forced to use cd /absolute/path && command for every single command, which is:
- Verbose (added ~30 characters to every command)
- Error-prone (easy to forget)
- Clutters logs with repetitive path information
Related Issues
This also affected:
- File operations with Read/Write/Edit tools (work fine with absolute paths)
- Task tool agent contexts (agents inherit original cwd, not current working location)
- Git operations across multiple repositories in same session
Request
Please either:
- Remove the forced cwd reset (preferred - maintain standard shell behavior)
- Make it configurable (setting to enable/disable cwd persistence)
- Provide clear documentation on why this behavior exists and recommended patterns
The current behavior makes multi-repository workflows significantly more difficult than necessary and introduces dangerous failure modes.
---
Session Context
During a typical multi-repo session, a user might work across two repositories:
/path/to/web-repo(Phase 1)/path/to/project-repo(Phase 2)
The cwd reset forces the use of cd /full/path && prefix on every single command (50+ times in a typical session), and causes code review agents to look in the wrong repository because their context inherits the reset cwd instead of the user's intended working location.
---
Additional Notes
This issue exists regardless of:
- Specific directory names or repository names
- Project type or structure
- Operating system (macOS, Linux)
- Number of repositories being managed
The fundamental problem is that the tool's behavior conflicts with standard shell semantics and user expectations about how directory navigation works.
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗