1use crate::compiler::trim_paths;
2use crate::compiler::{CompileKind, CompileMode, Layout, RustcTargetData};
3use crate::ops;
4use crate::util::HumanBytes;
5use crate::util::data_structures::{IndexMap, IndexSet};
6use crate::util::edit_distance;
7use crate::util::errors::CargoResult;
8use crate::util::interning::InternedString;
9use crate::util::{GlobalContext, Progress, ProgressStyle};
10use crate::workspace::profiles::Profiles;
11use crate::workspace::{PackageIdSpec, PackageIdSpecQuery, TargetKind, Workspace};
12use anyhow::bail;
13use cargo_util::paths;
14use cargo_util_terminal::report::Level;
15
16use std::ffi::OsString;
17use std::io::Read;
18use std::path::{Path, PathBuf};
19use std::rc::Rc;
20use std::{fs, io};
21
22pub struct CleanOptions<'gctx> {
23 pub gctx: &'gctx GlobalContext,
24 pub spec: IndexSet<String>,
26 pub targets: Vec<String>,
28 pub profile_specified: bool,
30 pub requested_profile: InternedString,
32 pub doc: bool,
34 pub dry_run: bool,
36 pub explicit_target_dir_arg: bool,
38}
39
40pub struct CleanContext<'gctx> {
41 pub gctx: &'gctx GlobalContext,
42 progress: Box<dyn CleaningProgressBar + 'gctx>,
43 pub dry_run: bool,
44 num_files_removed: u64,
45 num_dirs_removed: u64,
46 total_bytes_removed: u64,
47}
48
49pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
51 let mut target_dir = ws.target_dir();
52 let mut build_dir = ws.build_dir();
53 let gctx = opts.gctx;
54 let mut clean_ctx = CleanContext::new(gctx);
55 clean_ctx.dry_run = opts.dry_run;
56
57 const CLEAN_ABORT_NOTE: &str =
58 "cleaning has been aborted to prevent accidental deletion of unrelated files";
59
60 if let Ok(meta) = fs::symlink_metadata(target_dir.as_path_unlocked()) {
62 if !meta.is_symlink() && !meta.is_dir() {
64 let title = format!("cannot clean `{}`: not a directory", target_dir.display());
65 let report = [Level::ERROR
66 .primary_title(title)
67 .element(Level::NOTE.message(CLEAN_ABORT_NOTE))];
68 gctx.shell().print_report(&report, false)?;
69 return Err(crate::AlreadyPrintedError::new(anyhow::anyhow!("")).into());
70 }
71 }
72
73 if opts.explicit_target_dir_arg {
75 let target_dir_path = target_dir.as_path_unlocked();
76
77 if target_dir_path.exists()
79 && let Err(err) = validate_target_dir_tag(target_dir_path)
80 {
81 let title = format!("cannot clean `{}`: {err}", target_dir_path.display());
83 let report = [Level::ERROR
84 .primary_title(title)
85 .element(Level::NOTE.message(CLEAN_ABORT_NOTE))];
86 gctx.shell().print_report(&report, false)?;
87 return Err(crate::AlreadyPrintedError::new(anyhow::anyhow!("")).into());
88 }
89 }
90
91 if opts.doc {
92 if !opts.spec.is_empty() {
93 bail!("--doc cannot be used with -p");
100 }
101 let doc_dirs = CompileKind::from_requested_targets(gctx, &opts.targets)?
103 .into_iter()
104 .map(|kind| {
105 let target_dir = match kind {
106 CompileKind::Host => target_dir.clone(),
107 CompileKind::Target(target) => target_dir.join(target.short_name()),
108 };
109 target_dir.join("doc").into_path_unlocked()
110 })
111 .collect::<Vec<_>>();
112 clean_ctx.remove_paths(&doc_dirs)?;
113 } else {
114 let profiles = Profiles::new(&ws, opts.requested_profile)?;
115
116 if opts.profile_specified {
117 let dir_name = profiles.get_dir_name();
121 target_dir = target_dir.join(dir_name);
122 build_dir = build_dir.join(dir_name);
123 }
124
125 if opts.spec.is_empty() {
131 let paths: &[PathBuf] = if build_dir != target_dir {
132 &[
133 target_dir.into_path_unlocked(),
134 build_dir.into_path_unlocked(),
135 ]
136 } else {
137 &[target_dir.into_path_unlocked()]
138 };
139 clean_ctx.remove_paths(paths)?;
140 } else {
141 clean_specs(
142 &mut clean_ctx,
143 &ws,
144 &profiles,
145 &opts.targets,
146 &opts.spec,
147 opts.dry_run,
148 )?;
149 }
150 }
151
152 clean_ctx.display_summary()?;
153 Ok(())
154}
155
156fn validate_target_dir_tag(target_dir_path: &Path) -> CargoResult<()> {
157 const TAG_SIGNATURE: &[u8] = b"Signature: 8a477f597d28d172789f06886806bc55";
158
159 let tag_path = target_dir_path.join("CACHEDIR.TAG");
160
161 if tag_path.is_symlink() {
163 bail!("expect `CACHEDIR.TAG` to be a regular file, got a symlink");
164 }
165
166 if !tag_path.is_file() {
167 bail!("missing or invalid `CACHEDIR.TAG` file");
168 }
169
170 let mut file = fs::File::open(&tag_path)
171 .map_err(|err| anyhow::anyhow!("failed to open `{}`: {}", tag_path.display(), err))?;
172
173 let mut buf = [0u8; TAG_SIGNATURE.len()];
174 match file.read_exact(&mut buf) {
175 Ok(()) if &buf[..] == TAG_SIGNATURE => {}
176 Err(e) if e.kind() != io::ErrorKind::UnexpectedEof => {
177 bail!("failed to read `{}`: {e}", tag_path.display());
178 }
179 _ => {
180 bail!("invalid signature in `CACHEDIR.TAG` file");
181 }
182 }
183
184 Ok(())
185}
186
187fn clean_specs(
188 clean_ctx: &mut CleanContext<'_>,
189 ws: &Workspace<'_>,
190 profiles: &Profiles,
191 targets: &[String],
192 spec: &IndexSet<String>,
193 dry_run: bool,
194) -> CargoResult<()> {
195 let requested_kinds = CompileKind::from_requested_targets(clean_ctx.gctx, targets)?;
197 let target_data = RustcTargetData::new(ws, &requested_kinds)?;
198 let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
199 let prof_dir_name = profiles.get_dir_name();
200 let host_layout = Layout::new(ws, None, &prof_dir_name, true, true)?;
201 let target_layouts: Vec<(CompileKind, Layout)> = requested_kinds
203 .into_iter()
204 .filter_map(|kind| match kind {
205 CompileKind::Target(target) => {
206 match Layout::new(ws, Some(target), &prof_dir_name, true, true) {
207 Ok(layout) => Some(Ok((kind, layout))),
208 Err(e) => Some(Err(e)),
209 }
210 }
211 CompileKind::Host => None,
212 })
213 .collect::<CargoResult<_>>()?;
214 let layouts = if target_layouts.is_empty() {
217 vec![(CompileKind::Host, &host_layout)]
218 } else {
219 target_layouts
220 .iter()
221 .map(|(kind, layout)| (*kind, layout))
222 .collect()
223 };
224 let layouts_with_host: Vec<(CompileKind, &Layout)> =
226 std::iter::once((CompileKind::Host, &host_layout))
227 .chain(layouts.iter().map(|(k, l)| (*k, *l)))
228 .collect();
229
230 let mut pkg_ids = Vec::new();
237 for spec_str in spec.iter() {
238 let spec = PackageIdSpec::parse(spec_str)?;
240 if spec.partial_version().is_some() {
241 clean_ctx.gctx.shell().warn(&format!(
242 "version qualifier in `-p {}` is ignored, \
243 cleaning all versions of `{}` found",
244 spec_str,
245 spec.name()
246 ))?;
247 }
248 if spec.url().is_some() {
249 clean_ctx.gctx.shell().warn(&format!(
250 "url qualifier in `-p {}` ignored, \
251 cleaning all versions of `{}` found",
252 spec_str,
253 spec.name()
254 ))?;
255 }
256 let matches: Vec<_> = resolve.iter().filter(|id| spec.matches(*id)).collect();
257 if matches.is_empty() {
258 let mut suggestion = String::new();
259 suggestion.push_str(&edit_distance::closest_msg(
260 &spec.name(),
261 resolve.iter(),
262 |id| id.name().as_str(),
263 "package",
264 ));
265 anyhow::bail!(
266 "package ID specification `{}` did not match any packages{}",
267 spec,
268 suggestion
269 );
270 }
271 pkg_ids.extend(matches);
272 }
273 let packages = pkg_set.get_many(pkg_ids)?;
274
275 clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
276 let mut dirs_to_clean = DirectoriesToClean::default();
277
278 if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
279 for pkg in packages {
280 clean_ctx.progress.on_cleaning_package(&pkg.name())?;
281
282 for (_compile_kind, layout) in &layouts_with_host {
284 let dir = layout.build_dir().build_unit(&pkg.name());
285 clean_ctx.rm_rf(&dir)?;
286 }
287
288 for target in pkg.targets() {
290 if target.is_custom_build() {
291 continue;
292 }
293 let crate_name: Rc<str> = target.crate_name().into();
294 for &mode in &[
295 CompileMode::Build,
296 CompileMode::Test,
297 CompileMode::Check { test: false },
298 ] {
299 for (compile_kind, layout) in &layouts {
300 let triple = target_data.short_name(compile_kind);
301 let (file_types, _unsupported) = target_data
302 .info(*compile_kind)
303 .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
304 let artifact_dir = layout
305 .artifact_dir()
306 .expect("artifact-dir was not locked during clean");
307 let uplift_dir = match target.kind() {
308 TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
309 Some(artifact_dir.examples())
310 }
311 TargetKind::Test | TargetKind::Bench => None,
313 _ => Some(artifact_dir.dest()),
314 };
315 if let Some(uplift_dir) = uplift_dir {
316 for file_type in file_types {
317 let uplifted_filename = file_type.uplift_filename(target);
318
319 let dep_info = Path::new(&uplifted_filename)
321 .with_extension("d")
322 .to_string_lossy()
323 .into_owned();
324
325 dirs_to_clean.mark_utf(uplift_dir, |filename| {
326 filename == uplifted_filename || filename == dep_info
327 });
328 }
329 }
330 let path_dash = format!("{}-", crate_name);
331
332 dirs_to_clean.mark_utf(layout.build_dir().incremental(), |filename| {
333 filename.starts_with(&path_dash)
334 });
335 }
336 }
337 }
338 }
339 } else {
340 for pkg in packages {
341 clean_ctx.progress.on_cleaning_package(&pkg.name())?;
342
343 for (_, layout) in &layouts_with_host {
345 dirs_to_clean.mark_utf(layout.build_dir().legacy_fingerprint(), |filename| {
346 let Some((pkg_name, _)) = filename.rsplit_once('-') else {
347 return false;
348 };
349
350 pkg_name == pkg.name().as_str()
351 });
352 }
353
354 for target in pkg.targets() {
355 if target.is_custom_build() {
356 for (_, layout) in &layouts_with_host {
358 dirs_to_clean.mark_utf(layout.build_dir().build(), |filename| {
359 let Some((current_name, _)) = filename.rsplit_once('-') else {
360 return false;
361 };
362
363 current_name == pkg.name().as_str()
364 });
365 }
366 continue;
367 }
368 let crate_name: Rc<str> = target.crate_name().into();
369 let path_dot: &str = &format!("{crate_name}.");
370 let path_dash: &str = &format!("{crate_name}-");
371 for &mode in &[
372 CompileMode::Build,
373 CompileMode::Test,
374 CompileMode::Check { test: false },
375 ] {
376 for (compile_kind, layout) in &layouts {
377 let triple = target_data.short_name(compile_kind);
378 let (file_types, _unsupported) = target_data
379 .info(*compile_kind)
380 .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
381 let artifact_dir = layout
382 .artifact_dir()
383 .expect("artifact-dir was not locked during clean");
384 let (dir, uplift_dir) = match target.kind() {
385 TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
386 (layout.build_dir().examples(), Some(artifact_dir.examples()))
387 }
388 TargetKind::Test | TargetKind::Bench => {
390 (layout.build_dir().legacy_deps(), None)
391 }
392 _ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
393 };
394
395 for file_type in file_types {
396 let (prefix, suffix) = file_type.output_prefix_suffix(target);
398 let unhashed_name = file_type.output_filename(target, None);
399 let unhashed_unremap =
401 format!("{unhashed_name}{}", trim_paths::UNREMAP_SUFFIX);
402 dirs_to_clean.mark_utf(&dir, |filename| {
403 (filename.starts_with(&prefix) && filename.ends_with(&suffix))
404 || unhashed_name == filename
405 || unhashed_unremap == filename
406 });
407
408 if let Some(uplift_dir) = uplift_dir {
410 let uplifted_path =
411 uplift_dir.join(file_type.uplift_filename(target));
412 clean_ctx.rm_rf(&uplifted_path)?;
413 let dep_info = uplifted_path.with_extension("d");
415 clean_ctx.rm_rf(&dep_info)?;
416 let unremap = trim_paths::append_unremap_suffix(&uplifted_path);
418 clean_ctx.rm_rf(&unremap)?;
419 }
420 }
421 let unhashed_dep_info = format!("{}.d", crate_name);
422 dirs_to_clean.mark_utf(dir, |filename| filename == unhashed_dep_info);
423
424 dirs_to_clean.mark_utf(dir, |filename| {
425 if filename.starts_with(&path_dash) {
426 filename.ends_with(".d")
429 || filename.ends_with(trim_paths::UNREMAP_SUFFIX)
431 } else if filename.starts_with(&path_dot) {
432 [".o", ".dwo", ".dwp"]
434 .iter()
435 .any(|suffix| filename.ends_with(suffix))
436 } else {
437 false
438 }
439 });
440
441 dirs_to_clean.mark_utf(layout.build_dir().incremental(), |filename| {
443 filename.starts_with(path_dash)
444 });
445 }
446 }
447 }
448 }
449 }
450 clean_ctx.rm_rf_all(dirs_to_clean)?;
451
452 Ok(())
453}
454
455#[derive(Default)]
456struct DirectoriesToClean {
457 dir_contents: IndexMap<PathBuf, IndexSet<OsString>>,
458 to_remove: IndexSet<PathBuf>,
459}
460
461impl DirectoriesToClean {
462 fn mark(&mut self, directory: &Path, mut should_remove_entry: impl FnMut(&OsString) -> bool) {
463 let entry = match self.dir_contents.entry(directory.to_owned()) {
464 indexmap::map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
465 indexmap::map::Entry::Vacant(vacant_entry) => {
466 let Ok(dir_entries) = std::fs::read_dir(directory.to_owned()) else {
467 return;
468 };
469 vacant_entry.insert(
470 dir_entries
471 .into_iter()
472 .flatten()
473 .map(|entry| entry.file_name())
474 .collect::<IndexSet<_>>(),
475 )
476 }
477 };
478
479 entry.retain(|path| {
480 let should_remove = should_remove_entry(path);
481 if should_remove {
482 self.to_remove.insert(directory.join(path));
483 }
484 !should_remove
485 });
486 }
487
488 fn mark_utf(&mut self, directory: &Path, mut should_remove_entry: impl FnMut(&str) -> bool) {
489 self.mark(directory, move |filename| {
490 let Some(as_utf) = filename.to_str() else {
491 return false;
492 };
493 should_remove_entry(as_utf)
494 });
495 }
496}
497
498impl<'gctx> CleanContext<'gctx> {
499 pub fn new(gctx: &'gctx GlobalContext) -> Self {
500 let progress = CleaningFolderBar::new(gctx, 0);
503 CleanContext {
504 gctx,
505 progress: Box::new(progress),
506 dry_run: false,
507 num_files_removed: 0,
508 num_dirs_removed: 0,
509 total_bytes_removed: 0,
510 }
511 }
512
513 fn rm_rf_all(&mut self, dirs: DirectoriesToClean) -> CargoResult<()> {
514 for path in dirs.to_remove {
515 self.rm_rf(&path)?;
516 }
517 Ok(())
518 }
519
520 pub fn rm_rf(&mut self, path: &Path) -> CargoResult<()> {
521 let meta = match fs::symlink_metadata(path) {
522 Ok(meta) => meta,
523 Err(e) => {
524 if e.kind() != std::io::ErrorKind::NotFound {
525 self.gctx
526 .shell()
527 .warn(&format!("cannot access {}: {e}", path.display()))?;
528 }
529 return Ok(());
530 }
531 };
532
533 if !self.dry_run {
535 self.gctx
536 .shell()
537 .verbose(|shell| shell.status("Removing", path.display()))?;
538 }
539 self.progress.display_now()?;
540
541 let mut rm_file = |path: &Path, meta: Result<std::fs::Metadata, _>| {
542 if let Ok(meta) = meta {
543 self.total_bytes_removed += meta.len();
547 }
548 self.num_files_removed += 1;
549 if !self.dry_run {
550 paths::remove_file(path)?;
551 }
552 Ok(())
553 };
554
555 if !meta.is_dir() {
556 return rm_file(path, Ok(meta));
557 }
558
559 for entry in walkdir::WalkDir::new(path).contents_first(true) {
560 let entry = entry?;
561 self.progress.on_clean()?;
562 if self.dry_run {
563 self.gctx
568 .shell()
569 .verbose(|shell| Ok(writeln!(shell.out(), "{}", entry.path().display())?))?;
570 }
571 if entry.file_type().is_dir() {
572 self.num_dirs_removed += 1;
573 if !self.dry_run {
578 paths::remove_dir_all(entry.path())?;
579 }
580 } else {
581 rm_file(entry.path(), entry.metadata())?;
582 }
583 }
584
585 Ok(())
586 }
587
588 pub fn display_summary(&self) -> CargoResult<()> {
589 let status = if self.dry_run { "Summary" } else { "Removed" };
590 let byte_count = if self.total_bytes_removed == 0 {
591 String::new()
592 } else {
593 let bytes = HumanBytes(self.total_bytes_removed);
594 format!(", {bytes:.1} total")
595 };
596 let file_count = match (self.num_files_removed, self.num_dirs_removed) {
602 (0, 0) => format!("0 files"),
603 (0, 1) => format!("1 directory"),
604 (0, 2..) => format!("{} directories", self.num_dirs_removed),
605 (1, _) => format!("1 file"),
606 (2.., _) => format!("{} files", self.num_files_removed),
607 };
608 self.gctx
609 .shell()
610 .status(status, format!("{file_count}{byte_count}"))?;
611 if self.dry_run {
612 self.gctx
613 .shell()
614 .warn("no files deleted due to --dry-run")?;
615 }
616 Ok(())
617 }
618
619 pub fn remove_paths(&mut self, paths: &[PathBuf]) -> CargoResult<()> {
625 let num_paths = paths
626 .iter()
627 .map(|path| walkdir::WalkDir::new(path).into_iter().count())
628 .sum();
629 self.progress = Box::new(CleaningFolderBar::new(self.gctx, num_paths));
630 for path in paths {
631 self.rm_rf(path)?;
632 }
633 Ok(())
634 }
635}
636
637trait CleaningProgressBar {
638 fn display_now(&mut self) -> CargoResult<()>;
639 fn on_clean(&mut self) -> CargoResult<()>;
640 fn on_cleaning_package(&mut self, _package: &str) -> CargoResult<()> {
641 Ok(())
642 }
643}
644
645struct CleaningFolderBar<'gctx> {
646 bar: Progress<'gctx>,
647 max: usize,
648 cur: usize,
649}
650
651impl<'gctx> CleaningFolderBar<'gctx> {
652 fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
653 Self {
654 bar: Progress::with_style("Cleaning", ProgressStyle::Percentage, gctx),
655 max,
656 cur: 0,
657 }
658 }
659
660 fn cur_progress(&self) -> usize {
661 std::cmp::min(self.cur, self.max)
662 }
663}
664
665impl<'gctx> CleaningProgressBar for CleaningFolderBar<'gctx> {
666 fn display_now(&mut self) -> CargoResult<()> {
667 self.bar.tick_now(self.cur_progress(), self.max, "")
668 }
669
670 fn on_clean(&mut self) -> CargoResult<()> {
671 self.cur += 1;
672 self.bar.tick(self.cur_progress(), self.max, "")
673 }
674}
675
676struct CleaningPackagesBar<'gctx> {
677 bar: Progress<'gctx>,
678 max: usize,
679 cur: usize,
680 num_files_folders_cleaned: usize,
681 package_being_cleaned: String,
682}
683
684impl<'gctx> CleaningPackagesBar<'gctx> {
685 fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
686 Self {
687 bar: Progress::with_style("Cleaning", ProgressStyle::Ratio, gctx),
688 max,
689 cur: 0,
690 num_files_folders_cleaned: 0,
691 package_being_cleaned: String::new(),
692 }
693 }
694
695 fn cur_progress(&self) -> usize {
696 std::cmp::min(self.cur, self.max)
697 }
698
699 fn format_message(&self) -> String {
700 format!(
701 ": {}, {} files/folders cleaned",
702 self.package_being_cleaned, self.num_files_folders_cleaned
703 )
704 }
705}
706
707impl<'gctx> CleaningProgressBar for CleaningPackagesBar<'gctx> {
708 fn display_now(&mut self) -> CargoResult<()> {
709 self.bar
710 .tick_now(self.cur_progress(), self.max, &self.format_message())
711 }
712
713 fn on_clean(&mut self) -> CargoResult<()> {
714 self.bar
715 .tick(self.cur_progress(), self.max, &self.format_message())?;
716 self.num_files_folders_cleaned += 1;
717 Ok(())
718 }
719
720 fn on_cleaning_package(&mut self, package: &str) -> CargoResult<()> {
721 self.cur += 1;
722 self.package_being_cleaned = String::from(package);
723 self.bar
724 .tick(self.cur_progress(), self.max, &self.format_message())
725 }
726}