Skip to content

Commit 9c685ab

Browse files
committed
Add rm_rf_prefix_list
1 parent d4c83ee commit 9c685ab

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ fn clean_specs(
193193
continue;
194194
}
195195
let crate_name = target.crate_name();
196+
let path_dot = format!("{crate_name}.");
197+
let path_dash = format!("{crate_name}-");
196198
for &mode in &[
197199
CompileMode::Build,
198200
CompileMode::Test,
@@ -212,8 +214,8 @@ fn clean_specs(
212214
TargetKind::Test | TargetKind::Bench => (layout.deps(), None),
213215
_ => (layout.deps(), Some(layout.dest())),
214216
};
215-
let dir_glob = escape_glob_path(dir)?;
216-
let dir_glob = Path::new(&dir_glob);
217+
let mut dir_glob_str = escape_glob_path(dir)?;
218+
let dir_glob = Path::new(&dir_glob_str);
217219
for file_type in file_types {
218220
// Some files include a hash in the filename, some don't.
219221
let hashed_name = file_type.output_filename(target, Some("*"));
@@ -233,17 +235,21 @@ fn clean_specs(
233235
}
234236
// Remove dep-info file generated by rustc. It is not tracked in
235237
// file_types. It does not have a prefix.
236-
let hashed_dep_info = dir_glob.join(format!("{}-*.d", crate_name));
237-
clean_ctx.rm_rf_glob(&hashed_dep_info)?;
238238
let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
239239
clean_ctx.rm_rf(&unhashed_dep_info)?;
240240
// Remove split-debuginfo files generated by rustc.
241-
let split_debuginfo_obj = dir_glob.join(format!("{}.*.o", crate_name));
242-
clean_ctx.rm_rf_glob(&split_debuginfo_obj)?;
243-
let split_debuginfo_dwo = dir_glob.join(format!("{}.*.dwo", crate_name));
244-
clean_ctx.rm_rf_glob(&split_debuginfo_dwo)?;
245-
let split_debuginfo_dwp = dir_glob.join(format!("{}.*.dwp", crate_name));
246-
clean_ctx.rm_rf_glob(&split_debuginfo_dwp)?;
241+
242+
let paths = [
243+
(path_dash.as_str(), ".d"),
244+
(path_dot.as_str(), ".o"),
245+
(path_dot.as_str(), ".dwo"),
246+
(path_dot.as_str(), ".dwp"),
247+
];
248+
if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
249+
dir_glob_str.push(std::path::MAIN_SEPARATOR);
250+
}
251+
dir_glob_str.push('*');
252+
clean_ctx.rm_rf_prefix_list(&dir_glob_str.as_str(), &paths)?;
247253

248254
// TODO: what to do about build_script_build?
249255
let dir = escape_glob_path(layout.incremental())?;
@@ -323,6 +329,26 @@ impl<'gctx> CleanContext<'gctx> {
323329
Ok(())
324330
}
325331

332+
fn rm_rf_prefix_list(
333+
&mut self,
334+
pattern: &str,
335+
path_matchers: &[(&str, &str)],
336+
) -> CargoResult<()> {
337+
// TODO: Display utf8 warning to user? Or switch to globset?
338+
339+
for path in glob::glob(pattern)? {
340+
let path = path?;
341+
let filename = path.file_name().and_then(|name| name.to_str()).unwrap();
342+
if path_matchers
343+
.iter()
344+
.any(|(prefix, suffix)| filename.starts_with(prefix) && filename.ends_with(suffix))
345+
{
346+
self.rm_rf(&path)?;
347+
}
348+
}
349+
Ok(())
350+
}
351+
326352
pub fn rm_rf(&mut self, path: &Path) -> CargoResult<()> {
327353
let meta = match fs::symlink_metadata(path) {
328354
Ok(meta) => meta,

0 commit comments

Comments
 (0)