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

[BUG] Claude doesnt use the right pwd

Open tjerkw opened this issue 8 months ago • 5 comments

Claude keeps changing the working directory

➜ tebi3 [tjerk/dashboard-in-app) ✗]claude ╭───────────────────────────────────────────────────╮ │ ✻ Welcome to Claude Code research preview! │ │ │ │ /help for help, /status for your current setup │ │ │ │ cwd: /Users/tjerkw/dev/tebi3 │ │ │ │ Account: API │ ╰───────────────────────────────────────────────────╯

✔ Loaded project memory • /memory

what is your pwd

⏺ I'll check the current working directory.

⏺ Bash(pwd)… ⎿  /Users/tjerkw/dev/tebi ⎿ Shell cwd was reset to /Users/tjerkw/dev/tebi3

⏺ /Users/tjerkw/dev/tebi3

tjerkw avatar May 07 '25 13:05 tjerkw

I've also had similar problems where the path actually gets set to a (sometimes deeply) nested folder during Claude's work and doesn't get reset when claude stops.

> Specifically rearding telemetry, what about the "workflow context" and the "analyze-telemetry.ts" files?

⏺ Let me search for those files to examine them.

⏺ Search(pattern: "**/analyze-telemetry.ts")…
  ⎿  Error: No files found

⏺ Search(pattern: "**/ai-framework/**/*workflow*.ts")…
  ⎿  Error: No files found

⏺ Task(Search for workflow context and telemetry)…
  ⎿  Done (13 tool uses · 41.3k tokens · 41.9s)

⏺ Search(pattern: "**/ai-framework/types/telemetry.ts")…
  ⎿  Error: No files found

⏺ Search(pattern: "**/ai-framework/workflow.ts")…
  ⎿  Error: No files found
  ⎿  Interrupted by user

! pwd
  ⎿  /Users/chadfurman/FOSSA/f/o/l/d/e/r

! cd ../../../../../
  ⎿  Changed directory to /Users/chadfurman/FOSSA/f/

> please try your search again.  You were in the wrong folder

⏺ I'll conduct a more thorough search from the correct directory.

⏺ Bash(pwd)…
  ⎿  /Users/chadfurman/fossa/f
  ⎿
     Shell cwd was reset to /Users/chadfurman/FOSSA/f

⏺ Search(pattern: "**/ai-framework/types/telemetry.ts")…
  ⎿  Found 1 file (ctrl+r to expand)

⏺ Read(o/src/ai-framework/types/telemetry.ts)…
  ⎿  Read 33 lines (ctrl+r to expand)

⏺ Search(pattern: "**/ai-framework/**/workflow*.ts")…
  ⎿  Found 3 files (ctrl+r to expand)

Note after I told Claude it was in the wrong folder, it reset itself. However, I had already moved to the right folder with !cd lol

chad-fossa avatar May 07 '25 13:05 chad-fossa

Is there anything in your ~/.zshrc or ~/.bashrc that cd's into a different directory, by any chance?

bcherny avatar May 07 '25 15:05 bcherny

This was indeed the case. Removing that solved the problem. But this is a quite common thing to have right?

-- Tjerk Wolterink

On Wed, May 7, 2025 at 17:38 Boris Cherny @.***> wrote:

bcherny left a comment (anthropics/claude-code#989) https://github.com/anthropics/claude-code/issues/989#issuecomment-2859084511

Is there anything in your ~/.zshrc or ~/.bashrc that cd's into a different directory, by any chance?

— Reply to this email directly, view it on GitHub https://github.com/anthropics/claude-code/issues/989#issuecomment-2859084511, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGH5MLW2U3OL2AYOGXL2B325ISHJAVCNFSM6AAAAAB4T3JXGOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQNJZGA4DINJRGE . You are receiving this because you authored the thread.Message ID: @.***>

tjerkw avatar May 07 '25 19:05 tjerkw

Okay, I ran into this too. I'm in WSL and in my .profile I had a CD that went into my sites (projects) directory. This was screwing up both Claude Code and I believe it also messed up OpenAI's Codex when I trialed it.

Gemini 2.5 pro gave me an elegant solution -- although I can't explain it myself:

# Only cd if this is a top-level interactive shell
# This is an example, might need adjustment for your specific terminal/setup
if [ "$SHLVL" -eq 1 ] && [ -t 0 ]; then # SHLVL 1 and connected to a terminal
    # You could also try to check $TERM or parent process
    # e.g. parent_cmd=$(ps -o comm= -p $PPID)
    # if [[ "$parent_cmd" == "gnome-terminal-"* || "$parent_cmd" == "login" ]]; then
    cd /home/david/sites
    # fi
fi

I verified this works -- I get CD'd into my sites dir when I open a new tab, and Claude Code now correctly respects the appropriate directory.

Sirfrummel avatar May 21 '25 16:05 Sirfrummel

✅ Solution: Early return in .zshrc using Claude environment variables

I asked Claude Code to check for Claude Code environment variables and bingo, he listed:

  • CLAUDECODE=1
  • CLAUDE_CODE_ENTRYPOINT=cli

So I simply asked Claude Code to place an early return on my .zshrc

# Skip shell initialization in Claude Code to avoid conflicts
if [[ -n "$CLAUDECODE" ]]; then
    # Only keep essential PATH and minimal setup
    export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$HOME/.local/bin:$PATH"
    return 0
fi

This is added at the very top of .zshrc (after the shebang/header comment) to ensure early exit before any problematic code runs.

  • No more git errors - Shell prompt doesn't try to check git status (coming from powerlevel10k)
  • Much faster Claude Code commands - Claude doesn't need to load complex zsh configurations, plugins, etc.

adriangalilea avatar May 27 '25 10:05 adriangalilea

@adriangalilea poassible improvement to your fix ... also condition -t 0 is false, so one can split rc file into interactive and non-interactive parts

happy-barney avatar Jul 01 '25 15:07 happy-barney

It looks like this was caused by shell config that changes cwd, but there are many cwd issues with Claude which aren't caused by unusual shell config.

I feel this issue should be closed, and potentially marked as duplicate of #1669 if you still feel strongly that it's a bug. My concern is that another bug, which is similar but has a much narrower scope, can divert attention to the larger issue. I've had so many professional experiences where someone reads something like this first, and then every time the broader issue comes up, they immediately say "yeah but that's caused by people changing cwd in their shell config".. they say this to other people, it gets repeated and spreads, and a quite large issue ends up being widely dismissed as an edge case or user error as people repeat to each other that it only happens in this specific situation, because one bug report involved that specific situation.

adfaklsdjf avatar Oct 09 '25 13:10 adfaklsdjf