claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Claude Code frequently loses track of which directory it is in

Open lukehutch opened this issue 6 months ago • 23 comments

Claude Code frequently assumes it is in a different directory than the one it is in. A command will fail, and sometimes it takes several rounds of its own troubleshooting before it realizes the problem and changes to the correct directory. This is not only annoying, it's dangerous -- it caused me to lose 60 hours of wrangling with Claude Code when a git hard reset was executed in the wrong directory (#1668).

Please prepend to every prompt something like "You are currently in this working directory: ..." or whatever is necessary to get it to understand where it is starting from.

lukehutch avatar Jun 05 '25 18:06 lukehutch

I have seen this issue as well. My folder structure is /Users/username/workspace/project-name claude code will often try to run code in workspace folder. I even moved to /Users/username/project-name outside of workspace folder but it seem to remember that folder and keeps navigating back to it.

cheeseblubber avatar Jul 09 '25 20:07 cheeseblubber

I can't stress enough how significant this issue is. It's in the top 3 time wasters along with forgetting to source a venv and not sufficiently testing code before marking a todo complete.

jxstanford avatar Jul 18 '25 05:07 jxstanford

Right, it wastes both user time, but also user tokens (we're paying for this).

lukehutch avatar Jul 18 '25 08:07 lukehutch

This happens all the time. Every time it tries to invoke make or run a test or git add files. It tries commands in the wrong directory, then tries to cd to directories it's already in, then tries to change to an absolute path that may or may not be correct. Maybe it should cd back to the directory it was started in?

bgrant0607 avatar Jul 24 '25 23:07 bgrant0607

This looked like an infinite loop. It just kept going until I stopped it:

⏺ Bash(cd ...)
  ⎿  Error: (eval):cd:1: no such file or directory: ...

⏺ Bash(cd ...)
  ⎿  Error: (eval):cd:1: no such file or directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(...)
  ⎿  Read 40 lines (ctrl+r to expand)

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../..)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...

⏺ Read(../../../../../../...)
  ⎿  Error: File does not exist. Current working directory: ...
  ⎿  Interrupted by user

All of these were trying to access the same file from the same directory. The file appeared to be in the current working directory, judging by the one command that succeeded.

One possibly relevant detail: Our codebase is primarily Go, but we also have Typescript -- it's a small monorepo. The Go code is split at the top level into public/ and internal/, each of which has its own go.mod. Typescript is in ui/src primarily. I start claude code in the top-level directory of the git repo.

bgrant0607 avatar Jul 25 '25 01:07 bgrant0607

How about providing a guideline like this in Claude's markdown file, or a generic markdown file that you ask claude to read before starting any session. This worked for me till some extent, until it loses context after compacting. I just had to ask it to go through the markdown again. Just a workaround, till the time it is actually fixed by the devs.

Directory Navigation Guidelines:

  • Project Root: /Users/abc/Github/Project/ - Contains client/, server/, sdk/, docs/, etc.
  • Working Directory: /Users/abc/Github/Project/client/ - This is where you'll usually be working
  • Key Directories:
    • Documentation: /Users/abc/Github/Project/docs/ (NOT in client/docs/)
    • Frontend Code: /Users/abc/Github/Project/client/ (apps/, libs/, etc.)
    • Server Code: /Users/abc/Github/Project/server/
  • When files are not found: Always check if they exist at the project root level (../filename) before assuming they don't exist
  • Use absolute paths: When in doubt, use full absolute paths starting from /Users/abc/Github/Project/

ankuranandredelk avatar Aug 08 '25 10:08 ankuranandredelk

This is currently the most severe and debilitating issue I've experienced with claude-code. In one of my recent sessions, it mistook the directory it was in and removed all files in the project directory (it was trying to clean the build subdirectory). Thankfully, there were no changes since the last Git commit, so nothing was lost, but it's extremely frustrating that this keeps happening. How hard is it to keep Claude's cwd in memory and/or checking before running commands?

ido50 avatar Sep 09 '25 22:09 ido50

Why does Claude not prepend to every prompt, "You are currently in this directory:..."?

lukehutch avatar Sep 09 '25 22:09 lukehutch

I have the current directory in my status line and check it often because of this very problem.

#!/bin/bash
# Read JSON input from stdin
input=$(cat)

# Extract values using jq
MODEL_DISPLAY=$(echo "$input" | jq -r '.model.display_name')
CURRENT_DIR=$(echo "$input" | jq -r '.workspace.current_dir')

# Show git branch if in a git repo
GIT_BRANCH=""
if git rev-parse --git-dir > /dev/null 2>&1; then
    BRANCH=$(git branch --show-current 2>/dev/null)
    if [ -n "$BRANCH" ]; then
        GIT_BRANCH=" |  $BRANCH"
    fi
