Skip to content

Commit d33ef8f

Browse files
committed
Treat calls to rm_rf_prefix_list as idempotent
Starting with this commit we deduplicate calls to rm_rf_prefix_list by crate name and not by directory; this can lead to more calls to rm_rf_prefix_list (especially in presence of multiple -p arguments), but it is also more transparent in terms of progress reporting (we're just storing away whether a given directory + glob pair has already been removed)
1 parent c770700 commit d33ef8f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ fn clean_specs(
170170
let packages = pkg_set.get_many(pkg_ids)?;
171171

172172
clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
173-
let mut dirs_to_clean: HashMap<_, HashSet<_>> = HashMap::new();
173+
174+
let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
174175
for pkg in packages {
175176
let pkg_dir = format!("{}-*", pkg.name());
176177
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
@@ -194,10 +195,9 @@ fn clean_specs(
194195
}
195196
continue;
196197
}
197-
let crate_name = target.crate_name();
198+
let crate_name: Rc<str> = target.crate_name().into();
198199
let path_dot: Rc<str> = format!("{crate_name}.").into();
199200
let path_dash: Rc<str> = format!("{crate_name}-").into();
200-
201201
for &mode in &[
202202
CompileMode::Build,
203203
CompileMode::Test,
@@ -242,17 +242,24 @@ fn clean_specs(
242242
clean_ctx.rm_rf(&unhashed_dep_info)?;
243243
// Remove split-debuginfo files generated by rustc.
244244

245-
let paths = [
246-
(path_dash.clone(), ".d"),
247-
(path_dot.clone(), ".o"),
248-
(path_dot.clone(), ".dwo"),
249-
(path_dot.clone(), ".dwp"),
250-
];
251245
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
252246
dir_glob_str.push(std::path::MAIN_SEPARATOR);
253247
}
254248
dir_glob_str.push('*');
255-
dirs_to_clean.entry(dir_glob_str).or_default().extend(paths);
249+
let dir_glob_str: Rc<str> = dir_glob_str.into();
250+
if cleaned_packages
251+
.entry(dir_glob_str.clone())
252+
.or_default()
253+
.insert(crate_name.clone())
254+
{
255+
let paths = [
256+
(path_dash.clone(), ".d"),
257+
(path_dot.clone(), ".o"),
258+
(path_dot.clone(), ".dwo"),
259+
(path_dot.clone(), ".dwp"),
260+
];
261+
clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
262+
}
256263

257264
// TODO: what to do about build_script_build?
258265
let dir = escape_glob_path(layout.incremental())?;
@@ -263,9 +270,6 @@ fn clean_specs(
263270
}
264271
}
265272

266-
for (dir, paths) in dirs_to_clean {
267-
clean_ctx.rm_rf_prefix_list(&dir, &paths)?;
268-
}
269273
Ok(())
270274
}
271275

@@ -338,7 +342,7 @@ impl<'gctx> CleanContext<'gctx> {
338342
fn rm_rf_prefix_list(
339343
&mut self,
340344
pattern: &str,
341-
path_matchers: &HashSet<(Rc<str>, &str)>,
345+
path_matchers: &[(Rc<str>, &str)],
342346
) -> CargoResult<()> {
343347
// TODO: Display utf8 warning to user? Or switch to globset?
344348

0 commit comments

Comments
 (0)