Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,19 @@ 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();

// should not error if target_dir does not exist
p.cargo("clean --target-dir bar")
.with_stderr_data(str![[r#"
[REMOVED] 0 files

"#]])
.run();
}
18 changes: 18 additions & 0 deletions tests/testsuite/clean_new_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,3 +1345,21 @@ 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();

// 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#"
[REMOVED] 0 files

"#]])
.run();
}