CORE JSC

International Technology Partnership

Roo Tools

Fixing Roo Code's execute_command Tool That Silently Runs Each Command From the Original Directory, Ignoring a Previous cd

A task asks Roo Code to cd into a subdirectory and then run a build or test command there. The cd command appears to succeed, but the very next command fails as if it ran from the project root instead — because, in a very literal sense, it did. Each execute_command call can spin up its own shell process, and nothing about a previous cd survives into the next one unless the terminal session is explicitly reused.

Core JSC Team·December 2, 2026
Roo Codeexecute_commandTerminalDeveloper ToolsTroubleshooting

The Problem

A task instructs Roo Code to move into a subdirectory — a monorepo package, a backend/ or client/ folder — and then run something there, like npm test or cargo build. The cd command Roo Code runs completes without any error. The very next command, which was supposed to run from inside that subdirectory, fails with a "file not found" or "no such file or directory" error, or runs successfully but against the wrong files entirely — behaving exactly as if the cd had never happened, despite it showing a clean success in the previous step.

Why It Happens

Each execute_command call runs in its own shell invocation by default, and a shell process's working directory dies with that process

A shell's current working directory is state that belongs to that one running shell process — it isn't saved anywhere external to it. When a tool runs a command by spawning a shell, letting it execute, and then letting that shell process exit, any cd performed during that command is scoped entirely to the now-dead process. The next command, if it spawns a fresh shell rather than reusing the exact same still-running one, starts back at whatever directory it was configured to start in — almost always the workspace root — with absolutely no memory of the previous command's cd.

The two commands look like one continuous session in the task transcript, which hides that they're actually two independent shell invocations

In the task's visible history, "cd packages/api" followed by "npm test" reads as a single, obviously sequential pair of steps — exactly how a person typing into one terminal window would experience it. That visual continuity is misleading: unless the execution layer is explicitly configured to keep one shell process alive and route every subsequent command into that same process's stdin, each line is its own independent, short-lived shell that starts fresh. The transcript gives no visual signal for which one actually happened.

A command that chains cd and the real command together with && appears to work, masking the underlying issue until a later command is split apart

Writing cd packages/api && npm test as a single command genuinely works, because both the cd and the following command execute inside the same one shell invocation. This creates a false impression that cd "works fine" in general, so a later multi-step task that separates them into two distinct command calls — often because an intermediate step was inserted, like checking a file exists first — reintroduces the exact same failure, now harder to spot because the working && pattern was seen earlier in the same task.

The Fix

1. Chain a directory change and everything that depends on it into one command, every time

# Fragile across two separate execute_command calls:
cd packages/api
npm test

# Reliable as one call:
cd packages/api && npm test

Whenever a command's correctness depends on a prior cd, the safest rule is to never let that dependency cross a command boundary at all — combine them with && so they're guaranteed to run in the exact same shell process, regardless of how the underlying tool happens to manage sessions between separate calls.

2. For a sequence of several commands in one directory, repeat the cd (or an absolute path) in each one rather than relying on a single earlier cd

# Each call stands on its own, no shared state assumed:
cd packages/api && npm install
cd packages/api && npm run lint
cd packages/api && npm test

This is slightly more repetitive than a single cd followed by several bare commands, but it makes each individual execute_command call self-contained and correct in isolation — it doesn't depend on whether the tool happens to be reusing a persistent terminal for this particular task.

3. Prefer passing an explicit working directory to the command itself when the tool supports it

# npm's --prefix avoids depending on shell cwd entirely:
npm --prefix packages/api test

# git's -C flag does the same for git commands:
git -C packages/api status

Many common CLIs accept their target directory as an explicit flag rather than depending on the shell's current working directory at all. Using that flag removes the ambiguity completely — the command is correct no matter what directory the shell process it runs in happens to start from.

4. If the tool exposes a setting for a persistent or reused terminal session, verify it's actually enabled before relying on cd to carry over

Check the tool's terminal/shell integration settings for
an option like "reuse existing terminal" or "persistent
shell session" — if it's off, every execute_command call
is assumed to be an independent shell by default.

Some setups do keep one shell process alive across an entire task specifically so that state like cd and exported environment variables persists naturally between commands. Confirming whether that behavior is actually active — rather than assuming it is because it worked once — avoids re-discovering this issue days later in a different task.

Why This Works

Treating every execute_command call as its own independent shell, with no guaranteed memory of any previous call, matches what's actually happening under most default configurations — so designing commands around that assumption makes them correct regardless of whether the underlying tool happens to reuse a session or not. Chaining a cd with its dependent command via &&, or passing an explicit working-directory flag, both eliminate the dependency on cross-call state entirely rather than hoping it's preserved.

Conclusion

A cd that appears to succeed but has no effect on the command that follows it is almost never a bug in the directory-change command itself — it's a sign that the two commands ran in two separate, independent shell processes, each starting from the same default directory. Chaining directory changes together with the commands that depend on them, repeating the cd in every command of a sequence rather than just the first, and preferring a tool's own explicit working-directory flag where one exists all remove the dependency on shared shell state that caused the failure in the first place.