Skip to content

Commit

Permalink
Clean non-filetype based entries just once
Browse files Browse the repository at this point in the history
  • Loading branch information
osiewicz committed Apr 28, 2024
1 parent 9c685ab commit e2a1df8
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use crate::util::interning::InternedString;
use crate::util::{human_readable_bytes, GlobalContext, Progress, ProgressStyle};
use anyhow::bail;
use cargo_util::paths;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::rc::Rc;

pub struct CleanOptions<'gctx> {
pub gctx: &'gctx GlobalContext,
Expand Down Expand Up @@ -168,7 +170,7 @@ fn clean_specs(
let packages = pkg_set.get_many(pkg_ids)?;

clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));

let mut dirs_to_clean: HashMap<_, HashSet<_>> = HashMap::new();
for pkg in packages {
let pkg_dir = format!("{}-*", pkg.name());
clean_ctx.progress.on_cleaning_package(&pkg.name())?;
Expand All @@ -193,8 +195,9 @@ fn clean_specs(
continue;
}
let crate_name = target.crate_name();
let path_dot = format!("{crate_name}.");
let path_dash = format!("{crate_name}-");
let path_dot: Rc<str> = format!("{crate_name}.").into();
let path_dash: Rc<str> = format!("{crate_name}-").into();

for &mode in &[
CompileMode::Build,
CompileMode::Test,
Expand Down Expand Up @@ -240,16 +243,16 @@ fn clean_specs(
// Remove split-debuginfo files generated by rustc.

let paths = [
(path_dash.as_str(), ".d"),
(path_dot.as_str(), ".o"),
(path_dot.as_str(), ".dwo"),
(path_dot.as_str(), ".dwp"),
(path_dash.clone(), ".d"),
(path_dot.clone(), ".o"),
(path_dot.clone(), ".dwo"),
(path_dot.clone(), ".dwp"),
];
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
dir_glob_str.push(std::path::MAIN_SEPARATOR);
}
dir_glob_str.push('*');
clean_ctx.rm_rf_prefix_list(&dir_glob_str.as_str(), &paths)?;
dirs_to_clean.entry(dir_glob_str).or_default().extend(paths);

// TODO: what to do about build_script_build?
let dir = escape_glob_path(layout.incremental())?;
Expand All @@ -260,6 +263,9 @@ fn clean_specs(
}
}

for (dir, paths) in dirs_to_clean {
clean_ctx.rm_rf_prefix_list(&dir, &paths)?;
}
Ok(())
}

Expand Down Expand Up @@ -332,17 +338,16 @@ impl<'gctx> CleanContext<'gctx> {
fn rm_rf_prefix_list(
&mut self,
pattern: &str,
path_matchers: &[(&str, &str)],
path_matchers: &HashSet<(Rc<str>, &str)>,
) -> CargoResult<()> {
// TODO: Display utf8 warning to user? Or switch to globset?

for path in glob::glob(pattern)? {
let path = path?;
let filename = path.file_name().and_then(|name| name.to_str()).unwrap();
if path_matchers
.iter()
.any(|(prefix, suffix)| filename.starts_with(prefix) && filename.ends_with(suffix))
{
if path_matchers.iter().any(|(prefix, suffix)| {
filename.starts_with(&**prefix) && filename.ends_with(suffix)
}) {
self.rm_rf(&path)?;
}
}
Expand Down

0 comments on commit e2a1df8

Please sign in to comment.