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

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

Open thlandgraf opened this issue 6 months ago • 3 comments

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

thlandgraf avatar May 29 '25 14:05 thlandgraf

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

thlandgraf avatar Jun 03 '25 08:06 thlandgraf

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.

staff0rd avatar Jun 05 '25 11:06 staff0rd

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.

thlandgraf avatar Jun 05 '25 13:06 thlandgraf

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

wolffiex avatar Jun 05 '25 16:06 wolffiex

> 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

thlandgraf avatar Jun 05 '25 16:06 thlandgraf

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?

wolffiex avatar Jun 05 '25 16:06 wolffiex

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.

thlandgraf avatar Jun 05 '25 17:06 thlandgraf

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

wolffiex avatar Jun 05 '25 17:06 wolffiex

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 avatar Jun 05 '25 17:06 thlandgraf

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

thlandgraf avatar Jun 05 '25 17:06 thlandgraf

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

LaurensRietveld avatar Jul 30 '25 16:07 LaurensRietveld

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

jbasdf avatar Aug 02 '25 21:08 jbasdf

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.

github-actions[bot] avatar Aug 11 '25 14:08 github-actions[bot]