From 466f010a58a2676ec564f70856266eed1c0a5259 Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 12 Nov 2026 13:58:43 +0100 Subject: [PATCH] fix(automation): Claude Code install says when the older entry could not be removed `install_claude_code` ignored the result of `claude mcp remove`, so a failed remove (on CI, a freshly written script briefly busy) led to a second "already exists" and an error claiming the entry had been removed. It now reports the failed remove with the command to run instead, and the test covers it. Also the CLI tests for the new `mcp install` subcommand: a stray word after `mcp` is an unrecognized subcommand, and it has its own man page. --- apps/photocraft-cli/tests/it/cli.rs | 4 ++- .../tests/it/man_and_completions.rs | 2 ++ crates/automation/src/agent_link.rs | 28 +++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/photocraft-cli/tests/it/cli.rs b/apps/photocraft-cli/tests/it/cli.rs index 22a988baa5..f0d2ab4195 100644 --- a/apps/photocraft-cli/tests/it/cli.rs +++ b/apps/photocraft-cli/tests/it/cli.rs @@ -163,7 +163,9 @@ fn server_usage_error(args: &[&str], expected: &str) { #[test] fn server_modes_reject_ignored_or_conflicting_options() { for sub in ["mcp", "serve"] { - server_usage_error(&[sub, "stray.psd"], "unexpected argument 'stray.psd'"); + // `mcp` has a subcommand (`mcp install`), so clap reads a stray word there as one. + let stray = if sub == "mcp" { "unrecognized subcommand 'stray.psd'" } else { "unexpected argument 'stray.psd'" }; + server_usage_error(&[sub, "stray.psd"], stray); for flag in ["--control-token", "--control-token-file"] { server_usage_error(&[sub, flag, "unused"], if sub == "mcp" { "requires --bridge" } else { "requires --port" }); } diff --git a/apps/photocraft-cli/tests/it/man_and_completions.rs b/apps/photocraft-cli/tests/it/man_and_completions.rs index 9c1e2aabbf..d23e6f25a6 100644 --- a/apps/photocraft-cli/tests/it/man_and_completions.rs +++ b/apps/photocraft-cli/tests/it/man_and_completions.rs @@ -49,6 +49,8 @@ fn the_example_writes_the_files_the_packages_install() { let mut expected: Vec = completions.map(String::from).into(); expected.push("photocraft-cli.1".into()); expected.extend(subcommands.iter().map(|s| format!("photocraft-cli-{s}.1"))); + // Nested subcommands get their own page too (the packages install every `*.1`). + expected.push("photocraft-cli-mcp-install.1".into()); expected.sort(); assert_eq!(written, expected); std::fs::remove_dir_all(&dir).unwrap(); diff --git a/crates/automation/src/agent_link.rs b/crates/automation/src/agent_link.rs index fb33b998aa..4a0d4bd199 100644 --- a/crates/automation/src/agent_link.rs +++ b/crates/automation/src/agent_link.rs @@ -595,7 +595,18 @@ fn install_claude_code(launch: &McpLaunch, env: &ClientEnv) -> Result None, + Ok(out) => Some(String::from_utf8_lossy(if out.stderr.is_empty() { &out.stdout } else { &out.stderr }).trim().to_string()), + Err(e) => Some(format!("{}: {e}", claude.display())), + }; + if let Some(why) = why_not { + return Err(AutomationError::Other(format!( + "Claude Code already has a `{SERVER_NAME}` entry and removing it failed ({why}); run this in a terminal to replace it: claude mcp remove --scope user {SERVER_NAME} && {by_hand}" + ))); + } replaced = true; result = run(&add); } @@ -882,10 +893,12 @@ mod tests { let log = home.join("calls.txt"); let fail = home.join("fail"); let exists = home.join("exists"); + let keep = home.join("keep"); // `add` fails while an entry exists (as claude's does) or while `fail` is there; `remove` - // removes the entry. + // removes the entry, or fails while `keep` is there. let script = format!( "#!/bin/sh\nprintf 'PATH=%s\\n' \"$PATH\" >> '{log}'\nprintf '%s ' \"$@\" >> '{log}'\nprintf '\\n' >> '{log}'\n\ + if [ \"$2\" = remove ] && [ -e '{keep}' ]; then echo 'locked' >&2; exit 1; fi\n\ if [ \"$2\" = remove ]; then rm -f '{exists}'; exit 0; fi\n\ if [ \"$2\" = get ]; then [ -e '{exists}' ]; exit $?; fi\n\ if [ \"$2\" = add ] && [ -e '{exists}' ]; then echo 'already exists' >&2; exit 1; fi\n\ @@ -893,7 +906,8 @@ mod tests { if [ \"$2\" = add ]; then : > '{exists}'; fi\n", log = log.display(), fail = fail.display(), - exists = exists.display() + exists = exists.display(), + keep = keep.display() ); let claude = bin.join("claude"); std::fs::write(&claude, script).unwrap(); @@ -929,6 +943,14 @@ mod tests { let e = run().unwrap_err().to_string(); assert!(e.contains("boom") && !e.contains("was removed"), "{e}"); assert!(!std::fs::read_to_string(&log).unwrap().contains("mcp remove")); + // An entry exists and removing it fails: the error says so, and does not claim it went. + std::fs::remove_file(&fail).unwrap(); + std::fs::write(&exists, "").unwrap(); + std::fs::write(&keep, "").unwrap(); + let e = run().unwrap_err().to_string(); + assert!(e.contains("removing it failed (locked)") && !e.contains("was removed"), "{e}"); + assert!(e.contains("claude mcp remove --scope user photocraft && claude mcp add"), "{e}"); + assert!(exists.exists(), "the older entry is still there"); let _ = std::fs::remove_dir_all(&home); }