claude-code
claude-code copied to clipboard
Command Execution in Incorrect Directory When Running npx nx run-many via Claude Code
Hi,
I’m encountering an issue with Claude Code executing commands in the wrong directory.
Setup: • I’m in the root of my Nx monorepo. • A CLAUDE.md file is present in this directory.
Issue:
When I instruct Claude Code to run:
npx nx run-many --target=test --all
It executes the command, but I receive the following error:
NX The current directory isn't part of an Nx workspace.
Upon investigation, it appears that the command is being executed from my home directory ($HOME) instead of the project root.
Steps Taken: • Confirmed that I’m running Claude Code from the project root. • Verified the presence of CLAUDE.md in the current directory. • Checked that npx nx run-many --target=test --all works as expected when run directly in the terminal from the project root.
Expected Behavior:
Claude Code should execute commands from the directory it’s launched in, respecting the current working directory context.
Additional Information: • Claude Code version: 1.0.5 • Operating System: MacOS 15.4
Is there a known workaround or configuration to ensure commands are executed in the correct directory?
Thank you for your assistance.
Best regards,
Thomas
... I want to share an observation. It seems that the command executed by claude code is executed in my home directory instead of the project root (from where i launched claude code)
In my case this appears to be because claude code starts bash with my user profile, (.profile) which has a cd /to/my/dir in it. I worked around this by checking $CLAUDECODE is not set when that cd happens in my profile, but probably it would be better if claude code ran bash with --noprofile or similar.
In my case this appears to be because claude code starts bash with my user profile, (
.profile) which has acd /to/my/dirin it. I worked around this by checking$CLAUDECODEis not set when thatcdhappens in my profile, but probably it would be better if claude code ran bash with--noprofileor similar.
Hmm, not in my case... when I cd /tmp then bash then pwd ... I get /tmp as result - means the bash rc files do not cd anywhere.
can you ask claude to print the cwd using the shell tool? the current directory should stick to where you started claude regardless of what your shell config does
> execute the pwd command with bash
⏺ I'll execute the pwd command to show the current working directory.
⏺ Bash(pwd)
⎿
/Users/tl
⎿ Shell cwd was reset to /Users/tl/work/MYPROJECT
⏺ /Users/tl/work/MYPROJECT
... /Users/tl is my homedir ... therefore I think thats the cause of my problems.
For crosscheck I asked for:
/Users/tl
⎿ Shell cwd was reset to /Users/tl/work/MYPROJECT
⏺ /Users/tl/work/MYPROJECT
> now execute "echo $PWD"
⏺ Bash(echo $PWD)
⎿
/Users/tl
⎿ Shell cwd was reset to /Users/tl/work/MYPROJECT
⏺ /Users/tl
... IMHO same situation - hope this helps.
best,
Thomas
I'm guessing that your bashrc or zshrc has a cd command in it? If so can you condition that on CLAUDE_CODE env var not being set?
grep cd .*rc
.zshrc:# Make `cd` from a vscode terminal go to the workspace root
.zshrc: alias cd="HOME=\"${VSCODE_WS}\" cd"
which comes from
if [[ -v VSCODE_WS ]] && [[ "$VSCODE_WS" != '${workspaceFolder}' ]]; then
alias cd="HOME=\"${VSCODE_WS}\" cd"
fi
... that the only occurence. Removing that does not alter the situation.
if this is a mac, the cd command might also appear in ~/.bash_profile OR ~/.bash_login OR ~/.profile for bash or ~/.zprofile OR ~/.zlogin for zsh
maybe this helps:
which cd
cd: aliased to HOME="/Users/tl/work/MYPROJECT" cd
then in claude cli:
> run "which cd" as a bash command
⏺ I'll run the which cd command for you.
⏺ Bash(which cd)
⎿
cd: shell built-in command
⎿ Shell cwd was reset to /Users/tl/work/MYPROJECT
⏺ The cd command is a shell built-in command, not an external executable file, which is why which doesn't return a file path for it.
... yeah, in .zlogin, i had a cd - thanks a lot
In my case this appears to be because claude code starts bash with my user profile, (
.profile) which has acd /to/my/dirin it. I worked around this by checking$CLAUDECODEis not set when thatcdhappens in my profile, but probably it would be better if claude code ran bash with--noprofileor similar.
Sorry for poluting this thread, but you're a lifesafer. This was my issue as well. Wouldn't have found this out otherwise
If you are using autoenv it overrides "cd" and you will experience this issue. I uninstalled autoenv and that fixed it for me.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.