Skip to content

fix: stop deleting $CARGO_HOME during uninstall#4861

Open
Cloud0310 wants to merge 11 commits into
rust-lang:mainfrom
Cloud0310:main
Open

fix: stop deleting $CARGO_HOME during uninstall#4861
Cloud0310 wants to merge 11 commits into
rust-lang:mainfrom
Cloud0310:main

Conversation

@Cloud0310

Copy link
Copy Markdown
Contributor

fix #285
This PR fix the cargo bin nuke and the path removing possilble toutoc issue.
Here's the implementation details:

  • compare cargo bin dir content to rustup binary, if it's a link, remove it
  • then, we remove the rustup binary, on success, we then remove the cargo bin from path

This prevents rustup self uninstall from nuking out the $CARGO_HOME/bin and possible toctou problem of removing up the path.

Comment thread tests/suite/cli_self_upd.rs
@Cloud0310 Cloud0310 marked this pull request as ready for review May 19, 2026 10:37
@Cloud0310

Cloud0310 commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

I think I may need more tests against this PR, this includes:

  • $CARGO_HOME/bin with unrelated files
  • empty bin cleanup test

Should there be more tests? Please let me know @FranciscoTGouveia @rami3l .

Comment thread src/cli/self_update/unix.rs Outdated
Comment thread src/cli/self_update/unix.rs Outdated
Comment thread tests/suite/cli_self_upd.rs Outdated
Comment thread src/cli/self_update.rs Outdated
Comment thread src/cli/self_update/unix.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update.rs Outdated
Comment thread src/cli/self_update.rs Outdated
@Cloud0310

Copy link
Copy Markdown
Contributor Author

I've add more tests about the PR here. Please review.

Comment thread src/cli/self_update/unix.rs Outdated

@rami3l rami3l left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo some tiny nits, nice work :D

View changes since this review

Comment thread src/cli/self_update.rs Outdated
Comment thread tests/suite/cli_self_upd.rs Outdated
Comment thread tests/suite/cli_self_upd.rs
@Cloud0310 Cloud0310 force-pushed the main branch 2 times, most recently from 4a4880a to 971dffa Compare May 22, 2026 08:13
Cloud0310 added a commit to Cloud0310/rustup that referenced this pull request May 22, 2026
Avoid removing $CARGO_HOME when running uninstall cleanup.

Combine all follow-up work for PR rust-lang#4861:

- keep $CARGO_HOME/bin when it is non-empty

- cover cargo bin cleanup behavior in uninstall tests

- update uninstall cleanup comments and related docs

- rename cleanup internals for clearer intent
Cloud0310 added a commit to Cloud0310/rustup that referenced this pull request May 22, 2026
Avoid removing $CARGO_HOME when running uninstall cleanup.

Combine all follow-up work for PR rust-lang#4861:

- keep $CARGO_HOME/bin when it is non-empty

- cover cargo bin cleanup behavior in uninstall tests

- update uninstall cleanup comments and related docs

- rename cleanup internals for clearer intent
@rustbot

This comment has been minimized.

Cloud0310 added a commit to Cloud0310/rustup that referenced this pull request May 22, 2026
Avoid removing $CARGO_HOME when running uninstall cleanup.

Combine all follow-up work for PR rust-lang#4861:

- keep $CARGO_HOME/bin when it is non-empty

- cover cargo bin cleanup behavior in uninstall tests

- update uninstall cleanup comments and related docs

- rename cleanup internals for clearer intent

@djc djc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR needs a bunch of work, it seems to have a lot of changes together in a single commit that are pretty messy. It would be good if the commit history made it clear that there are a sequence of smaller changes being made.

View changes since this review

Comment thread src/cli/self_update/unix.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update/windows.rs Outdated
Comment thread src/cli/self_update/windows.rs
Cloud0310 added a commit to Cloud0310/rustup that referenced this pull request May 22, 2026
Avoid removing $CARGO_HOME when running uninstall cleanup.

Combine all follow-up work for PR rust-lang#4861:

