diff --git a/src/cargo/core/compiler/build_runner/compilation_files.rs b/src/cargo/core/compiler/build_runner/compilation_files.rs index f09e591bd33..f6817a60a85 100644 --- a/src/cargo/core/compiler/build_runner/compilation_files.rs +++ b/src/cargo/core/compiler/build_runner/compilation_files.rs @@ -220,7 +220,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { panic!("doc tests do not have an out dir"); } else if unit.target.is_custom_build() { self.build_script_dir(unit) - } else if unit.target.is_example() { + } else if unit.target.is_example() && !self.ws.gctx().cli_unstable().build_dir_new_layout { self.layout(unit.kind).build_dir().examples().to_path_buf() } else if unit.artifact.is_true() { self.artifact_dir(unit) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 37f205c6250..dd4c3993fc1 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -209,6 +209,7 @@ fn clean_specs( if target.is_custom_build() { continue; } + let crate_name: Rc = target.crate_name().into(); for &mode in &[ CompileMode::Build, CompileMode::Test, @@ -240,6 +241,10 @@ fn clean_specs( clean_ctx.rm_rf(&dep_info)?; } } + + let dir = escape_glob_path(layout.build_dir().incremental())?; + let incremental = Path::new(&dir).join(format!("{}-*", crate_name)); + clean_ctx.rm_rf_glob(&incremental)?; } } } diff --git a/tests/testsuite/build_dir.rs b/tests/testsuite/build_dir.rs index 8e409bcf932..d2b7ebf4260 100644 --- a/tests/testsuite/build_dir.rs +++ b/tests/testsuite/build_dir.rs @@ -396,8 +396,8 @@ fn examples_should_output_to_build_dir_and_uplift_to_target_dir() { [ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/example-foo [ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/example-foo.json [ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/invoked.timestamp -[ROOT]/foo/build-dir/debug/examples/foo[..][EXE] -[ROOT]/foo/build-dir/debug/examples/foo[..].d +[ROOT]/foo/build-dir/debug/build/foo/[HASH]/deps/foo[..][EXE] +[ROOT]/foo/build-dir/debug/build/foo/[HASH]/deps/foo[..].d "#]]); diff --git a/tests/testsuite/clean_new_layout.rs b/tests/testsuite/clean_new_layout.rs index 33d618fabec..ad4b01c982b 100644 --- a/tests/testsuite/clean_new_layout.rs +++ b/tests/testsuite/clean_new_layout.rs @@ -142,6 +142,10 @@ fn clean_multiple_packages_in_glob_char_path() { p.cargo("clean -p foo") .arg("-Zbuild-dir-new-layout") .masquerade_as_nightly_cargo(&["new build-dir layout"]) + .with_stderr_data(str![[r#" +[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total + +"#]]) .run(); assert_eq!(get_build_artifacts(foo_path, file_glob).len(), 0); } @@ -598,7 +602,7 @@ fn package_cleans_all_the_things() { .arg("-Zbuild-dir-new-layout") .masquerade_as_nightly_cargo(&["new build-dir layout"]) .run(); - //assert_all_clean(&p.build_dir()); // FIXME + assert_all_clean(&p.build_dir()); } let p = project() .file( @@ -657,7 +661,7 @@ fn package_cleans_all_the_things() { .arg("-Zbuild-dir-new-layout") .masquerade_as_nightly_cargo(&["new build-dir layout"]) .run(); - //assert_all_clean(&p.build_dir()); // FIXME + assert_all_clean(&p.build_dir()); // Try some targets. p.cargo("build --all-targets --target") @@ -670,7 +674,43 @@ fn package_cleans_all_the_things() { .arg("-Zbuild-dir-new-layout") .masquerade_as_nightly_cargo(&["new build-dir layout"]) .run(); - //assert_all_clean(&p.build_dir()); // FIXME + assert_all_clean(&p.build_dir()); +} + +// Ensures that all files for the package have been deleted. +#[track_caller] +fn assert_all_clean(build_dir: &Path) { + let walker = walkdir::WalkDir::new(build_dir).into_iter(); + for entry in walker.filter_entry(|e| { + let path = e.path(); + // This is a known limitation, clean can't differentiate between + // the different build scripts from different packages. + !(path + .file_name() + .unwrap() + .to_str() + .unwrap() + .starts_with("build_script_build") + && path + .parent() + .unwrap() + .file_name() + .unwrap() + .to_str() + .unwrap() + == "incremental") + }) { + let entry = entry.unwrap(); + let path = entry.path(); + if let ".rustc_info.json" | ".cargo-lock" | "CACHEDIR.TAG" = + path.file_name().unwrap().to_str().unwrap() + { + continue; + } + if path.is_symlink() || path.is_file() { + panic!("{:?} was not cleaned", path); + } + } } #[cargo_test]