From 0c8498d195df377bd0982d33db0a0225772afd16 Mon Sep 17 00:00:00 2001 From: cavemanloverboy <93507302+cavemanloverboy@users.noreply.github.com> Date: Sat, 27 May 2023 06:53:02 -0700 Subject: [PATCH] cli: Exit `anchor clean` without error when dirs don't exist (#2504) --- cli/src/lib.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 0d2dd1f386..8d305f5446 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -3051,23 +3051,31 @@ fn clean(cfg_override: &ConfigOverride) -> Result<()> { let target_dir = cfg_parent.join("target"); let deploy_dir = target_dir.join("deploy"); - for entry in fs::read_dir(target_dir)? { - let path = entry?.path(); - if path.is_dir() && path != deploy_dir { - fs::remove_dir_all(&path) - .map_err(|e| anyhow!("Could not remove directory {}: {}", path.display(), e))?; - } else if path.is_file() { - fs::remove_file(&path) - .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?; + if target_dir.exists() { + for entry in fs::read_dir(target_dir)? { + let path = entry?.path(); + if path.is_dir() && path != deploy_dir { + fs::remove_dir_all(&path) + .map_err(|e| anyhow!("Could not remove directory {}: {}", path.display(), e))?; + } else if path.is_file() { + fs::remove_file(&path) + .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?; + } } + } else { + println!("skipping target directory: not found") } - for file in fs::read_dir(deploy_dir)? { - let path = file?.path(); - if path.extension() != Some(&OsString::from("json")) { - fs::remove_file(&path) - .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?; + if deploy_dir.exists() { + for file in fs::read_dir(deploy_dir)? { + let path = file?.path(); + if path.extension() != Some(&OsString::from("json")) { + fs::remove_file(&path) + .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?; + } } + } else { + println!("skipping deploy directory: not found") } Ok(())