Skip to content

Commit 10ae887

Browse files
committed
Auto merge of #8398 - ehuss:clean-reserved-name, r=Eh2406
Fix overzealous `clean -p` for reserved names. #8210 changed the way `clean -p` worked, but in some ways it is a little too sloppy. If a package has a test named `build`, then it would delete the `build` directory thinking an executable named "build" exists. This changes it so that it does not attempt to delete tests/benches from the uplift directory.
2 parents b3b25ab + 6263d72 commit 10ae887

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::core::compiler::{CompileKind, CompileMode, Layout, RustcTargetData};
22
use crate::core::profiles::Profiles;
3-
use crate::core::{InternedString, PackageIdSpec, Workspace};
3+
use crate::core::{InternedString, PackageIdSpec, TargetKind, Workspace};
44
use crate::ops;
55
use crate::util::errors::{CargoResult, CargoResultExt};
66
use crate::util::paths;
@@ -147,10 +147,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
147147
let (file_types, _unsupported) = target_data
148148
.info(*compile_kind)
149149
.rustc_outputs(mode, target.kind(), triple)?;
150-
let (dir, uplift_dir) = if target.is_example() {
151-
(layout.examples(), layout.examples())
152-
} else {
153-
(layout.deps(), layout.dest())
150+
let (dir, uplift_dir) = match target.kind() {
151+
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
152+
(layout.examples(), Some(layout.examples()))
153+
}
154+
// Tests/benchmarks are never uplifted.
155+
TargetKind::Test | TargetKind::Bench => (layout.deps(), None),
156+
_ => (layout.deps(), Some(layout.dest())),
154157
};
155158
for file_type in file_types {
156159
// Some files include a hash in the filename, some don't.
@@ -166,11 +169,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
166169
rm_rf(&unhashed_dep_info, config)?;
167170

168171
// Remove the uplifted copy.
169-
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
170-
rm_rf(&uplifted_path, config)?;
171-
// Dep-info generated by Cargo itself.
172-
let dep_info = uplifted_path.with_extension("d");
173-
rm_rf(&dep_info, config)?;
172+
if let Some(uplift_dir) = uplift_dir {
173+
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
174+
rm_rf(&uplifted_path, config)?;
175+
// Dep-info generated by Cargo itself.
176+
let dep_info = uplifted_path.with_extension("d");
177+
rm_rf(&dep_info, config)?;
178+
}
174179
}
175180
// TODO: what to do about build_script_build?
176181
let incremental = layout.incremental().join(format!("{}-*", crate_name));

tests/testsuite/clean.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,55 @@ fn clean_spec_multiple() {
481481
panic!("{:?} was not cleaned", e.path());
482482
}
483483
}
484+
485+
#[cargo_test]
486+
fn clean_spec_reserved() {
487+
// Clean when a target (like a test) has a reserved name. In this case,
488+
// make sure `clean -p` doesn't delete the reserved directory `build` when
489+
// there is a test named `build`.
490+
Package::new("bar", "1.0.0")
491+
.file("src/lib.rs", "")
492+
.file("build.rs", "fn main() {}")
493+
.publish();
494+
495+
let p = project()
496+
.file(
497+
"Cargo.toml",
498+
r#"
499+
[package]
500+
name = "foo"
501+
version = "0.1.0"
502+
503+
[dependencies]
504+
bar = "1.0"
505+
"#,
506+
)
507+
.file("src/lib.rs", "")
508+
.file("tests/build.rs", "")
509+
.build();
510+
511+
p.cargo("build --all-targets").run();
512+
assert!(p.target_debug_dir().join("build").is_dir());
513+
let build_test = p.glob("target/debug/deps/build-*").next().unwrap().unwrap();
514+
assert!(build_test.exists());
515+
// Tests are never "uplifted".
516+
assert!(p.glob("target/debug/build-*").next().is_none());
517+
518+
p.cargo("clean -p foo").run();
519+
// Should not delete this.
520+
assert!(p.target_debug_dir().join("build").is_dir());
521+
522+
// This should not rebuild bar.
523+
p.cargo("build -v --all-targets")
524+
.with_stderr(
525+
"\
526+
[FRESH] bar v1.0.0
527+
[COMPILING] foo v0.1.0 [..]
528+
[RUNNING] `rustc [..]
529+
[RUNNING] `rustc [..]
530+
[RUNNING] `rustc [..]
531+
[FINISHED] [..]
532+
",
533+
)
534+
.run();
535+
}

0 commit comments

Comments
 (0)