Skip to content

The agent that grows with you

fix(pm): clear read-only bits with a pm-local helper, not hermes_cli

teknium13a744c7d6644
diff --git a/pm/filesystem.py b/pm/filesystem.py
index f7dc30b4823..1c687b6b0c5 100644
--- a/pm/filesystem.py
+++ b/pm/filesystem.py
@@ -9,7 +9,9 @@ import errno
import hashlib
import os
from pathlib import Path
+import shutil
import stat
+import sys
import tempfile
import time
@@ -80,3 +82,18 @@ def durable_write_bytes(path: Path, data: bytes) -> None:
os.close(directory)
finally:
Path(temporary).unlink(missing_ok=True)
+
+
+def remove_tree(path: Path) -> None:
+ """``shutil.rmtree`` that clears a read-only bit and retries the failing unlink.
+
+ Windows refuses to unlink read-only files (PortableGit ships ``etc/hosts`` read-only), and no
+ retry clears that bit. Lives here, not in ``hermes_cli``: PM installs run before the rest of
+ the tree exists (the Docker toolchain stage copies only ``pm/``).
+ """
+ def _retry_writable(func, failed, _exc):
+ os.chmod(failed, stat.S_IWRITE)
+ func(failed)
+
+ handler = {"onexc": _retry_writable} if sys.version_info >= (3, 12) else {"onerror": _retry_writable}
+ shutil.rmtree(path, **handler)
diff --git a/pm/install.py b/pm/install.py
index 3b45415c22d..434fc812e1e 100644
--- a/pm/install.py
+++ b/pm/install.py
@@ -15,6 +15,7 @@ from typing import Optional
from pm import paths
from pm.downloader import DownloadPaused, ProgressFn
+from pm.filesystem import remove_tree
from pm.lock import Facts, Lockfile
from pm.package import InstallError, Package, Runner, StatePackage, compose_env
from pm.plugin_inputs import Candidates, Members, PluginInput, Selection, StagedUpdate
@@ -225,17 +226,13 @@ def _remove_entry(store: Store, entry_name: str, *, attempts: int = 5) -> None:
"""
import time
- from hermes_cli.fs_utils import rmtree_force
-
entry = store.entry(entry_name)
for attempt in range(attempts):
try:
if entry.is_symlink() or not entry.is_dir():
entry.unlink(missing_ok=True)
else:
- # Windows refuses to unlink read-only files (PortableGit ships etc/hosts
- # read-only); no retry clears that bit, so the tree could never be removed.
- rmtree_force(entry)
+ remove_tree(entry)
return
except FileNotFoundError:
return