From 71e941ffc6f8aa63f797e7738f078cdc36e98697 Mon Sep 17 00:00:00 2001 From: Tanmay Arya Date: Thu, 23 Apr 2026 19:03:33 +0530 Subject: [PATCH 1/2] cargo clean: add test showing cargo error when explicitely specified target-dir does not exist --- tests/testsuite/clean.rs | 18 ++++++++++++++++++ tests/testsuite/clean_new_layout.rs | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 0ad0ed2869b..13c77e7a10e 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -1450,3 +1450,21 @@ fn config_target_dir_tag_valid() { p.cargo("clean").run(); } + +#[cargo_test] +fn explicit_target_dir_not_exists() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("clean --target-dir bar") + .with_stderr_data(str![[r#" +[ERROR] cannot clean `[ROOT]/foo/bar`: missing or invalid `CACHEDIR.TAG` file + | + = [NOTE] cleaning has been aborted to prevent accidental deletion of unrelated files + +"#]]) + .with_status(101) + .run(); +} diff --git a/tests/testsuite/clean_new_layout.rs b/tests/testsuite/clean_new_layout.rs index 0dd9c0149e7..118b88c172a 100644 --- a/tests/testsuite/clean_new_layout.rs +++ b/tests/testsuite/clean_new_layout.rs @@ -1345,3 +1345,23 @@ fn config_target_dir_tag_valid() { .masquerade_as_nightly_cargo(&["new build-dir layout"]) .run(); } + +#[cargo_test] +fn explicit_target_dir_not_exists() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("clean --target-dir bar") + .arg("-Zbuild-dir-new-layout") + .masquerade_as_nightly_cargo(&["new build-dir layout"]) + .with_stderr_data(str![[r#" +[ERROR] cannot clean `[ROOT]/foo/bar`: missing or invalid `CACHEDIR.TAG` file + | + = [NOTE] cleaning has been aborted to prevent accidental deletion of unrelated files + +"#]]) + .with_status(101) + .run(); +} From 587a8a2b548dff671e3d3ac13ad434be31559545 Mon Sep 17 00:00:00 2001 From: Tanmay Arya Date: Thu, 23 Apr 2026 19:10:39 +0530 Subject: [PATCH 2/2] cargo clean: do not error if explicit target-dir does not exist --- src/cargo/ops/cargo_clean.rs | 6 ++++-- tests/testsuite/clean.rs | 6 ++---- tests/testsuite/clean_new_layout.rs | 6 ++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 34c74afed5b..84970c90aee 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -73,8 +73,10 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> { if opts.explicit_target_dir_arg { let target_dir_path = target_dir.as_path_unlocked(); - // check if the target directory has a valid CACHEDIR.TAG - if let Err(err) = validate_target_dir_tag(target_dir_path) { + // perform validation on target_dir only if it exists and check if the target directory has a valid CACHEDIR.TAG + if target_dir_path.exists() + && let Err(err) = validate_target_dir_tag(target_dir_path) + { // if target_dir was passed explicitly via --target-dir, then hard error if validation fails let title = format!("cannot clean `{}`: {err}", target_dir_path.display()); let report = [Level::ERROR diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index 13c77e7a10e..ccc294fe390 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -1458,13 +1458,11 @@ fn explicit_target_dir_not_exists() { .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); + // should not error if target_dir does not exist p.cargo("clean --target-dir bar") .with_stderr_data(str![[r#" -[ERROR] cannot clean `[ROOT]/foo/bar`: missing or invalid `CACHEDIR.TAG` file - | - = [NOTE] cleaning has been aborted to prevent accidental deletion of unrelated files +[REMOVED] 0 files "#]]) - .with_status(101) .run(); } diff --git a/tests/testsuite/clean_new_layout.rs b/tests/testsuite/clean_new_layout.rs index 118b88c172a..425d09224a8 100644 --- a/tests/testsuite/clean_new_layout.rs +++ b/tests/testsuite/clean_new_layout.rs @@ -1353,15 +1353,13 @@ fn explicit_target_dir_not_exists() { .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) .build(); + // should not error if target_dir does not exist p.cargo("clean --target-dir bar") .arg("-Zbuild-dir-new-layout") .masquerade_as_nightly_cargo(&["new build-dir layout"]) .with_stderr_data(str![[r#" -[ERROR] cannot clean `[ROOT]/foo/bar`: missing or invalid `CACHEDIR.TAG` file - | - = [NOTE] cleaning has been aborted to prevent accidental deletion of unrelated files +[REMOVED] 0 files "#]]) - .with_status(101) .run(); }