Skip to content

Commit e99f374

Browse files
committed
fix(clean): Clean up incremental builds with new layout
1 parent f1f686c commit e99f374

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ fn clean_specs(
209209
if target.is_custom_build() {
210210
continue;
211211
}
212+
let crate_name: Rc<str> = target.crate_name().into();
212213
for &mode in &[
213214
CompileMode::Build,
214215
CompileMode::Test,
@@ -240,6 +241,10 @@ fn clean_specs(
240241
clean_ctx.rm_rf(&dep_info)?;
241242
}
242243
}
244+
245+
let dir = escape_glob_path(layout.build_dir().incremental())?;
246+
let incremental = Path::new(&dir).join(format!("{}-*", crate_name));
247+
clean_ctx.rm_rf_glob(&incremental)?;
243248
}
244249
}
245250
}

tests/testsuite/clean_new_layout.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ fn clean_multiple_packages_in_glob_char_path() {
142142
p.cargo("clean -p foo")
143143
.arg("-Zbuild-dir-new-layout")
144144
.masquerade_as_nightly_cargo(&["new build-dir layout"])
145+
.with_stderr_data(str![[r#"
146+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
147+
148+
"#]])
145149
.run();
146150
assert_eq!(get_build_artifacts(foo_path, file_glob).len(), 0);
147151
}
@@ -598,7 +602,7 @@ fn package_cleans_all_the_things() {
598602
.arg("-Zbuild-dir-new-layout")
599603
.masquerade_as_nightly_cargo(&["new build-dir layout"])
600604
.run();
601-
//assert_all_clean(&p.build_dir()); // FIXME
605+
assert_all_clean(&p.build_dir());
602606
}
603607
let p = project()
604608
.file(
@@ -657,7 +661,7 @@ fn package_cleans_all_the_things() {
657661
.arg("-Zbuild-dir-new-layout")
658662
.masquerade_as_nightly_cargo(&["new build-dir layout"])
659663
.run();
660-
//assert_all_clean(&p.build_dir()); // FIXME
664+
assert_all_clean(&p.build_dir());
661665

662666
// Try some targets.
663667
p.cargo("build --all-targets --target")
@@ -670,7 +674,43 @@ fn package_cleans_all_the_things() {
670674
.arg("-Zbuild-dir-new-layout")
671675
.masquerade_as_nightly_cargo(&["new build-dir layout"])
672676
.run();
673-
//assert_all_clean(&p.build_dir()); // FIXME
677+
assert_all_clean(&p.build_dir());
678+
}
679+
680+
// Ensures that all files for the package have been deleted.
681+
#[track_caller]
682+
fn assert_all_clean(build_dir: &Path) {
683+
let walker = walkdir::WalkDir::new(build_dir).into_iter();
684+
for entry in walker.filter_entry(|e| {
685+
let path = e.path();
686+
// This is a known limitation, clean can't differentiate between
687+
// the different build scripts from different packages.
688+
!(path
689+
.file_name()
690+
.unwrap()
691+
.to_str()
692+
.unwrap()
693+
.starts_with("build_script_build")
694+
&& path
695+
.parent()
696+
.unwrap()
697+
.file_name()
698+
.unwrap()
699+
.to_str()
700+
.unwrap()
701+
== "incremental")
702+
}) {
703+
let entry = entry.unwrap();
704+
let path = entry.path();
705+
if let ".rustc_info.json" | ".cargo-lock" | "CACHEDIR.TAG" =
706+
path.file_name().unwrap().to_str().unwrap()
707+
{
708+
continue;
709+
}
710+
if path.is_symlink() || path.is_file() {
711+
panic!("{:?} was not cleaned", path);
712+
}
713+
}
674714
}
675715

676716
#[cargo_test]

0 commit comments

Comments
 (0)