The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pm): PortableGit unpack raises InstallError and never pipes the extractor
kshitijk4poor084645fba4ab
diff --git a/pm/packages.py b/pm/packages.pyindex 78ec764e422..a42ebf64df0 100644--- a/pm/packages.py+++ b/pm/packages.py@@ -622,14 +622,11 @@ _HOST_IS_WINDOWS = os.name == "nt"@registerclass Git(BinaryPackage):"""Windows only: Git for Windows carries the bash.exe contract. POSIX- uses the system git - a deliberate gap, not an oversight. The pinned- PortableGit asset is a self-extracting 7z: it carries its own extractor,- so no tar bzip2 filter and no bzip2.exe are needed on the machine- (#122512). The stub still shows an Extracting progress window (-y only- suppresses prompts and error boxes), and its RunProgram then runs the- vendor post-install (git-bash hardlinking, etc/ hosts files, mtab- snapshot), so the staged tree is post-install output - the old tar.bz2- pin never ran post-install."""+ uses the system git - a deliberate gap, not an oversight. The pin is the+ PortableGit self-extracting 7z: it carries its own extractor (stock+ Windows 10 tar.exe has no bzip2), shows a progress window (-y only drops+ prompts), and runs the vendor post-install, so the staged tree is+ post-install output."""name = "git"optional = True@@ -662,46 +659,44 @@ class Git(BinaryPackage):return outdef unpack(self, archive: Path, staged: Path, target: str) -> None:- import shutil- import subprocess-if not _HOST_IS_WINDOWS:- raise RuntimeError(- "the pinned git artifact is a PortableGit self-extracting 7z: "- "staging the win32 git target runs the vendor extractor and "- "requires a Windows host (POSIX hosts get git from the system "- "tool, see this package's gaps)"+ raise InstallError(+ self.name,+ "the pinned artifact is a PortableGit self-extracting 7z, which "+ "only runs on Windows",+ "stage win32 git on a Windows host; POSIX hosts use the system git",)- staged.mkdir(parents=True, exist_ok=True)- # Never execute the cached fetch-<sha> bytes: an executed PE can stay- # handle-held (Defender on-execute scan, the stub's RunProgram child- # chain) past pm's ~2 s download-cleanup retry and fail the install- # with WinError 32 (CI run 36189416163). Execute a copy beside the- # staging tree instead: pm's .staging-* scratch teardown owns that- # path and ignores errors, so any hold lands on a disposable path.- work = Path(tempfile.mkdtemp(prefix=".sfx-", dir=staged.parent))- exe = work / archive.name- try:+ shutil.rmtree(staged, ignore_errors=True)+ staged.mkdir(parents=True)+ # Execute a scratch copy, never the cached fetch-<sha> bytes: an executed+ # PE can stay handle-held (on-execute AV scan, the RunProgram children)+ # past pm's download-cleanup retry and fail the install (WinError 32).+ with tempfile.TemporaryDirectory(+ prefix=".sfx-", dir=staged.parent, ignore_cleanup_errors=True+ ) as work:+ exe = Path(work) / archive.nameshutil.copy2(archive, exe)- proc = subprocess.run(- [str(exe), f"-o{staged}", "-y"],- capture_output=True,- text=True,- timeout=600,- )- if proc.returncode:- # Under -y the GUI stub is silent on every channel; say so and- # name the usual causes instead of promising captured output.- tail = (proc.stderr or proc.stdout or "").strip()[-200:]- raise RuntimeError(- f"PortableGit self-extractor failed with exit code {proc.returncode}; "- "under -y the GUI stub is silent (no stdout, stderr, or error box), "- "so the usual causes are disk full, a path-length limit, or an "- "antivirus lock"- + (f" -- extractor output: {tail}" if tail else "")+ # No pipes: RunProgram children would inherit them and hold run()+ # open past the stub's exit. Under -y the stub prints nothing anyway.+ try:+ proc = subprocess.run(+ [str(exe), f"-o{staged}", "-y"],+ stdin=subprocess.DEVNULL,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ timeout=600,)- finally:- shutil.rmtree(work, ignore_errors=True)+ except subprocess.TimeoutExpired:+ raise InstallError(+ self.name, "PortableGit self-extractor did not finish in 600 s"+ ) from None+ if proc.returncode:+ raise InstallError(+ self.name,+ f"PortableGit self-extractor exited {proc.returncode} (it reports "+ "nothing under -y; usual causes: disk full, path-length limit, "+ "antivirus lock)",+ )def env(self, entry: Path, target: str) -> dict:return {"PATH": [str(entry / "cmd"), str(entry / "usr" / "bin")]}diff --git a/tests/pm/test_store_extract.py b/tests/pm/test_store_extract.pyindex 2f00fd2417e..acc1c3c8ac5 100644--- a/tests/pm/test_store_extract.py+++ b/tests/pm/test_store_extract.py@@ -42,97 +42,90 @@ def test_symlinks_escaping_the_destination_are_rejected(tmp_path, linkname):extract(archive, tmp_path / "out")assert not (tmp_path / "out" / "python/bin/evil").is_symlink()-def test_git_pin_is_the_self_extracting_archive():- """Both win32 targets pin the self-extracting PortableGit archive: it- carries its own extractor, so no tar/bzip2 is needed anywhere (#122512)."""- from pm.packages import Git- for target in ("win32-x64", "win32-arm64"):- assert Git().fetch_url("2.53.0+3", target).endswith(".7z.exe")+def _portable_git(tmp_path):+ archive = tmp_path / "fetch" / "PortableGit-2.53.0.3-64-bit.7z.exe"+ archive.parent.mkdir()+ archive.write_bytes(b"pinned bytes; the fake run never executes them")+ return archive+++def _fake_run(monkeypatch, returncode=0, calls=None):+ import subprocess++ def run(argv, **kwargs):+ if calls is not None:+ calls.append((argv, kwargs))+ return subprocess.CompletedProcess(argv, returncode)++ monkeypatch.setattr(subprocess, "run", run)def test_git_unpack_executes_a_scratch_copy_never_the_cached_bytes(tmp_path, monkeypatch):- """Executing the cached fetch-<sha> artifact in place kept it handle-held- (Defender on-execute scan, the stub's RunProgram child chain) past pm's- ~2 s download-cleanup retry and failed the install with WinError 32- (CI run 36189416163). The extractor must run from a disposable copy- beside the staging tree, where the .staging-* teardown owns it."""+ """An executed PE can stay handle-held past pm's download-cleanup retry+ (WinError 32), so the extractor runs from a disposable copy beside the+ staging tree, never from the cached fetch-<sha> entry."""import subprocessfrom pathlib import Pathimport pm.packagesfrom pm.packages import Git- # The pinned asset is a PE self-extractor: pin the host flag so the POSIX- # guard (tested separately) doesn't short-circuit the behaviour here.- monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True, raising=False)-+ monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True)calls = []-- class _Result:- returncode = 0- stdout = ""- stderr = ""-- monkeypatch.setattr(subprocess, "run", lambda argv, **kw: calls.append(argv) or _Result())- archive = tmp_path / "fetch" / "PortableGit-2.53.0.3-64-bit.7z.exe"- archive.parent.mkdir()- archive.write_bytes(b"pinned bytes; the fake run never executes them")+ _fake_run(monkeypatch, calls=calls)+ archive = _portable_git(tmp_path)staged = tmp_path / "scratch" / "tree"Git().unpack(archive, staged, "win32-x64")- argv = calls[0]+ (argv, kwargs), = callsassert argv[1:] == [f"-o{staged}", "-y"]- assert argv[0] != str(archive)- assert archive.parent not in Path(argv[0]).parents- assert staged.parent in Path(argv[0]).parents+ exe = Path(argv[0])+ assert archive.parent not in exe.parents+ assert staged.parent in exe.parents+ assert not exe.exists(), "the scratch copy is removed after the run"+ # Inherited pipes would let the RunProgram children hold run() open.+ assert all(kwargs[k] is subprocess.DEVNULL for k in ("stdin", "stdout", "stderr"))def test_git_unpack_names_the_extractor_exit_code(tmp_path, monkeypatch):- """Under -y the GUI stub fails silently (no stdout, no stderr, no error- box), so the message must carry the exit code and the usual causes- instead of promising captured output (#123094 review)."""- import subprocess+ """Under -y the stub reports nothing, so the error carries the exit code."""import pm.packages+ from pm.package import InstallErrorfrom pm.packages import Git- monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True, raising=False)+ monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True)+ _fake_run(monkeypatch, returncode=7)+ with pytest.raises(InstallError, match="exited 7"):+ Git().unpack(_portable_git(tmp_path), tmp_path / "scratch" / "tree", "win32-x64")- class _Result:- returncode = 7- stdout = ""- stderr = ""- monkeypatch.setattr(subprocess, "run", lambda argv, **kw: _Result())- archive = tmp_path / "fetch" / "PortableGit-2.53.0.3-64-bit.7z.exe"- archive.parent.mkdir()- archive.write_bytes(b"pinned bytes")- with pytest.raises(RuntimeError) as excinfo:- Git().unpack(archive, tmp_path / "scratch" / "tree", "win32-x64")- message = str(excinfo.value)- assert "exit code 7" in message- assert "silent" in message+def test_git_unpack_timeout_is_a_package_error(tmp_path, monkeypatch):+ import subprocess+ import pm.packages+ from pm.package import InstallError+ from pm.packages import Git++ def hang(argv, **kwargs):+ raise subprocess.TimeoutExpired(argv, kwargs["timeout"])++ monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True)+ monkeypatch.setattr(subprocess, "run", hang)+ with pytest.raises(InstallError, match="did not finish"):+ Git().unpack(_portable_git(tmp_path), tmp_path / "scratch" / "tree", "win32-x64")def test_git_unpack_requires_a_windows_host(tmp_path, monkeypatch):- """Cross-host staging of win32 git worked with the old tar.bz2 pin; the- self-extracting pin must refuse off-Windows with that trade-off stated- instead of a raw exec error (#123094 review)."""- import subprocess+ """Off Windows the PE extractor cannot run: refuse before executing+ anything, with a remedy that is not "retry"."""import pm.packages+ from pm.package import InstallErrorfrom pm.packages import Gitcalls = []-- class _Result:- returncode = 0- stdout = ""- stderr = ""-- monkeypatch.setattr(subprocess, "run", lambda argv, **kw: calls.append(argv) or _Result())- monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", False, raising=False)- archive = tmp_path / "x.7z.exe"- archive.write_bytes(b"pinned bytes")- with pytest.raises(RuntimeError, match="Windows host"):- Git().unpack(archive, tmp_path / "out", "win32-x64")+ _fake_run(monkeypatch, calls=calls)+ monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", False)+ with pytest.raises(InstallError, match="Windows host") as excinfo:+ Git().unpack(_portable_git(tmp_path), tmp_path / "out", "win32-x64")+ assert "retry" not in str(excinfo.value)assert not calls, "the guard must refuse before any execution"