Command Execution in Incorrect Directory When Running npx nx run-many via Claude Code

Status Fixed / completed
Maintainer reply ✓ Yes — wolffiex
Activity 13 comments · opened May 29, 2025 · closed Jun 5, 2025
💡 Likely answer: A maintainer (wolffiex, collaborator) responded on this thread — see the highlighted reply below.

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

View original on GitHub ↗

13 Comments

thlandgraf · 1 year ago

... 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)

staff0rd · 1 year ago

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.

thlandgraf · 1 year ago
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.

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.

wolffiex collaborator · 1 year ago

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

thlandgraf · 1 year ago
> 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

wolffiex collaborator · 1 year ago

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?

thlandgraf · 1 year ago
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.

wolffiex collaborator · 1 year ago

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

thlandgraf · 1 year ago

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.
````
thlandgraf · 1 year ago

... yeah, in .zlogin, i had a cd - thanks a lot

LaurensRietveld · 1 year ago
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.

Sorry for poluting this thread, but you're a lifesafer. This was my issue as well. Wouldn't have found this out otherwise

jbasdf · 1 year ago

If you are using autoenv it overrides "cd" and you will experience this issue. I uninstalled autoenv and that fixed it for me.

github-actions[bot] · 1 year ago

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.