Pass --directory flag to Claude Code agents to prevent worktree escape

Claude Code resolves its project root by walking up from cwd looking
for .git. In worktrees, .git is a file pointing back to the main
checkout, so Claude Code would resolve the main repo as its project
and write files there instead of in the worktree. Adding --directory
explicitly pins it to the worktree path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dave
2026-02-23 18:43:23 +00:00
parent d8e35dbbf8
commit c169cfc9fa

View File

@@ -226,6 +226,11 @@ impl ProjectConfig {
args.push(render(sp));
}
// Explicitly set the project directory so Claude Code doesn't
// resolve the main checkout via .git worktree indirection.
args.push("--directory".to_string());
args.push(worktree_path.to_string());
Ok((command, args, prompt))
}
}
@@ -390,7 +395,8 @@ max_turns = 0
.render_agent_args("/tmp/wt", "42_foo", None, None)
.unwrap();
assert_eq!(cmd, "claude");
assert!(args.is_empty());
assert!(args.contains(&"--directory".to_string()));
assert!(args.contains(&"/tmp/wt".to_string()));
assert!(prompt.contains("42_foo"));
}