fi

echo "[$MODEL_DISPLAY] ${CURRENT_DIR}$GIT_BRANCH"

bradleyjames avatar Sep 09 '25 22:09 bradleyjames

I completely solved the problem with these hooks:

https://github.com/lpasqualis/lpclaude/tree/master/hooks

lpasqualis avatar Sep 09 '25 23:09 lpasqualis

I completely solved the problem with these hooks:

https://github.com/lpasqualis/lpclaude/tree/master/hooks

Thanks. Should I use both hooks, or is guard-cd.sh enough to solve this problem?

ido50 avatar Sep 09 '25 23:09 ido50

I completely solved the problem with these hooks:

https://github.com/lpasqualis/lpclaude/tree/master/hooks

Thanks. Should I use both hooks, or is guard-cd.sh enough to solve this problem?

Guard-cd is all you really need

lpasqualis avatar Sep 09 '25 23:09 lpasqualis

I completely solved the problem with these hooks: https://github.com/lpasqualis/lpclaude/tree/master/hooks

Thanks. Should I use both hooks, or is guard-cd.sh enough to solve this problem?

Guard-cd is all you really need

I ended up installing lpclaude in its entirety. Pretty cool, thanks a lot!

ido50 avatar Sep 10 '25 19:09 ido50

Duplicate https://github.com/anthropics/claude-code/issues/2001 has this comment:

CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR | Return to the original working directory after each Bash command

Set this to 1 in settings then claude code always returns - and somehow it says to bash that all should be relative - that fixed my issue

fsc-eriker avatar Sep 11 '25 11:09 fsc-eriker

My problem was fixed by removing the line cd $HOME from my ~/.profile. It seems that if you cd in your ~/.profile , ~/.bashrc or related scripts, Claude gets lost.

leonbloy avatar Sep 15 '25 14:09 leonbloy

@leonbloy That's on you, but this problem is generally unrelated to that. Claude genuinely forgets what it did a few seconds ago.

fsc-eriker avatar Sep 15 '25 16:09 fsc-eriker

I don't understand why the cwd isn't just part of the latest user prompt. I run into this issue pretty constantly

vanakema avatar Sep 25 '25 20:09 vanakema

Claude is constantly doing stuff like this to me, it really should be addressed - it adds a lot of delay to processing as it spins around 3 times to figure out where it is in the directory structure constantly:

Now let me test that one of the scripts works from its new location: ● Bash(cd scripts && bash -n quick_test.sh && echo "Script syntax check passed") ⎿ Script syntax check passed ● Let me verify the path resolution works: ● Bash(cd scripts && dirname "$PWD/quick_test.sh" && echo "../build from scripts goes to: $(cd ../build && pwd)") ⎿ Error: /bin/bash: line 1: cd: scripts: No such file or directory ● Bash(ls -la scripts/) ⎿ Error: ls: cannot access 'scripts/': No such file or directory ● Let me check where the files actually are: ● Bash(find . -name "quick_test.sh" -o -name "run_game.sh" -o -name "run_vortex.sh" 2>/dev/null) ⎿ ./quick_test.sh ./run_game.sh ./run_vortex.sh ● It seems the git mv command moved them to the current directory instead of scripts/. Let me fix this: ● Bash(mkdir -p scripts && git mv quick_test.sh scripts/ && git mv run_game.sh scripts/ && git mv run_vortex.sh scripts/) ⎿ (No content) ⎿ Interrupted · What should Claude do instead? > Check if you are in the scripts directory ● You're right! Let me check my current working directory: ● Bash(pwd) ⎿ /home/viblio/coding_projects/mm_claude/scripts ● Ah yes, I'm in the scripts directory! That explains why the earlier git mv commands moved the files here. Let me verify the scripts are now in the right place: (edited)

digitalmacgyver avatar Sep 29 '25 00:09 digitalmacgyver

This is one of the top issues with Claude; it severely degrades user experience, wastes time and money

mbUSC avatar Sep 30 '25 03:09 mbUSC

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 09 '25 10:12 github-actions[bot]

My subjective experience is that this has improved somewhat, but it is still a problem.

fsc-eriker avatar Dec 09 '25 11:12 fsc-eriker

Still a problem for me - see #14326

corneliusroemer avatar Dec 17 '25 12:12 corneliusroemer

I enabled CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR and so far had 0 issues of Claude being confused by the directory not changing - so maybe that should just be the default? Perhaps with an explicit /cd and/or Chdir tool for when changing the directory is intentional?

ANogin avatar Dec 18 '25 19:12 ANogin