From 9f28c6251da437edff32344df5687e28fb4485b6 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Wed, 17 Dec 2025 18:31:03 +0000 Subject: [PATCH] fix: proper skills dir cleanup (#8194) --- codex-rs/core/src/skills/public.rs | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/codex-rs/core/src/skills/public.rs b/codex-rs/core/src/skills/public.rs index 711dad979..8909d6a55 100644 --- a/codex-rs/core/src/skills/public.rs +++ b/codex-rs/core/src/skills/public.rs @@ -11,6 +11,35 @@ const PUBLIC_SKILLS_REPO_URL: &str = "https://github.com/openai/skills.git"; const PUBLIC_SKILLS_DIR_NAME: &str = ".public"; const SKILLS_DIR_NAME: &str = "skills"; +struct TempDirCleanup { + path: PathBuf, + // Disable Drop cleanup after explicit cleanup to avoid double-delete. + active: bool, +} + +impl TempDirCleanup { + fn new(path: PathBuf) -> Self { + Self { path, active: true } + } + + fn cleanup(&mut self) -> Result<(), PublicSkillsError> { + if self.active && self.path.exists() { + fs::remove_dir_all(&self.path) + .map_err(|source| PublicSkillsError::io("remove public skills tmp dir", source))?; + } + self.active = false; + Ok(()) + } +} + +impl Drop for TempDirCleanup { + fn drop(&mut self) { + if self.active && self.path.exists() { + let _ = fs::remove_dir_all(&self.path); + } + } +} + pub(crate) fn public_cache_root_dir(codex_home: &Path) -> PathBuf { codex_home .join(SKILLS_DIR_NAME) @@ -68,6 +97,7 @@ fn refresh_public_skills_inner( } fs::create_dir_all(&tmp_dir) .map_err(|source| PublicSkillsError::io("create public skills tmp dir", source))?; + let mut tmp_dir_cleanup = TempDirCleanup::new(tmp_dir.clone()); let checkout_dir = tmp_dir.join("checkout"); clone_repo(repo_url, &checkout_dir)?; @@ -87,8 +117,7 @@ fn refresh_public_skills_inner( atomic_swap_dir(&staged_public, &dest_public, &skills_root_dir)?; - fs::remove_dir_all(&tmp_dir) - .map_err(|source| PublicSkillsError::io("remove public skills tmp dir", source))?; + tmp_dir_cleanup.cleanup()?; Ok(PublicSkillsRefreshOutcome::Updated) }