Skip to content

Commit e0dd406

Browse files
authored
fix(layout): Put examples in their unit dir in new layout (#16335)
### What does this PR try to resolve? Before, pre-uplifted examples were being written to `{build-dir}/{profile}/examples/{name}-{hash}`. Now they are being written to `{build-dir}/{profile}/build/{pkgname}/{hash}/{name}-{hash}`. This does not affect uplifted examples (they lack the `-{hash}`). Pre-build-dir, these were placed in a "public" location but I don't see a reason for that to be the case (otherwise, we'd need to switch them to `{target-dir}/{profile}/examples/{name}-{hash}`). In that case, I don't see a reason to treat examples as any different than any other artifact and going in their unique build init directory. Fixes #16302 ### How to test and review this PR?
2 parents 9e691e7 + 16ab9d7 commit e0dd406

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/cargo/core/compiler/build_runner/compilation_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
220220
panic!("doc tests do not have an out dir");
221221
} else if unit.target.is_custom_build() {
222222
self.build_script_dir(unit)
223-
} else if unit.target.is_example() {
223+
} else if unit.target.is_example() && !self.ws.gctx().cli_unstable().build_dir_new_layout {
224224
self.layout(unit.kind).build_dir().examples().to_path_buf()
225225
} else if unit.artifact.is_true() {
226226
self.artifact_dir(unit)

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/build_dir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ fn examples_should_output_to_build_dir_and_uplift_to_target_dir() {
396396
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/example-foo
397397
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/example-foo.json
398398
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/invoked.timestamp
399-
[ROOT]/foo/build-dir/debug/examples/foo[..][EXE]
400-
[ROOT]/foo/build-dir/debug/examples/foo[..].d
399+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/deps/foo[..][EXE]
400+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/deps/foo[..].d
401401
402402
"#]]);
403403

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)