1use crate::core::compiler::{CompileKind, CompileMode, Layout, RustcTargetData};
2use crate::core::profiles::Profiles;
3use crate::core::{PackageIdSpec, PackageIdSpecQuery, TargetKind, Workspace};
4use crate::ops;
5use crate::util::edit_distance;
6use crate::util::errors::CargoResult;
7use crate::util::interning::InternedString;
8use crate::util::HumanBytes;
9use crate::util::{GlobalContext, Progress, ProgressStyle};
10use anyhow::bail;
11use cargo_util::paths;
12use std::collections::{HashMap, HashSet};
13use std::fs;
14use std::path::{Path, PathBuf};
15use std::rc::Rc;
16
17pub struct CleanOptions<'gctx> {
18 pub gctx: &'gctx GlobalContext,
19 pub spec: Vec<String>,
21 pub targets: Vec<String>,
23 pub profile_specified: bool,
25 pub requested_profile: InternedString,
27 pub doc: bool,
29 pub dry_run: bool,
31}
32
33pub struct CleanContext<'gctx> {
34 pub gctx: &'gctx GlobalContext,
35 progress: Box<dyn CleaningProgressBar + 'gctx>,
36 pub dry_run: bool,
37 num_files_removed: u64,
38 num_dirs_removed: u64,
39 total_bytes_removed: u64,
40}
41
42pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
44 let mut target_dir = ws.target_dir();
45 let mut build_dir = ws.build_dir();
46 let gctx = opts.gctx;
47 let mut clean_ctx = CleanContext::new(gctx);
48 clean_ctx.dry_run = opts.dry_run;
49
50 if opts.doc {
51 if !opts.spec.is_empty() {
52 bail!("--doc cannot be used with -p");
59 }
60 target_dir = target_dir.join("doc");
62 clean_ctx.remove_paths(&[target_dir.into_path_unlocked()])?;
63 } else {
64 let profiles = Profiles::new(&ws, opts.requested_profile)?;
65
66 if opts.profile_specified {
67 let dir_name = profiles.get_dir_name();
71 target_dir = target_dir.join(dir_name);
72 build_dir = build_dir.join(dir_name);
73 }
74
75 if opts.spec.is_empty() {
81 let paths: &[PathBuf] = if gctx.cli_unstable().build_dir && build_dir != target_dir {
82 &[
83 target_dir.into_path_unlocked(),
84 build_dir.into_path_unlocked(),
85 ]
86 } else {
87 &[target_dir.into_path_unlocked()]
88 };
89 clean_ctx.remove_paths(paths)?;
90 } else {
91 clean_specs(
92 &mut clean_ctx,
93 &ws,
94 &profiles,
95 &opts.targets,
96 &opts.spec,
97 opts.dry_run,
98 )?;
99 }
100 }
101
102 clean_ctx.display_summary()?;
103 Ok(())
104}
105
106fn clean_specs(
107 clean_ctx: &mut CleanContext<'_>,
108 ws: &Workspace<'_>,
109 profiles: &Profiles,
110 targets: &[String],
111 spec: &[String],
112 dry_run: bool,
113) -> CargoResult<()> {
114 let requested_kinds = CompileKind::from_requested_targets(clean_ctx.gctx, targets)?;
116 let target_data = RustcTargetData::new(ws, &requested_kinds)?;
117 let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
118 let prof_dir_name = profiles.get_dir_name();
119 let host_layout = Layout::new(ws, None, &prof_dir_name)?;
120 let target_layouts: Vec<(CompileKind, Layout)> = requested_kinds
122 .into_iter()
123 .filter_map(|kind| match kind {
124 CompileKind::Target(target) => match Layout::new(ws, Some(target), &prof_dir_name) {
125 Ok(layout) => Some(Ok((kind, layout))),
126 Err(e) => Some(Err(e)),
127 },
128 CompileKind::Host => None,
129 })
130 .collect::<CargoResult<_>>()?;
131 let layouts = if targets.is_empty() {
134 vec![(CompileKind::Host, &host_layout)]
135 } else {
136 target_layouts
137 .iter()
138 .map(|(kind, layout)| (*kind, layout))
139 .collect()
140 };
141 let layouts_with_host: Vec<(CompileKind, &Layout)> =
143 std::iter::once((CompileKind::Host, &host_layout))
144 .chain(layouts.iter().map(|(k, l)| (*k, *l)))
145 .collect();
146
147 let mut pkg_ids = Vec::new();
154 for spec_str in spec.iter() {
155 let spec = PackageIdSpec::parse(spec_str)?;
157 if spec.partial_version().is_some() {
158 clean_ctx.gctx.shell().warn(&format!(
159 "version qualifier in `-p {}` is ignored, \
160 cleaning all versions of `{}` found",
161 spec_str,
162 spec.name()
163 ))?;
164 }
165 if spec.url().is_some() {
166 clean_ctx.gctx.shell().warn(&format!(
167 "url qualifier in `-p {}` ignored, \
168 cleaning all versions of `{}` found",
169 spec_str,
170 spec.name()
171 ))?;
172 }
173 let matches: Vec<_> = resolve.iter().filter(|id| spec.matches(*id)).collect();
174 if matches.is_empty() {
175 let mut suggestion = String::new();
176 suggestion.push_str(&edit_distance::closest_msg(
177 &spec.name(),
178 resolve.iter(),
179 |id| id.name().as_str(),
180 "package",
181 ));
182 anyhow::bail!(
183 "package ID specification `{}` did not match any packages{}",
184 spec,
185 suggestion
186 );
187 }
188 pkg_ids.extend(matches);
189 }
190 let packages = pkg_set.get_many(pkg_ids)?;
191
192 clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
193
194 let mut cleaned_packages: HashMap<_, HashSet<_>> = HashMap::default();
197 for pkg in packages {
198 let pkg_dir = format!("{}-*", pkg.name());
199 clean_ctx.progress.on_cleaning_package(&pkg.name())?;
200
201 for (_, layout) in &layouts_with_host {
203 let dir = escape_glob_path(layout.fingerprint())?;
204 clean_ctx
205 .rm_rf_package_glob_containing_hash(&pkg.name(), &Path::new(&dir).join(&pkg_dir))?;
206 }
207
208 for target in pkg.targets() {
209 if target.is_custom_build() {
210 for (_, layout) in &layouts_with_host {
212 let dir = escape_glob_path(layout.build())?;
213 clean_ctx.rm_rf_package_glob_containing_hash(
214 &pkg.name(),
215 &Path::new(&dir).join(&pkg_dir),
216 )?;
217 }
218 continue;
219 }
220 let crate_name: Rc<str> = target.crate_name().into();
221 let path_dot: &str = &format!("{crate_name}.");
222 let path_dash: &str = &format!("{crate_name}-");
223 for &mode in &[
224 CompileMode::Build,
225 CompileMode::Test,
226 CompileMode::Check { test: false },
227 ] {
228 for (compile_kind, layout) in &layouts {
229 let triple = target_data.short_name(compile_kind);
230
231 let (file_types, _unsupported) = target_data
232 .info(*compile_kind)
233 .rustc_outputs(mode, target.kind(), triple)?;
234 let (dir, uplift_dir) = match target.kind() {
235 TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
236 (layout.build_examples(), Some(layout.examples()))
237 }
238 TargetKind::Test | TargetKind::Bench => (layout.deps(), None),
240 _ => (layout.deps(), Some(layout.dest())),
241 };
242 let mut dir_glob_str = escape_glob_path(dir)?;
243 let dir_glob = Path::new(&dir_glob_str);
244 for file_type in file_types {
245 let hashed_name = file_type.output_filename(target, Some("*"));
247 let unhashed_name = file_type.output_filename(target, None);
248
249 clean_ctx.rm_rf_glob(&dir_glob.join(&hashed_name))?;
250 clean_ctx.rm_rf(&dir.join(&unhashed_name))?;
251
252 if let Some(uplift_dir) = uplift_dir {
254 let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
255 clean_ctx.rm_rf(&uplifted_path)?;
256 let dep_info = uplifted_path.with_extension("d");
258 clean_ctx.rm_rf(&dep_info)?;
259 }
260 }
261 let unhashed_dep_info = dir.join(format!("{}.d", crate_name));
262 clean_ctx.rm_rf(&unhashed_dep_info)?;
263
264 if !dir_glob_str.ends_with(std::path::MAIN_SEPARATOR) {
265 dir_glob_str.push(std::path::MAIN_SEPARATOR);
266 }
267 dir_glob_str.push('*');
268 let dir_glob_str: Rc<str> = dir_glob_str.into();
269 if cleaned_packages
270 .entry(dir_glob_str.clone())
271 .or_default()
272 .insert(crate_name.clone())
273 {
274 let paths = [
275 (path_dash, ".d"),
278 (path_dot, ".o"),
280 (path_dot, ".dwo"),
281 (path_dot, ".dwp"),
282 ];
283 clean_ctx.rm_rf_prefix_list(&dir_glob_str, &paths)?;
284 }
285
286 let dir = escape_glob_path(layout.incremental())?;
288 let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
289 clean_ctx.rm_rf_glob(&incremental)?;
290 }
291 }
292 }
293 }
294
295 Ok(())
296}
297
298fn escape_glob_path(pattern: &Path) -> CargoResult<String> {
299 let pattern = pattern
300 .to_str()
301 .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?;
302 Ok(glob::Pattern::escape(pattern))
303}
304
305impl<'gctx> CleanContext<'gctx> {
306 pub fn new(gctx: &'gctx GlobalContext) -> Self {
307 let progress = CleaningFolderBar::new(gctx, 0);
310 CleanContext {
311 gctx,
312 progress: Box::new(progress),
313 dry_run: false,
314 num_files_removed: 0,
315 num_dirs_removed: 0,
316 total_bytes_removed: 0,
317 }
318 }
319
320 fn rm_rf_package_glob_containing_hash(
326 &mut self,
327 package: &str,
328 pattern: &Path,
329 ) -> CargoResult<()> {
330 let pattern = pattern
332 .to_str()
333 .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?;
334 for path in glob::glob(pattern)? {
335 let path = path?;
336
337 let pkg_name = path
338 .file_name()
339 .and_then(std::ffi::OsStr::to_str)
340 .and_then(|artifact| artifact.rsplit_once('-'))
341 .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?
342 .0;
343
344 if pkg_name != package {
345 continue;
346 }
347
348 self.rm_rf(&path)?;
349 }
350 Ok(())
351 }
352
353 fn rm_rf_glob(&mut self, pattern: &Path) -> CargoResult<()> {
354 let pattern = pattern
356 .to_str()
357 .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?;
358 for path in glob::glob(pattern)? {
359 self.rm_rf(&path?)?;
360 }
361 Ok(())
362 }
363
364 fn rm_rf_prefix_list(
371 &mut self,
372 pattern: &str,
373 path_matchers: &[(&str, &str)],
374 ) -> CargoResult<()> {
375 for path in glob::glob(pattern)? {
376 let path = path?;
377 let filename = path.file_name().and_then(|name| name.to_str()).unwrap();
378 if path_matchers
379 .iter()
380 .any(|(prefix, suffix)| filename.starts_with(prefix) && filename.ends_with(suffix))
381 {
382 self.rm_rf(&path)?;
383 }
384 }
385 Ok(())
386 }
387
388 pub fn rm_rf(&mut self, path: &Path) -> CargoResult<()> {
389 let meta = match fs::symlink_metadata(path) {
390 Ok(meta) => meta,
391 Err(e) => {
392 if e.kind() != std::io::ErrorKind::NotFound {
393 self.gctx
394 .shell()
395 .warn(&format!("cannot access {}: {e}", path.display()))?;
396 }
397 return Ok(());
398 }
399 };
400
401 if !self.dry_run {
403 self.gctx
404 .shell()
405 .verbose(|shell| shell.status("Removing", path.display()))?;
406 }
407 self.progress.display_now()?;
408
409 let mut rm_file = |path: &Path, meta: Result<std::fs::Metadata, _>| {
410 if let Ok(meta) = meta {
411 self.total_bytes_removed += meta.len();
415 }
416 self.num_files_removed += 1;
417 if !self.dry_run {
418 paths::remove_file(path)?;
419 }
420 Ok(())
421 };
422
423 if !meta.is_dir() {
424 return rm_file(path, Ok(meta));
425 }
426
427 for entry in walkdir::WalkDir::new(path).contents_first(true) {
428 let entry = entry?;
429 self.progress.on_clean()?;
430 if self.dry_run {
431 self.gctx
436 .shell()
437 .verbose(|shell| Ok(writeln!(shell.out(), "{}", entry.path().display())?))?;
438 }
439 if entry.file_type().is_dir() {
440 self.num_dirs_removed += 1;
441 if !self.dry_run {
446 paths::remove_dir_all(entry.path())?;
447 }
448 } else {
449 rm_file(entry.path(), entry.metadata())?;
450 }
451 }
452
453 Ok(())
454 }
455
456 pub fn display_summary(&self) -> CargoResult<()> {
457 let status = if self.dry_run { "Summary" } else { "Removed" };
458 let byte_count = if self.total_bytes_removed == 0 {
459 String::new()
460 } else {
461 let bytes = HumanBytes(self.total_bytes_removed);
462 format!(", {bytes:.1} total")
463 };
464 let file_count = match (self.num_files_removed, self.num_dirs_removed) {
470 (0, 0) => format!("0 files"),
471 (0, 1) => format!("1 directory"),
472 (0, 2..) => format!("{} directories", self.num_dirs_removed),
473 (1, _) => format!("1 file"),
474 (2.., _) => format!("{} files", self.num_files_removed),
475 };
476 self.gctx
477 .shell()
478 .status(status, format!("{file_count}{byte_count}"))?;
479 if self.dry_run {
480 self.gctx
481 .shell()
482 .warn("no files deleted due to --dry-run")?;
483 }
484 Ok(())
485 }
486
487 pub fn remove_paths(&mut self, paths: &[PathBuf]) -> CargoResult<()> {
493 let num_paths = paths
494 .iter()
495 .map(|path| walkdir::WalkDir::new(path).into_iter().count())
496 .sum();
497 self.progress = Box::new(CleaningFolderBar::new(self.gctx, num_paths));
498 for path in paths {
499 self.rm_rf(path)?;
500 }
501 Ok(())
502 }
503}
504
505trait CleaningProgressBar {
506 fn display_now(&mut self) -> CargoResult<()>;
507 fn on_clean(&mut self) -> CargoResult<()>;
508 fn on_cleaning_package(&mut self, _package: &str) -> CargoResult<()> {
509 Ok(())
510 }
511}
512
513struct CleaningFolderBar<'gctx> {
514 bar: Progress<'gctx>,
515 max: usize,
516 cur: usize,
517}
518
519impl<'gctx> CleaningFolderBar<'gctx> {
520 fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
521 Self {
522 bar: Progress::with_style("Cleaning", ProgressStyle::Percentage, gctx),
523 max,
524 cur: 0,
525 }
526 }
527
528 fn cur_progress(&self) -> usize {
529 std::cmp::min(self.cur, self.max)
530 }
531}
532
533impl<'gctx> CleaningProgressBar for CleaningFolderBar<'gctx> {
534 fn display_now(&mut self) -> CargoResult<()> {
535 self.bar.tick_now(self.cur_progress(), self.max, "")
536 }
537
538 fn on_clean(&mut self) -> CargoResult<()> {
539 self.cur += 1;
540 self.bar.tick(self.cur_progress(), self.max, "")
541 }
542}
543
544struct CleaningPackagesBar<'gctx> {
545 bar: Progress<'gctx>,
546 max: usize,
547 cur: usize,
548 num_files_folders_cleaned: usize,
549 package_being_cleaned: String,
550}
551
552impl<'gctx> CleaningPackagesBar<'gctx> {
553 fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
554 Self {
555 bar: Progress::with_style("Cleaning", ProgressStyle::Ratio, gctx),
556 max,
557 cur: 0,
558 num_files_folders_cleaned: 0,
559 package_being_cleaned: String::new(),
560 }
561 }
562
563 fn cur_progress(&self) -> usize {
564 std::cmp::min(self.cur, self.max)
565 }
566
567 fn format_message(&self) -> String {
568 format!(
569 ": {}, {} files/folders cleaned",
570 self.package_being_cleaned, self.num_files_folders_cleaned
571 )
572 }
573}
574
575impl<'gctx> CleaningProgressBar for CleaningPackagesBar<'gctx> {
576 fn display_now(&mut self) -> CargoResult<()> {
577 self.bar
578 .tick_now(self.cur_progress(), self.max, &self.format_message())
579 }
580
581 fn on_clean(&mut self) -> CargoResult<()> {
582 self.bar
583 .tick(self.cur_progress(), self.max, &self.format_message())?;
584 self.num_files_folders_cleaned += 1;
585 Ok(())
586 }
587
588 fn on_cleaning_package(&mut self, package: &str) -> CargoResult<()> {
589 self.cur += 1;
590 self.package_being_cleaned = String::from(package);
591 self.bar
592 .tick(self.cur_progress(), self.max, &self.format_message())
593 }
594}