Skip to content

fix!: stop deleting $CARGO_HOME during uninstall#4861

Draft
Cloud0310 wants to merge 8 commits into
rust-lang:mainfrom
Cloud0310:main
Draft

fix!: stop deleting $CARGO_HOME during uninstall#4861
Cloud0310 wants to merge 8 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
Copy link
Copy Markdown
Contributor Author

Cloud0310 commented May 19, 2026

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
Copy link
Copy Markdown
Member

@rami3l rami3l left a comment

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
Copy link
Copy Markdown
Contributor

@djc djc left a comment

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
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@Cloud0310 Cloud0310 force-pushed the main branch 2 times, most recently from 67d8653 to 9f17fce Compare May 30, 2026 17:18
@rustbot

This comment has been minimized.

@Cloud0310
Copy link
Copy Markdown
Contributor Author

The splitting is done. Do I need further splitting with tests?
If so, it could be more splitted into a) updated expected values, b) new tests upon keeping $CARGO_HOME.

@rami3l rami3l self-assigned this May 31, 2026
Copy link
Copy Markdown
Member

@rami3l rami3l left a comment

Choose a reason for hiding this comment

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

@Cloud0310 It is looking better, but some rework remains to be done. Especially, the 3rd commit (which used to be the 2nd commit in the last version) is still not boring enough.

The splitting is done. Do I need further splitting with tests?
If so, it could be more splitted into a) updated expected values, b) new tests upon keeping $CARGO_HOME.

Please perform the split you mentioned above as well.

Many thanks in advance :)

View changes since this review

Comment thread src/cli/self_update.rs Outdated
info!("removed cargo bin from $PATH");
}

info!("removed empty cargo 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.

Nit: The way you refer to the bindir is not consistent both in this function and elsewhere:

  • cargo bin
  • cargo bin `{cargo_bin_path}`
  • cargo bin directory `{cargo_bin_path}`

I suggest using the final one consistently.

Comment thread src/cli/self_update.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
// 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)?;
// Now that the parent has exited, rustup.exe is hopefully no longer open.
Copy link
Copy Markdown
Member

@rami3l rami3l May 31, 2026

Choose a reason for hiding this comment

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

This new hunk has the same contents as unix::clean_cargo_bin() except the let no_modify_path line; consider further polishing the refactoring process by first moving unix::clean_cargo_bin() to the parent module and inline your current delete_cargo_bin() over there (it should have a single caller by then so the introduction of this new function delete_cargo_bin() is unnecessary).

Copy link
Copy Markdown
Member

@rami3l rami3l Jun 1, 2026

Choose a reason for hiding this comment

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

@Cloud0310 Please delete the function delete_cargo_bin. As previously discussed, unix::clean_cargo_bin() should be moved to the parent module that contains logic common to both Windows and Unix (which includes the logic of delete_cargo_bin which is not repeated anywhere else); in Windows we use a platform specific wrapper to wrap that function in the windows module.

@rami3l rami3l changed the title fix: stop remove $CARGO_HOME on uninstallation fix: stop deleting $CARGO_HOME during uninstall May 31, 2026
@rami3l rami3l changed the title fix: stop deleting $CARGO_HOME during uninstall fix!: stop deleting $CARGO_HOME during uninstall May 31, 2026
Comment thread src/cli/self_update.rs
Comment thread src/cli/self_update/unix.rs Outdated
}

pub(crate) fn clean_cargo_bin(process: &Process) -> Result<()> {
pub(crate) fn clean_cargo_bin(process: &Process, no_modify_path: bool) -> Result<()> {
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

Nit: change the argument order (for this and other functions with the added argument), to have the more "interesting"/rare/specific argument before the less interesting/more common/less specific process argument.

View changes since the review

Comment thread src/cli/self_update/unix.rs Outdated
use anyhow::{Context, Result, bail};
use tracing::{error, warn};

use super::delete_cargo_bin;
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

Nit: this should be included in the existing super:: import, below.

View changes since the review

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 I'm not quite sure what you are trying to say here, I assume you mean "in a first commit merge those in two groups of imports super::{...} and crate::{...}, then introduce the new import in the respective group"? Because in this file the imports are not grouped yet.

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 mean that this is adding a use super::delete_cargo_bin; when there is already a use super::some_other_thing;.

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 Okay, so we should put the single-level imports like this use super::{delete_cargo_bin, some_other_thing};

Comment thread src/cli/self_update/unix.rs Outdated
utils::remove_dir("cargo_home", &cargo_home)
// Clean up CARGO_HOME/bin, if it's empty now
// On success, remove it from $PATH
delete_cargo_bin(cargo_bin_path, no_modify_path, process)
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

It is unclear to me why there are separate commits for the Unix and Windows paths? It seems to me like these should be in the same commit.

View changes since the review

Comment thread src/cli/self_update.rs
Comment on lines -1112 to -1128
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())?;
}
}
}
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

It is not clear from reviewing this (presumably Unix-only) commit why this (presumably platform-independent) logic could be removed.

View changes since the review

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

use super::super::errors::CliError;
use super::common;
use super::delete_cargo_bin;
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

Nit: use existing import.

View changes since the review

Copy link
Copy Markdown
Member

@rami3l rami3l Jun 1, 2026

Choose a reason for hiding this comment

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

Ok(utils::ExitCode(0))
}

const GC_MODIFY_PATH: &str = "RUSTUP_GC_MODIFY_PATH";
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

Perhaps this should be introduced independently to make it more clear why it is introduced/what it's purpose is? (It should be defined below all callers.)

View changes since the review

Comment thread src/cli/self_update.rs
Comment on lines +1049 to +1054
/// Uninstall process:
/// 1. Remove rustup home.
/// 2. Clean up rustup tool proxies.
/// 3. Remove rustup binary file.
/// 4. Try to clean up $CARGO_HOME/bin if it's empty.
/// 5. Upon successfully removing $CARGO_HOME/bin, clean up $PATH.
Copy link
Copy Markdown
Contributor

@djc djc Jun 1, 2026

Choose a reason for hiding this comment

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

Suggest introducing this in a separate first commit, and then updating it in each commit (where necessary) as a way of documenting what is changed.

View changes since the review

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 1, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rami3l rami3l marked this pull request as draft June 1, 2026 14:17
@rami3l
Copy link
Copy Markdown
Member

rami3l commented Jun 1, 2026

@Cloud0310 Since your PR needs more work, I've marked this PR as draft.

Please address or at least respond to all open threads before marking this PR as "ready for review" again, at which point the team will return for another round of review, thanks 🙏

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