Skip to content

Commit 05d080f

Browse files
committed
Auto merge of #8216 - ehuss:beta-1.44-clean-p-fix, r=alexcrichton
[beta] Fix `clean -p` with a build dependency. This is a temporary and simple fix for #8149 where `cargo clean -p foo` would fail if `foo` has a build dependency. The full fix is in #8210, but I think that PR is way too risky to backport.
2 parents ff91911 + 5a6849e commit 05d080f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
112112
for target in pkg.targets() {
113113
for kind in [CompileKind::Host, build_config.requested_kind].iter() {
114114
for mode in CompileMode::all_modes() {
115+
if target.is_custom_build() && (mode.is_any_test() || !kind.is_host()) {
116+
// Workaround where the UnitFor code will panic
117+
// because it is not expecting strange combinations
118+
// like "testing a build script".
119+
continue;
120+
}
115121
for unit_for in UnitFor::all_values() {
116122
let profile = if mode.is_run_custom_build() {
117123
bcx.profiles

tests/testsuite/clean.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,41 @@ fn clean_remove_rlib_rmeta() {
319319
assert!(!p.target_debug_dir().join("libfoo.rlib").exists());
320320
assert!(!rmeta.exists());
321321
}
322+
323+
#[cargo_test]
324+
fn clean_with_build_dep() {
325+
// Test for panic when there was a build dep with `-p`.
326+
Package::new("bar", "0.1.0").publish();
327+
let p = project()
328+
.file(
329+
"Cargo.toml",
330+
r#"
331+
[package]
332+
name = "foo"
333+
version = "0.1.0"
334+
335+
[build-dependencies]
336+
bar = "0.1"
337+
"#,
338+
)
339+
.file("src/lib.rs", "")
340+
.file("build.rs", "fn main() {}")
341+
.build();
342+
343+
p.cargo("build").run();
344+
// Two build directories, one for the executable, one for the run output.
345+
assert_eq!(p.glob(p.target_debug_dir().join("build/foo-*")).count(), 2);
346+
// Produces both an rlib and rmeta.
347+
assert_eq!(
348+
p.glob(p.target_debug_dir().join("deps/libfoo-*.*")).count(),
349+
2
350+
);
351+
p.cargo("clean -p foo").run();
352+
// `clean -p` doesn't clean the output directory, it should.
353+
// Will be fixed via https://github.com/rust-lang/cargo/pull/8210
354+
assert_eq!(p.glob(p.target_debug_dir().join("build/foo-*")).count(), 1);
355+
assert_eq!(
356+
p.glob(p.target_debug_dir().join("deps/libfoo-*.*")).count(),
357+
0
358+
);
359+
}

0 commit comments

Comments
 (0)