Compare commits

...
Author SHA1 Message Date
patrickandClaude Opus 4.7 303668b9bb fix(init): make --agent cursor truly standalone (#213)
`rtk init -g --agent cursor` still tried to write ~/.claude/RTK.md
because install_claude was derived from `!opencode` only, ignoring the
explicit non-Claude agent selection. Users with no prior Claude Code
install hit:

    Failed to write RTK.md: ~/.claude/RTK.md: No such file or directory

Fix:
- Route install_claude through a `should_install_claude(opencode, agent)`
  helper so `--agent cursor|windsurf|cline|kilocode|antigravity` skips
  the Claude artifacts (RTK.md, CLAUDE.md patch, settings.json hook).
  `--agent claude` and no-agent default continue to install Claude.
- Have `patch_cursor_hooks_json` create `~/.cursor/` on demand so
  Cursor-only users without that directory don't fail.

Tests:
- `test_should_install_claude_routing`: exhaustive matrix over
  (opencode, agent) combinations.
- `test_patch_cursor_hooks_json_creates_missing_parent_dir`: regression
  guard ensuring the parent directory is auto-created.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 19:26:49 +02:00
3 changed files with 83 additions and 3 deletions

No files matched your search

Generated
+1 -1
View File
@@ -892,7 +892,7 @@ dependencies = [
[[package]]
name = "rtk"
version = "0.36.0"
version = "0.34.3"
dependencies = [
"anyhow",
"automod",
+42 -1
View File
@@ -310,7 +310,8 @@ pub fn run(
}
}
// Cursor hooks (additive, installed alongside Claude Code)
// Cursor hooks: standalone when `--agent cursor` is the only target,
// additive when Claude Code is also being installed.
if install_cursor {
install_cursor_hooks(ctx)?;
}
@@ -2366,6 +2367,16 @@ fn patch_cursor_hooks_json(path: &Path, ctx: InitContext) -> Result<bool> {
}
}
// Ensure parent directory exists (Cursor-only users may not have ~/.cursor yet)
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).with_context(|| {
format!(
"Failed to create Cursor config directory: {}",
parent.display()
)
})?;
}
// Atomic write
atomic_write(path, &serialized)?;
@@ -4127,6 +4138,36 @@ mod tests {
assert!(json_content["hooks"]["afterFileEdit"].is_array());
}
/// Regression test for issue #213: `rtk init -g --agent cursor` must succeed
/// even when the parent directory does not exist yet (Cursor-only users
/// without a prior `~/.cursor/` directory).
#[test]
fn test_patch_cursor_hooks_json_creates_missing_parent_dir() {
let temp = TempDir::new().unwrap();
let hooks_path = temp.path().join(".cursor").join(HOOKS_JSON);
assert!(!hooks_path.parent().unwrap().exists());
let patched = patch_cursor_hooks_json(&hooks_path, InitContext::default()).unwrap();
assert!(patched, "hooks.json should have been created");
assert!(
hooks_path.exists(),
"hooks.json file must exist after patch"
);
assert!(
hooks_path.parent().unwrap().exists(),
"parent .cursor/ directory must be created"
);
let content = fs::read_to_string(&hooks_path).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
assert_eq!(
parsed["hooks"]["preToolUse"][0]["command"],
CURSOR_HOOK_COMMAND
);
assert_eq!(parsed["hooks"]["preToolUse"][0]["matcher"], "Shell");
}
#[test]
fn test_remove_cursor_hook_from_json() {
let mut json_content = serde_json::json!({
+40 -1
View File
@@ -47,6 +47,18 @@ pub enum AgentTarget {
Antigravity,
}
/// Decide whether `rtk init` should install Claude Code artifacts (RTK.md,
/// CLAUDE.md patch, settings.json hook). Claude is installed when the user did
/// not opt into OpenCode and either did not specify `--agent` or asked for
/// `--agent claude` explicitly. Regression guard for issue #213: `--agent
/// cursor` previously still wrote ~/.claude/RTK.md, breaking Cursor-only users.
fn should_install_claude(opencode: bool, agent: Option<AgentTarget>) -> bool {
if opencode {
return false;
}
matches!(agent, None | Some(AgentTarget::Claude))
}
#[derive(Parser)]
#[command(
name = "rtk",
@@ -1803,8 +1815,8 @@ fn run_cli() -> Result<i32> {
hooks::init::run_antigravity_mode(ctx)?;
} else {
let install_opencode = opencode;
let install_claude = !opencode;
let install_cursor = agent == Some(AgentTarget::Cursor);
let install_claude = should_install_claude(opencode, agent);
let install_windsurf = agent == Some(AgentTarget::Windsurf);
let install_cline = agent == Some(AgentTarget::Cline);
@@ -3049,4 +3061,31 @@ mod tests {
_ => panic!("Expected Commands::Npx for unknown tool"),
}
}
/// Regression test for issue #213: `rtk init -g --agent cursor` must not
/// drag in the Claude Code install path.
#[test]
fn test_should_install_claude_routing() {
// Default (no opencode, no agent) => install Claude
assert!(should_install_claude(false, None));
// OpenCode plugin only
assert!(!should_install_claude(true, None));
// Explicit non-Claude agent: never install Claude
assert!(!should_install_claude(false, Some(AgentTarget::Cursor)));
assert!(!should_install_claude(false, Some(AgentTarget::Windsurf)));
assert!(!should_install_claude(false, Some(AgentTarget::Cline)));
assert!(!should_install_claude(false, Some(AgentTarget::Kilocode)));
assert!(!should_install_claude(
false,
Some(AgentTarget::Antigravity)
));
// Explicit Claude agent => install Claude
assert!(should_install_claude(false, Some(AgentTarget::Claude)));
// OpenCode combined with any agent => skip Claude
assert!(!should_install_claude(true, Some(AgentTarget::Cursor)));
}
}