codex apply ignores root --cd and can patch the wrong repository

Open 💬 1 comment Opened Aug 1, 2026 by ShiroKSH

What version of Codex CLI is running?

Current main at 1e85ca0.

What subscription do you have?

N/A — this is a local CLI dispatch bug.

Which model were you using?

N/A.

What platform is your computer?

Darwin 25.5.0 arm64.

What issue are you seeing?

The root -C/--cd option is ignored by codex apply. The downloaded task diff is applied in the process's current directory instead. If both directories are compatible Git checkouts, the command can succeed while modifying the wrong repository.

What steps can reproduce the bug?

  1. Start in Git repository A.
  2. Have a task whose diff applies cleanly to both repository A and repository B.
  3. Run codex -C /path/to/repository-b apply <task-id>.
  4. Observe that the changed files appear in repository A, while repository B remains untouched.

The call path makes the directory loss explicit:

What is the expected behavior?

codex -C /path/to/repository-b apply <task-id> should apply the diff only in repository B, matching the behavior of the root option for the other command paths.

Additional information

The lower-level apply path already accepts an explicit working directory, and test_apply_command_creates_fibonacci_file covers applying into a supplied temporary repository. A focused fix would pass interactive.shared.cwd.clone() (or its dereferenced equivalent) at the CLI dispatch boundary and add a CLI regression with two temporary repositories to prove that only the selected repository changes.

I searched open and closed issues and PRs for apply --cd, codex -C apply, apply wrong directory, and run_apply_command cwd and did not find a matching report.

Would maintainers be open to a focused PR for this fix and regression test?

View original on GitHub ↗

1 Comment

chanse-syres · 25 days ago

I re-made this against main at 2b5bdcf67547860f2e5c5a605009a70026796b2b using two temporary Git repositories.

I launched the command from repository A with -C pointing to repository B.

Before the fix, the task diff was added to repository A. This confirms that the roots working directory value is lost at the Subcommand::Apply dispatch boundary.

The smallest local fix is to change:

run_apply_command(apply_cli, /*cwd*/ None)

to:

run_apply_command(apply_cli, interactive.shared.cwd.clone())

I also added a CLI integration test that:

  1. launches the real codex binary from repository A,
  2. passes repository B through -C,
  3. serves the task diff from a local mocked endpoint,
  4. verifies repository A remains unchanged, and
  5. verifies repository B receives the patch.

The regression test fails before the one-line correction and it passes afterward.

Local verification:

focused codex-cli regression: passed existing lower-level apply test: passed just test -p codex-cli: 340/340 passed just fix -p codex-cli: passed cargo fmt --all -- --check: passed git diff --check: passed

The proposed change is limited to the single line correction and the regression test. I kept it local and have not opened a pull request because external contributions are invite only.

If this approach matches the maintainers’ intended solution, would you be willing to invite a focused PR?