Skip to content

Commit

Permalink
Auto merge of #8398 - ehuss:clean-reserved-name, r=Eh2406
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jun 22, 2020
2 parents b3b25ab + 6263d72 commit 10ae887
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::core::compiler::{CompileKind, CompileMode, Layout, RustcTargetData};
use crate::core::profiles::Profiles;
use crate::core::{InternedString, PackageIdSpec, Workspace};
use crate::core::{InternedString, PackageIdSpec, TargetKind, Workspace};
use crate::ops;
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::paths;
Expand Down Expand Up @@ -147,10 +147,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
let (file_types, _unsupported) = target_data
.info(*compile_kind)
.rustc_outputs(mode, target.kind(), triple)?;
let (dir, uplift_dir) = if target.is_example() {
(layout.examples(), layout.examples())
} else {
(layout.deps(), layout.dest())
let (dir, uplift_dir) = match target.kind() {
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
(layout.examples(), Some(layout.examples()))
}
// Tests/benchmarks are never uplifted.
TargetKind::Test | TargetKind::Bench => (layout.deps(), None),
_ => (layout.deps(), Some(layout.dest())),
};
for file_type in file_types {
// Some files include a hash in the filename, some don't.
Expand All @@ -166,11 +169,13 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
rm_rf(&unhashed_dep_info, config)?;

// Remove the uplifted copy.
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
rm_rf(&uplifted_path, config)?;
// Dep-info generated by Cargo itself.
let dep_info = uplifted_path.with_extension("d");
rm_rf(&dep_info, config)?;
if let Some(uplift_dir) = uplift_dir {
let uplifted_path = uplift_dir.join(file_type.uplift_filename(target));
rm_rf(&uplifted_path, config)?;
// Dep-info generated by Cargo itself.
let dep_info = uplifted_path.with_extension("d");
rm_rf(&dep_info, config)?;
}
}
// TODO: what to do about build_script_build?
let incremental = layout.incremental().join(format!("{}-*", crate_name));
Expand Down
52 changes: 52 additions & 0 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,55 @@ fn clean_spec_multiple() {
panic!("{:?} was not cleaned", e.path());
}
}

#[cargo_test]
fn clean_spec_reserved() {
// Clean when a target (like a test) has a reserved name. In this case,
// make sure `clean -p` doesn't delete the reserved directory `build` when
// there is a test named `build`.
Package::new("bar", "1.0.0")
.file("src/lib.rs", "")
.file("build.rs", "fn main() {}")
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.file("tests/build.rs", "")
.build();

p.cargo("build --all-targets").run();
assert!(p.target_debug_dir().join("build").is_dir());
let build_test = p.glob("target/debug/deps/build-*").next().unwrap().unwrap();
assert!(build_test.exists());
// Tests are never "uplifted".
assert!(p.glob("target/debug/build-*").next().is_none());

p.cargo("clean -p foo").run();
// Should not delete this.
assert!(p.target_debug_dir().join("build").is_dir());

// This should not rebuild bar.
p.cargo("build -v --all-targets")
.with_stderr(
"\
[FRESH] bar v1.0.0
[COMPILING] foo v0.1.0 [..]
[RUNNING] `rustc [..]
[RUNNING] `rustc [..]
[RUNNING] `rustc [..]
[FINISHED] [..]
",
)
.run();
}

0 comments on commit 10ae887

Please sign in to comment.