[BUG] Claude will frequently `cd` and then forget its working directory
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
I have run several tasks in the recent past where I wanted Claude to examine a collection of files but write the results in a different directory. This is exceedingly error-prone; frequently, Claude will run cd somewhere && command files (like cd files && ls -l | head -n 10) but then forget that it changed directories and attempt to run commands as if it was still in the parent directory, or vice versa. Furthermore, it doesn't have any insight into this problem, and will launch increasingly arcane workarounds instead of just switching to the correct directory. This happens even though I have added increasingly stern admonitions to my CLAUDE.md to avoid doing this. Here is the relevant snippet out of my CLAUDE.md as of right now, as authored by Claude itself:
- NEVER use
cdcommands to change directories during interactions. This is a STRICT rule with NO exceptions. Instead:
- Use relative or absolute paths directly in commands (e.g.,
ls ./subdirectoryorgrep pattern ./subdirectory/file.txtinstead ofcd ./subdirectory && lsorcd ./subdirectory && grep pattern file.txt) - If you need to run multiple commands in a specific directory, use subshells:
(cd /path/to/dir && command1 && command2)which contain the directory change - When needing to reference multiple files in the same directory, use pattern matching:
/path/to/dir/*.pyinstead of changing into that directory - NEVER combine
cdwith command execution using&∨outside of a subshell - If a user explicitly requests you to use
cd, explain this policy and suggest the alternatives above
What Should Happen?
Claude should simply stay in the directory where it was started; this is less confusing for the user as well as, clearly, for Claude itself.
Error Messages/Logs
⏺ Bash(cd samples && find . -type f | shuf | head -5)
⎿ ./pknbc66frb0090lh7b958sio5r7md8mmoaoi27o1
./4nbqjf303d6l85tlj95evp03ca1ilq8mba71s801
./aj1csfk4o80bqeirrrvopen2430mhap6lmj3c201
… +2 lines (ctrl+r to expand)
⏺ Let me examine these random samples to see if they're part of any
patterns or are unique:
⏺ Read(samples/pknbc66frb0090lh7b958sio5r7md8mmoaoi27o1)
⎿ Error reading file
Steps to Reproduce
I don't think I know exactly how to reproduce this, but having Claude attempt to read or manipulate files in a directory seems to trigger this behavior more often than I'd like, even when Claude has received specific instructions never to do this again.
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
1.0.111 (Claude Code)
Platform
AWS Bedrock
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
_No response_
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