From 9ac7508d3ee24f55f9a8ebd45b436c9c840b5713 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:38:04 -0800 Subject: [PATCH] fix: treat an empty CARGO_HOME as unset An empty CARGO_HOME made terminal::installed look for Cargo's bin folder at the relative path `bin`, so a program installed with `cargo install` into ~/.cargo/bin was reported as an install-script copy: `graphicle update` printed the install script command and `graphicle uninstall` said to rm the file instead of `cargo uninstall graphicle-runtime`. Cargo itself treats an empty CARGO_HOME as unset and uses ~/.cargo. Signed-off-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> --- crates/graphicle/src/terminal.rs | 1 + crates/graphicle/tests/it/ux_terminal.rs | 84 +++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/crates/graphicle/src/terminal.rs b/crates/graphicle/src/terminal.rs index c7ef5911..943b2e79 100644 --- a/crates/graphicle/src/terminal.rs +++ b/crates/graphicle/src/terminal.rs @@ -1064,6 +1064,7 @@ fn shell_quote(value: &str) -> String { /// in a container. `graphicle update` and `graphicle uninstall` both use it. pub fn installed(exe: &Path) -> Install { let cargo_bin = std::env::var_os("CARGO_HOME") + .filter(|value| !value.is_empty()) .map(PathBuf::from) .or_else(|| home_dir().map(|h| h.join(".cargo"))) .map(|cargo| cargo.join("bin")); diff --git a/crates/graphicle/tests/it/ux_terminal.rs b/crates/graphicle/tests/it/ux_terminal.rs index 94851e94..6d5f8d20 100644 --- a/crates/graphicle/tests/it/ux_terminal.rs +++ b/crates/graphicle/tests/it/ux_terminal.rs @@ -89,8 +89,18 @@ impl Sandbox { } /// The program with this sandbox's environment, in `cwd`. fn command(&self, cwd: &Path, args: &[&str], env: &[(&str, &str)]) -> Command { + self.command_of(Path::new(env!("CARGO_BIN_EXE_graphicle")), cwd, args, env) + } + /// [`Sandbox::command`] for `program`. + fn command_of( + &self, + program: &Path, + cwd: &Path, + args: &[&str], + env: &[(&str, &str)], + ) -> Command { let home = self.home(); - let mut command = Command::new(env!("CARGO_BIN_EXE_graphicle")); + let mut command = Command::new(program); command .current_dir(cwd) .arg("--data-dir") @@ -916,6 +926,78 @@ fn update_without_a_feed_says_how_to_update_and_sync_defaults_to_this_folder() { assert!(again.contains("Code graph: 7 nodes"), "{again}"); } +/// Update and uninstall treat an empty CARGO_HOME as unset. +#[test] +fn update_and_uninstall_treat_an_empty_cargo_home_as_unset() { + let sandbox = Sandbox::new(); + let cargo_bin = sandbox.home().join(".cargo").join("bin"); + std::fs::create_dir_all(&cargo_bin).unwrap(); + let program = cargo_bin.join(crate::child_env::program("graphicle")); + if std::fs::hard_link(env!("CARGO_BIN_EXE_graphicle"), &program).is_err() { + std::fs::copy(env!("CARGO_BIN_EXE_graphicle"), &program).unwrap(); + } + for env in [&[][..], &[("CARGO_HOME", "")][..]] { + let update = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &program, + &sandbox.elsewhere(), + &["update"], + env, + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(update["installed"], "cargo", "{update}"); + let commands = update["update"].as_array().unwrap(); + assert!( + commands.iter().any(|command| { + command.as_str() == Some("cargo install --path runtime --locked --force") + }), + "{update}" + ); + } + let custom = sandbox.root.join("cargo"); + std::fs::create_dir_all(custom.join("bin")).unwrap(); + let that_program = custom + .join("bin") + .join(crate::child_env::program("graphicle")); + if std::fs::hard_link(env!("CARGO_BIN_EXE_graphicle"), &that_program).is_err() { + std::fs::copy(env!("CARGO_BIN_EXE_graphicle"), &that_program).unwrap(); + } + let update = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &that_program, + &sandbox.elsewhere(), + &["update"], + &[("CARGO_HOME", custom.to_str().unwrap())], + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(update["installed"], "cargo", "{update}"); + std::fs::create_dir_all(sandbox.data()).unwrap(); + let report = json_of( + &crate::fork_guard::spawn(&mut sandbox.command_of( + &program, + &sandbox.elsewhere(), + &["uninstall"], + &[("CARGO_HOME", "")], + )) + .unwrap() + .wait_with_output() + .unwrap(), + ); + assert_eq!(report["remove_program"]["method"], "cargo", "{report:#}"); + assert!( + report["remove_program"]["command"] + .as_str() + .is_some_and(|command| command.starts_with("cargo uninstall ")), + "{report:#}" + ); +} + #[test] fn open_reuses_a_running_server_and_prints_the_address_without_a_browser() { let sandbox = Sandbox::new();