Skip to content

Commit 26cf274

Browse files
committed
Combine the behavior for cargo uninstall
1 parent 0fd4fd3 commit 26cf274

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/cargo/ops/cargo_uninstall.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,15 @@ fn uninstall_pkgid(
138138
}
139139

140140
if bins.is_empty() {
141-
to_remove.extend(installed.iter().map(|b| dst.join(b)));
142-
tracker.remove(pkgid, &installed);
141+
to_remove = installed.iter().collect();
143142
} else {
144-
for bin in bins.iter() {
145-
to_remove.push(dst.join(bin));
146-
}
147-
tracker.remove(pkgid, &bins);
143+
to_remove = bins.iter().collect();
148144
}
149145

150-
// This should have been handled last, but now restore it to reproduce the problem on the Windows platform.
151-
// See issue #3364.
152-
tracker.save()?;
153-
154146
for bin in to_remove {
155-
config.shell().status("Removing", bin.display())?;
156-
paths::remove_file(bin)?;
147+
let bin_path = dst.join(bin);
148+
config.shell().status("Removing", bin_path.display())?;
149+
tracker.remove_bin_then_save(pkgid, bin, &bin_path)?;
157150
}
158151

159152
Ok(())

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::rc::Rc;
77
use std::task::Poll;
88

99
use anyhow::{bail, format_err, Context as _};
10+
use cargo_util::paths;
1011
use ops::FilterRule;
1112
use serde::{Deserialize, Serialize};
1213

@@ -319,6 +320,20 @@ impl InstallTracker {
319320
self.v1.remove(pkg_id, bins);
320321
self.v2.remove(pkg_id, bins);
321322
}
323+
324+
/// Remove a bin after it successfully had been removed in disk and the save the tracker at last.
325+
pub fn remove_bin_then_save(
326+
&mut self,
327+
pkg_id: PackageId,
328+
bin: &str,
329+
bin_path: &PathBuf,
330+
) -> CargoResult<()> {
331+
paths::remove_file(bin_path)?;
332+
self.v1.remove_bin(pkg_id, bin);
333+
self.v2.remove_bin(pkg_id, bin);
334+
self.save()?;
335+
Ok(())
336+
}
322337
}
323338

324339
impl CrateListingV1 {
@@ -359,6 +374,17 @@ impl CrateListingV1 {
359374
}
360375
}
361376

377+
fn remove_bin(&mut self, pkg_id: PackageId, bin: &str) {
378+
let mut installed = match self.v1.entry(pkg_id) {
379+
btree_map::Entry::Occupied(e) => e,
380+
btree_map::Entry::Vacant(..) => panic!("v1 unexpected missing `{}`", pkg_id),
381+
};
382+
installed.get_mut().remove(bin);
383+
if installed.get().is_empty() {
384+
installed.remove();
385+
}
386+
}
387+
362388
fn save(&self, lock: &FileLock) -> CargoResult<()> {
363389
let mut file = lock.file();
364390
file.seek(SeekFrom::Start(0))?;
@@ -468,6 +494,17 @@ impl CrateListingV2 {
468494
}
469495
}
470496

497+
fn remove_bin(&mut self, pkg_id: PackageId, bin: &str) {
498+
let mut info_entry = match self.installs.entry(pkg_id) {
499+
btree_map::Entry::Occupied(e) => e,
500+
btree_map::Entry::Vacant(..) => panic!("v1 unexpected missing `{}`", pkg_id),
501+
};
502+
info_entry.get_mut().bins.remove(bin);
503+
if info_entry.get().bins.is_empty() {
504+
info_entry.remove();
505+
}
506+
}
507+
471508
fn save(&self, lock: &FileLock) -> CargoResult<()> {
472509
let mut file = lock.file();
473510
file.seek(SeekFrom::Start(0))?;

0 commit comments

Comments
 (0)