- keep $CARGO_HOME/bin when it is non-empty

- cover cargo bin cleanup behavior in uninstall tests

- update uninstall cleanup comments and related docs

- rename cleanup internals for clearer intent
Cloud0310 added a commit to Cloud0310/rustup that referenced this pull request May 22, 2026
Avoid removing $CARGO_HOME when running uninstall cleanup.

Combine all follow-up work for PR rust-lang#4861:

- keep $CARGO_HOME/bin when it is non-empty

- cover cargo bin cleanup behavior in uninstall tests

- update uninstall cleanup comments and related docs

- rename cleanup internals for clearer intent
@Cloud0310 Cloud0310 force-pushed the main branch 2 times, most recently from af1c3ac to 60c433c Compare June 8, 2026 18:02
@Cloud0310 Cloud0310 marked this pull request as ready for review June 8, 2026 18:04
@Cloud0310 Cloud0310 changed the title fix!: stop deleting $CARGO_HOME during uninstall fix: stop deleting $CARGO_HOME during uninstall Jun 8, 2026
Comment thread src/cli/self_update.rs Outdated
Comment thread tests/suite/cli_self_upd.rs
Comment thread src/cli/self_update.rs Outdated
Comment thread src/cli/self_update/windows.rs
Comment thread src/cli/self_update.rs
@Cloud0310 Cloud0310 force-pushed the main branch 2 times, most recently from 0677697 to d5e6935 Compare June 10, 2026 14:29

@djc djc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely getting better, still some suggested improvements though.

View changes since this review

Comment thread src/cli/self_update/windows.rs Outdated
// .. 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 spawn_uninstall_gc(process: &Process) -> Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest moving this commit earlier in the series (maybe the very first) to make this more obvious during the changes made earlier.

Comment thread src/cli/self_update/unix.rs Outdated
}

pub(crate) fn delete_rustup_and_cargo_home(process: &Process) -> Result<()> {
pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this rename should be postponed until the commit where the change actually happens (from home to just bin).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djc For extra clarity for @Cloud0310, do you mean right before the commit X where the change happens, after X, or within X?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably in the same commit?

Comment thread src/cli/self_update.rs
Comment thread src/cli/self_update.rs Outdated
/// 2. Remove other tools in $CARGO_HOME/bin, but skipping rustup binary file and links to rustup.
/// 3. If no `--no-modify-path` is passed $CARGO_HOME/bin, clean up $PATH.
/// 4. Remove $CARGO_HOME
/// 2. Remove rustup tool links.

@rami3l rami3l Jun 11, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the commit "fix: preserve CARGO_HOME during uninstall", we are not deleting rustup tool links yet.

The comments should really reflect what the code is doing in that particular commit.

View changes since the review

Comment thread src/cli/self_update.rs
Comment on lines -1098 to -1119
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())?;
}
}
}

@rami3l rami3l Jun 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cloud0310 Really sorry for the back and forth, what you have implemented here matches the expected semantics in a future version, there's nothing wrong about it; but I think it might cause a downgrade in terms of UX within the scope of this PR, so it would be better if:

  • The code that "removes everything except the bin directory" is kept;
  • The code that "removes everything in bin except rustup and tools" is removed as you are doing right now.

When we move further into XDG support it is indeed intended to do this more precisely, but for now it might still feel surprising if self uninstall is not removing a bunch of cargo-specific stuff.

The comments and the tests should be updated accordingly.

My apologies for not having noticed this in advance 🙏

View changes since the review

Comment thread src/cli/self_update.rs
info!("removing empty cargo bin directory `{cargo_bin_path_display}`");

let Err(e) = fs::remove_dir(&cargo_bin_path) else {
if !no_modify_path {

@rami3l rami3l Jun 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to 6aa45d2 (this PR), the user might want to see $CARGO_HOME being removed if it ends up empty at this point. In most cases this will be a no-op.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop nuking $CARGO_HOME (~/.cargo) on rustup self uninstall

5 participants