From 754b48daec040f9d2ec2a28096519800f71016b6 Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Sat, 23 May 2026 02:24:18 +0800 Subject: [PATCH 1/9] refactor: rename delete_rustup_and_cargo_home to clean_cargo_bin --- src/cli/self_update.rs | 6 +++--- src/cli/self_update/unix.rs | 2 +- src/cli/self_update/windows.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 564fe080d8..7f85023463 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -80,7 +80,7 @@ mod shell; #[cfg(unix)] mod unix; #[cfg(unix)] -use unix::{delete_rustup_and_cargo_home, do_add_to_path, do_remove_from_path}; +use unix::{clean_cargo_bin, do_add_to_path, do_remove_from_path}; #[cfg(unix)] pub(crate) use unix::{run_update, self_replace}; @@ -91,7 +91,7 @@ pub use windows::complete_windows_uninstall; #[cfg(all(windows, feature = "test"))] pub use windows::{RegistryGuard, RegistryValueId, USER_PATH, get_path}; #[cfg(windows)] -use windows::{delete_rustup_and_cargo_home, do_add_to_path, do_remove_from_path}; +use windows::{clean_cargo_bin, do_add_to_path, do_remove_from_path}; #[cfg(windows)] pub(crate) use windows::{run_update, self_replace}; @@ -1151,7 +1151,7 @@ pub(crate) fn uninstall( // Delete rustup. This is tricky because this is *probably* // the running executable and on Windows can't be unlinked until // the process exits. - delete_rustup_and_cargo_home(process)?; + clean_cargo_bin(process)?; info!("rustup is uninstalled"); diff --git a/src/cli/self_update/unix.rs b/src/cli/self_update/unix.rs index 05c598f551..0c2e3e6e33 100644 --- a/src/cli/self_update/unix.rs +++ b/src/cli/self_update/unix.rs @@ -47,7 +47,7 @@ pub(crate) fn do_anti_sudo_check(no_prompt: bool, process: &Process) -> Result Result<()> { +pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> { let cargo_home = process.cargo_home()?; utils::remove_dir("cargo_home", &cargo_home) } diff --git a/src/cli/self_update/windows.rs b/src/cli/self_update/windows.rs index 07578e3ec1..417371e2be 100644 --- a/src/cli/self_update/windows.rs +++ b/src/cli/self_update/windows.rs @@ -674,7 +674,7 @@ pub(crate) fn self_replace(process: &Process) -> Result { // // .. augmented with this SO answer // https://stackoverflow.com/questions/10319526/understanding-a-self-deleting-program-in-c -pub(crate) fn delete_rustup_and_cargo_home(process: &Process) -> Result<()> { +pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> { use std::io; use std::ptr; use std::thread; From f2bfa7828c0f57d5e61ca0d54d34901a9643cb5c Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Mon, 8 Jun 2026 23:43:36 +0800 Subject: [PATCH 2/9] refactor(cli/self-update): move `unix::clean_cargo_bin()` to parent module --- src/cli/self_update.rs | 8 +++++++- src/cli/self_update/unix.rs | 5 ----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 7f85023463..406f46c84a 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -80,7 +80,7 @@ mod shell; #[cfg(unix)] mod unix; #[cfg(unix)] -use unix::{clean_cargo_bin, do_add_to_path, do_remove_from_path}; +use unix::{do_add_to_path, do_remove_from_path}; #[cfg(unix)] pub(crate) use unix::{run_update, self_replace}; @@ -1158,6 +1158,12 @@ pub(crate) fn uninstall( Ok(ExitCode::SUCCESS) } +#[cfg(unix)] +fn clean_cargo_bin(process: &Process) -> Result<()> { + let cargo_home = process.cargo_home()?; + utils::remove_dir("cargo_home", &cargo_home) +} + #[derive(Clone, Copy, Debug)] pub(crate) enum SelfUpdatePermission { HardFail, diff --git a/src/cli/self_update/unix.rs b/src/cli/self_update/unix.rs index 0c2e3e6e33..44f073a3f8 100644 --- a/src/cli/self_update/unix.rs +++ b/src/cli/self_update/unix.rs @@ -47,11 +47,6 @@ pub(crate) fn do_anti_sudo_check(no_prompt: bool, process: &Process) -> Result Result<()> { - let cargo_home = process.cargo_home()?; - utils::remove_dir("cargo_home", &cargo_home) -} - pub(crate) fn do_remove_from_path(process: &Process) -> Result<()> { for sh in shell::get_available_shells(process) { let source_bytes = format!("{}\n", sh.source_string(process)?).into_bytes(); From 0f086b30bab777fcd6739d978e4f045238addeb2 Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Mon, 8 Jun 2026 23:58:22 +0800 Subject: [PATCH 3/9] refactor: pass PATH cleanup to `clean_cargo_bin` --- src/cli/self_update.rs | 22 +++++++++++++--------- src/cli/self_update/windows.rs | 14 ++++++++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 406f46c84a..4f8bb3be9d 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -91,7 +91,7 @@ pub use windows::complete_windows_uninstall; #[cfg(all(windows, feature = "test"))] pub use windows::{RegistryGuard, RegistryValueId, USER_PATH, get_path}; #[cfg(windows)] -use windows::{clean_cargo_bin, do_add_to_path, do_remove_from_path}; +use windows::{do_add_to_path, do_remove_from_path}; #[cfg(windows)] pub(crate) use windows::{run_update, self_replace}; @@ -1092,11 +1092,6 @@ pub(crate) fn uninstall( info!("removing cargo home"); - // Remove CARGO_HOME/bin from PATH - if !no_modify_path { - do_remove_from_path(process)?; - } - // Delete everything in CARGO_HOME *except* the rustup bin // First everything except the bin directory @@ -1151,16 +1146,25 @@ pub(crate) fn uninstall( // Delete rustup. This is tricky because this is *probably* // the running executable and on Windows can't be unlinked until // the process exits. - clean_cargo_bin(process)?; + #[cfg(unix)] + clean_cargo_bin(no_modify_path, process)?; + // NOTE: On windows, this is *tricky*, + // the running executable and on Windows can't be unlinked until the process exits. + // see: windows::{complete_windows_uninstall,clean_cargo_bin} + #[cfg(windows)] + windows::clean_cargo_bin(no_modify_path, process)?; info!("rustup is uninstalled"); Ok(ExitCode::SUCCESS) } -#[cfg(unix)] -fn clean_cargo_bin(process: &Process) -> Result<()> { +fn clean_cargo_bin(no_modify_path: bool, process: &Process) -> Result<()> { let cargo_home = process.cargo_home()?; + if !no_modify_path { + do_remove_from_path(process)?; + } + utils::remove_dir("cargo_home", &cargo_home) } diff --git a/src/cli/self_update/windows.rs b/src/cli/self_update/windows.rs index 417371e2be..1391cbfe9d 100644 --- a/src/cli/self_update/windows.rs +++ b/src/cli/self_update/windows.rs @@ -355,9 +355,10 @@ pub fn complete_windows_uninstall(process: &Process) -> Result wait_for_parent()?; - // Now that the parent has exited there are hopefully no more files open in CARGO_HOME - let cargo_home = process.cargo_home()?; - utils::remove_dir("cargo_home", &cargo_home)?; + let no_modify_path = process.var_os(GC_MODIFY_PATH).as_deref() != Some(OsStr::new("1")); + + // Now that the parent has exited there are hopefully no more files open in CARGO_HOME. + super::clean_cargo_bin(no_modify_path, process)?; // Now, run a *system* binary to inherit the DELETE_ON_CLOSE // handle to *this* process, then exit. The OS will delete the gc @@ -674,7 +675,7 @@ pub(crate) fn self_replace(process: &Process) -> Result { // // .. augmented with this SO answer // https://stackoverflow.com/questions/10319526/understanding-a-self-deleting-program-in-c -pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> { +pub(crate) fn clean_cargo_bin(no_modify_path: bool, process: &Process) -> Result<()> { use std::io; use std::ptr; use std::thread; @@ -734,6 +735,7 @@ pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> { }; Command::new(gc_exe) + .env(GC_MODIFY_PATH, if no_modify_path { "0" } else { "1" }) .spawn() .context(CliError::WindowsUninstallMadness)?; @@ -749,6 +751,10 @@ pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> { Ok(()) } +// The rustup-gc executable cannot accept normal function call here, +// so we use env var here, notifying it if we need to remove $CARGO_HOME/bin from $PATH +const GC_MODIFY_PATH: &str = "RUSTUP_GC_MODIFY_PATH"; + #[cfg(any(test, feature = "test"))] pub fn get_path() -> Result> { USER_PATH.get() From 92964af4105d46fad87626c1e6a70d27779c77a3 Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Sun, 7 Jun 2026 00:13:06 +0800 Subject: [PATCH 4/9] doc: add uninstall process overall docstring --- src/cli/self_update.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 4f8bb3be9d..82712aa5f4 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -1048,6 +1048,12 @@ async fn maybe_install_rust(opts: InstallOpts<'_>, cfg: &mut Cfg<'_>) -> Result< Ok(()) } +/// Uninstall process: +/// 1. Remove rustup home. +/// 2. Remove cargo home except $CARGO_HOME/bin +/// 3. Remove other tools in $CARGO_HOME/bin. +/// 4. Remove rustup binary file and links to rustup. +/// 5. Upon successfully removing $CARGO_HOME/bin, clean up $PATH. pub(crate) fn uninstall( no_prompt: bool, no_modify_path: bool, From dd55cc5c3d89b2ef1d80e64e90d5c5f6da868e94 Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Tue, 9 Jun 2026 00:04:18 +0800 Subject: [PATCH 5/9] fix: preserve CARGO_HOME during uninstall --- src/cli/self_update.rs | 43 ++++++++++++++++++++++++---------- src/cli/self_update/windows.rs | 13 +++++----- tests/suite/cli_self_upd.rs | 35 ++++++++++----------------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index 82712aa5f4..d75aeac65f 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -24,15 +24,14 @@ //! During uninstall (`rustup self uninstall`): //! //! * Delete `$RUSTUP_HOME`. -//! * Delete everything in `$CARGO_HOME`, including -//! the rustup binary and its hardlinks +//! * Delete rustup from `$CARGO_HOME`/bin. +//! * Delete `$CARGO_HOME`/bin if it is empty after uninstall. //! //! Deleting the running binary during uninstall is tricky //! and racy on Windows. use std::borrow::Cow; use std::env::{self, consts::EXE_SUFFIX}; -#[cfg(not(windows))] use std::io; use std::io::Write; use std::path::{Component, MAIN_SEPARATOR, Path, PathBuf}; @@ -1050,10 +1049,10 @@ async fn maybe_install_rust(opts: InstallOpts<'_>, cfg: &mut Cfg<'_>) -> Result< /// Uninstall process: /// 1. Remove rustup home. -/// 2. Remove cargo home except $CARGO_HOME/bin -/// 3. Remove other tools in $CARGO_HOME/bin. -/// 4. Remove rustup binary file and links to rustup. -/// 5. Upon successfully removing $CARGO_HOME/bin, clean up $PATH. +/// 2. Remove rustup tool links. +/// 3. Remove rustup binary. +/// 3. Try to remove $CARGO_HOME/bin directory if it's empty. +/// 4. Upon successfully removing $CARGO_HOME/bin, clean up $PATH. pub(crate) fn uninstall( no_prompt: bool, no_modify_path: bool, @@ -1067,7 +1066,8 @@ pub(crate) fn uninstall( let cargo_home = process.cargo_home()?; - if !cargo_home.join(format!("bin/rustup{EXE_SUFFIX}")).exists() { + let rustup_path = cargo_home.join(format!("bin/rustup{EXE_SUFFIX}")); + if !rustup_path.exists() { return Err(CliError::NotSelfInstalled { p: cargo_home }.into()); } @@ -1166,12 +1166,31 @@ pub(crate) fn uninstall( } fn clean_cargo_bin(no_modify_path: bool, process: &Process) -> Result<()> { - let cargo_home = process.cargo_home()?; - if !no_modify_path { - do_remove_from_path(process)?; + let cargo_bin_path = process.cargo_home()?.join("bin"); + let rustup_path = cargo_bin_path.join(format!("rustup{EXE_SUFFIX}")); + + utils::remove_file("rustup_bin", &rustup_path)?; + + let cargo_bin_path_display = cargo_bin_path.display(); + info!("removing empty cargo bin directory `{cargo_bin_path_display}`"); + + let Err(e) = fs::remove_dir(&cargo_bin_path) else { + if !no_modify_path { + info!("removing cargo bin directory `{cargo_bin_path_display}` from $PATH"); + do_remove_from_path(process)?; + } + + return Ok(()); + }; + + if e.kind() == io::ErrorKind::DirectoryNotEmpty { + warn!("keeping non-empty cargo bin directory `{cargo_bin_path_display}`"); + + return Ok(()); } - utils::remove_dir("cargo_home", &cargo_home) + Err(e) + .with_context(|| format!("failed to remove cargo bin directory `{cargo_bin_path_display}`")) } #[derive(Clone, Copy, Debug)] diff --git a/src/cli/self_update/windows.rs b/src/cli/self_update/windows.rs index 1391cbfe9d..875f7deece 100644 --- a/src/cli/self_update/windows.rs +++ b/src/cli/self_update/windows.rs @@ -348,7 +348,7 @@ fn has_windows_sdk_libs(process: &Process) -> bool { false } -/// Run by rustup-gc-$num.exe to delete CARGO_HOME +/// Run by rustup-gc-$num.exe to delete rustup binary #[tracing::instrument(level = "trace")] pub fn complete_windows_uninstall(process: &Process) -> Result { use std::process::Stdio; @@ -357,7 +357,8 @@ pub fn complete_windows_uninstall(process: &Process) -> Result let no_modify_path = process.var_os(GC_MODIFY_PATH).as_deref() != Some(OsStr::new("1")); - // Now that the parent has exited there are hopefully no more files open in CARGO_HOME. + // Clean up CARGO_HOME/bin if it's empty now + // On success, also remove it from $PATH. super::clean_cargo_bin(no_modify_path, process)?; // Now, run a *system* binary to inherit the DELETE_ON_CLOSE @@ -645,9 +646,9 @@ pub(crate) fn self_replace(process: &Process) -> Result { Ok(utils::ExitCode(0)) } -// The last step of uninstallation is to delete *this binary*, -// rustup.exe and the CARGO_HOME that contains it. On Unix, this -// works fine. On Windows you can't delete files while they are open, +// The last step of uninstallation is to delete *this binary*, rustup.exe. +// On Unix, this works fine. +// On Windows you can't delete files while they are open, // like when they are running. // // Here's what we're going to do: @@ -660,7 +661,7 @@ pub(crate) fn self_replace(process: &Process) -> Result { // processes created with the option to inherit handles // will also keep them open. // - Run the gc exe, which waits for the original rustup.exe -// process to close, then deletes CARGO_HOME. This process +// process to close, then deletes rustup.exe. This process // has inherited a FILE_FLAG_DELETE_ON_CLOSE handle to itself. // - Finally, spawn yet another system binary with the inherit handles // flag, so *it* inherits the FILE_FLAG_DELETE_ON_CLOSE handle to diff --git a/tests/suite/cli_self_upd.rs b/tests/suite/cli_self_upd.rs index f821659935..a20c176335 100644 --- a/tests/suite/cli_self_upd.rs +++ b/tests/suite/cli_self_upd.rs @@ -259,13 +259,13 @@ async fn uninstall_works_if_rustup_home_doesnt_exist() { } #[tokio::test] -async fn uninstall_deletes_cargo_home() { +async fn uninstall_keeps_cargo_home() { let cx = setup_empty_installed().await; cx.config .expect(["rustup", "self", "uninstall", "-y"]) .await .is_ok(); - assert!(!cx.config.cargodir.exists()); + assert!(cx.config.cargodir.exists()); } #[tokio::test] @@ -300,26 +300,7 @@ async fn uninstall_self_delete_works() { assert!(out.status.success()); assert!(!rustup.exists()); - assert!(!cx.config.cargodir.exists()); - - let rustc = cx.config.cargodir.join(format!("bin/rustc{EXE_SUFFIX}")); - let rustdoc = cx.config.cargodir.join(format!("bin/rustdoc{EXE_SUFFIX}")); - let cargo = cx.config.cargodir.join(format!("bin/cargo{EXE_SUFFIX}")); - let rust_lldb = cx - .config - .cargodir - .join(format!("bin/rust-lldb{EXE_SUFFIX}")); - let rust_gdb = cx.config.cargodir.join(format!("bin/rust-gdb{EXE_SUFFIX}")); - let rust_gdbgui = cx - .config - .cargodir - .join(format!("bin/rust-gdbgui{EXE_SUFFIX}")); - assert!(!rustc.exists()); - assert!(!rustdoc.exists()); - assert!(!cargo.exists()); - assert!(!rust_lldb.exists()); - assert!(!rust_gdb.exists()); - assert!(!rust_gdbgui.exists()); + assert!(cx.config.cargodir.exists()); } // On windows rustup self uninstall temporarily puts a rustup-gc-$randomnumber.exe @@ -347,7 +328,15 @@ async fn uninstall_doesnt_leave_gc_file() { fn ensure_empty(dir: &Path) -> Result<(), GcErr> { let garbage = fs::read_dir(dir) .unwrap() - .map(|d| d.unwrap().path().to_string_lossy().to_string()) + .filter_map(|entry| { + let path = entry.unwrap().path(); + let name = path.file_name()?.to_str()?; + // On Windows, this binary is cleaned up on exit + if !(name.starts_with("rustup-gc-") && name.ends_with(EXE_SUFFIX)) { + return None; + } + Some(path.to_string_lossy().to_string()) + }) .collect::>(); match garbage.len() { 0 => Ok(()), From 48307ed2e585fa66feded49ecfe2e55ae784403b Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Tue, 9 Jun 2026 00:06:26 +0800 Subject: [PATCH 6/9] fix: remove rustup proxy links during uninstall --- src/cli/self_update.rs | 79 +++++++++++------------------------------- 1 file changed, 20 insertions(+), 59 deletions(-) diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index d75aeac65f..9e067df7bc 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -24,7 +24,7 @@ //! During uninstall (`rustup self uninstall`): //! //! * Delete `$RUSTUP_HOME`. -//! * Delete rustup from `$CARGO_HOME`/bin. +//! * Delete rustup tool proxy binaries from `$CARGO_HOME`/bin. //! * Delete `$CARGO_HOME`/bin if it is empty after uninstall. //! //! Deleting the running binary during uninstall is tricky @@ -46,7 +46,7 @@ use clap::ValueEnum; use clap::builder::PossibleValue; use clap_cargo::style::{GOOD, WARN}; use itertools::Itertools; -use same_file::Handle; +use same_file::{Handle, is_same_file}; use serde::{Deserialize, Serialize}; use tracing::{error, info, trace, warn}; @@ -1096,62 +1096,8 @@ pub(crate) fn uninstall( utils::remove_dir("rustup_home", &rustup_dir)?; } - info!("removing cargo home"); - - // Delete everything in CARGO_HOME *except* the rustup bin - - // First everything except the bin directory - let diriter = fs::read_dir(&cargo_home).map_err(|e| CliError::ReadDirError { - p: cargo_home.clone(), - source: e, - })?; - for dirent in diriter { - let dirent = dirent.map_err(|e| CliError::ReadDirError { - p: cargo_home.clone(), - source: e, - })?; - if dirent.file_name().to_str() != Some("bin") { - if dirent.path().is_dir() { - utils::remove_dir("cargo_home", &dirent.path())?; - } else { - utils::remove_file("cargo_home", &dirent.path())?; - } - } - } - - // Then everything in bin except rustup and tools. These can't be unlinked - // until this process exits (on windows). - let tools = TOOLS - .iter() - .chain(DUP_TOOLS.iter()) - .map(|t| format!("{t}{EXE_SUFFIX}")); - let tools: Vec<_> = tools.chain(vec![format!("rustup{EXE_SUFFIX}")]).collect(); - let bin_dir = cargo_home.join("bin"); - let diriter = fs::read_dir(&bin_dir).map_err(|e| CliError::ReadDirError { - p: bin_dir.clone(), - source: e, - })?; - for dirent in diriter { - let dirent = dirent.map_err(|e| CliError::ReadDirError { - p: bin_dir.clone(), - source: e, - })?; - let name = dirent.file_name(); - let file_is_tool = name.to_str().map(|n| tools.iter().any(|t| *t == n)); - if file_is_tool == Some(false) { - if dirent.path().is_dir() { - utils::remove_dir("cargo_home", &dirent.path())?; - } else { - utils::remove_file("cargo_home", &dirent.path())?; - } - } - } - - info!("removing rustup binaries"); - - // Delete rustup. This is tricky because this is *probably* - // the running executable and on Windows can't be unlinked until - // the process exits. + // Remove rustup tool links and executable, then remove `$CARGO_HOME/bin` directory if empty. + // Optionally remove it from $PATH if successfully removed. #[cfg(unix)] clean_cargo_bin(no_modify_path, process)?; // NOTE: On windows, this is *tricky*, @@ -1161,16 +1107,31 @@ pub(crate) fn uninstall( windows::clean_cargo_bin(no_modify_path, process)?; info!("rustup is uninstalled"); - Ok(ExitCode::SUCCESS) } +/// Remove rustup tool links and executable, then remove `$CARGO_HOME/bin` directory if empty. +/// On success, remove it from `$PATH` unless `no_modify_path` is set. +/// If the directory is not empty, emit a warning and return success. fn clean_cargo_bin(no_modify_path: bool, process: &Process) -> Result<()> { let cargo_bin_path = process.cargo_home()?.join("bin"); let rustup_path = cargo_bin_path.join(format!("rustup{EXE_SUFFIX}")); + // Remove rustup tool links + let proxy_paths = TOOLS + .iter() + .chain(DUP_TOOLS.iter()) + .map(|tool| cargo_bin_path.join(format!("{tool}{EXE_SUFFIX}"))); + for proxy_path in proxy_paths { + if is_same_file(&proxy_path, &rustup_path).unwrap_or(false) { + utils::remove_file("rustup tool proxy", &proxy_path)?; + } + } + + // Remove rustup binary utils::remove_file("rustup_bin", &rustup_path)?; + // Remove $CARGO_HOME/bin let cargo_bin_path_display = cargo_bin_path.display(); info!("removing empty cargo bin directory `{cargo_bin_path_display}`"); From ed751b48058a9a26c0784d4f767ef38fcb215e6b Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:06:03 +0800 Subject: [PATCH 7/9] test: cover cargo bin removal during uninstall --- tests/suite/cli_self_upd.rs | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/suite/cli_self_upd.rs b/tests/suite/cli_self_upd.rs index a20c176335..e34d452411 100644 --- a/tests/suite/cli_self_upd.rs +++ b/tests/suite/cli_self_upd.rs @@ -268,6 +268,37 @@ async fn uninstall_keeps_cargo_home() { assert!(cx.config.cargodir.exists()); } +#[tokio::test] +async fn uninstall_removes_empty_cargo_bin() { + let cx = setup_empty_installed().await; + cx.config + .expect(["rustup", "self", "uninstall", "-y"]) + .await + .is_ok(); + assert!(!cx.config.cargodir.join("bin").exists()); +} + +#[tokio::test] +async fn uninstall_keeps_non_empty_cargo_bin() { + let cx = setup_empty_installed().await; + let cargo_bin = cx.config.cargodir.join("bin"); + + let mock_file = cargo_bin.join(".DS_Store"); + fs::write(&mock_file, "").unwrap(); + + cx.config + .expect(["rustup", "self", "uninstall", "-y"]) + .await + .with_stderr(snapbox::str![[r#" +... +warn: keeping non-empty cargo bin directory `[..]` +... +"#]]) + .is_ok(); + assert!(cargo_bin.exists()); + assert!(mock_file.exists()); +} + #[tokio::test] async fn uninstall_fails_if_not_installed() { let cx = setup_empty_installed().await; @@ -301,6 +332,25 @@ async fn uninstall_self_delete_works() { assert!(out.status.success()); assert!(!rustup.exists()); assert!(cx.config.cargodir.exists()); + + let rustc = cx.config.cargodir.join(format!("bin/rustc{EXE_SUFFIX}")); + let rustdoc = cx.config.cargodir.join(format!("bin/rustdoc{EXE_SUFFIX}")); + let cargo = cx.config.cargodir.join(format!("bin/cargo{EXE_SUFFIX}")); + let rust_lldb = cx + .config + .cargodir + .join(format!("bin/rust-lldb{EXE_SUFFIX}")); + let rust_gdb = cx.config.cargodir.join(format!("bin/rust-gdb{EXE_SUFFIX}")); + let rust_gdbgui = cx + .config + .cargodir + .join(format!("bin/rust-gdbgui{EXE_SUFFIX}")); + assert!(!rustc.exists()); + assert!(!rustdoc.exists()); + assert!(!cargo.exists()); + assert!(!rust_lldb.exists()); + assert!(!rust_gdb.exists()); + assert!(!rust_gdbgui.exists()); } // On windows rustup self uninstall temporarily puts a rustup-gc-$randomnumber.exe From 64736cc290fd3e13d69e800bcb7b8f9eb7c309ba Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:06:30 +0800 Subject: [PATCH 8/9] test: cover cargo bin PATH cleanup during uninstall --- tests/suite/cli_self_upd.rs | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/suite/cli_self_upd.rs b/tests/suite/cli_self_upd.rs index e34d452411..c1e5923152 100644 --- a/tests/suite/cli_self_upd.rs +++ b/tests/suite/cli_self_upd.rs @@ -299,6 +299,60 @@ warn: keeping non-empty cargo bin directory `[..]` assert!(mock_file.exists()); } +#[cfg(not(windows))] +#[tokio::test] +async fn uninstall_removes_path_only_when_bin_removed() { + async fn install(cx: &CliTestContext) { + cx.config + .expect(["rustup-init", "-y", "--default-toolchain", "none"]) + .await + .is_ok(); + } + + fn source_line(cx: &CliTestContext) -> String { + format!( + r#". "{}/env" +"#, + cx.config.cargodir.display() + ) + } + + let cx = CliTestContext::new(Scenario::Empty).await; + install(&cx).await; + let profile = cx.config.homedir.join(".profile"); + assert!( + fs::read_to_string(&profile) + .unwrap() + .contains(&source_line(&cx)) + ); + cx.config + .expect(["rustup", "self", "uninstall", "-y"]) + .await + .is_ok(); + assert!(!cx.config.cargodir.join("bin").exists()); + assert!( + !fs::read_to_string(&profile) + .unwrap() + .contains(&source_line(&cx)) + ); + + let cx = CliTestContext::new(Scenario::Empty).await; + install(&cx).await; + let cargo_bin = cx.config.cargodir.join("bin"); + let profile = cx.config.homedir.join(".profile"); + fs::write(cargo_bin.join("custom-tool"), "").unwrap(); + cx.config + .expect(["rustup", "self", "uninstall", "-y"]) + .await + .is_ok(); + assert!(cargo_bin.exists()); + assert!( + fs::read_to_string(&profile) + .unwrap() + .contains(&source_line(&cx)) + ); +} + #[tokio::test] async fn uninstall_fails_if_not_installed() { let cx = setup_empty_installed().await; From ca2bba6484354b3b89ed1c076b1b425477ff8d65 Mon Sep 17 00:00:00 2001 From: Cloud0310 <60375730+Cloud0310@users.noreply.github.com> Date: Mon, 1 Jun 2026 21:06:53 +0800 Subject: [PATCH 9/9] test: cover Windows cargo bin cleanup during uninstall --- tests/suite/cli_self_upd.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/suite/cli_self_upd.rs b/tests/suite/cli_self_upd.rs index c1e5923152..bd1146073d 100644 --- a/tests/suite/cli_self_upd.rs +++ b/tests/suite/cli_self_upd.rs @@ -353,6 +353,26 @@ async fn uninstall_removes_path_only_when_bin_removed() { ); } +#[cfg(windows)] +#[tokio::test] +async fn windows_complete_uninstall_removes_empty_cargo_bin() { + let cx = setup_empty_installed().await; + let cargo_bin = cx.config.cargodir.join("bin"); + cx.config + .expect(["rustup", "self", "uninstall", "-y"]) + .await + .is_ok(); + + let check = || { + if cargo_bin.exists() { + Err(format!("cargo bin still exists: {}", cargo_bin.display())) + } else { + Ok(()) + } + }; + retry(Fibonacci::from_millis(1).map(jitter).take(23), check).unwrap() +} + #[tokio::test] async fn uninstall_fails_if_not_installed() { let cx = setup_empty_installed().await;