From 6263d726f6b32ff7bcb5e1c292e0c83d65145e41 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 22 Jun 2020 12:56:10 -0700 Subject: [PATCH] Fix overzealous `clean -p` for reserved names. --- src/cargo/ops/cargo_clean.rs | 25 ++++++++++------- tests/testsuite/clean.rs | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index a8b39c4b5b9..d4bc8ce5295 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -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; @@ -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. @@ -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)); diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 673dc4969a1..b24ad390c0b 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -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(); +}