Skip to content

The agent that grows with you

fix(pm): execute the pinned git SFX from a scratch copy, not the cache

finn7637a40c28750c4
diff --git a/pm/packages.py b/pm/packages.py
index d2fc2a79c6a..78ec764e422 100644
--- a/pm/packages.py
+++ b/pm/packages.py
@@ -614,13 +614,22 @@ class Npm(BinaryPackage):
return [latest] if latest else []
+# The pinned git artifact is a self-extracting 7z: only hosts where PE images
+# execute can stage it (tests patch this flag).
+_HOST_IS_WINDOWS = os.name == "nt"
+
+
@register
class 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), and no GUI."""
+ (#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."""
name = "git"
optional = True
@@ -653,23 +662,46 @@ class Git(BinaryPackage):
return out
def unpack(self, archive: Path, staged: Path, target: str) -> None:
+ import shutil
import subprocess
- # The self-extracting 7z carries its own extractor: running it needs
- # no tar bzip2 filter and no bzip2.exe (#122512). Download bytes are
- # sha256-pinned before this runs, same as every other artifact.
- staged.mkdir(parents=True, exist_ok=True)
- proc = subprocess.run(
- [str(archive), f"-o{staged}", "-y"],
- capture_output=True,
- text=True,
- timeout=600,
- )
- if proc.returncode:
+ if not _HOST_IS_WINDOWS:
raise RuntimeError(
- f"PortableGit self-extractor failed ({proc.returncode}): "
- f"{(proc.stderr or proc.stdout).strip()[-500:]}"
+ "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)"
)
+ 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.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 "")
+ )
+ finally:
+ shutil.rmtree(work, ignore_errors=True)
def env(self, entry: Path, target: str) -> dict:
return {"PATH": [str(entry / "cmd"), str(entry / "usr" / "bin")]}
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index d670f5ca324..c5343a3599c 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -558,10 +558,20 @@ function Get-PinnedGit {
# filter ("unable to run program bzip2 -d", issue #122512).
Unblock-File -Path $sfxPath -ErrorAction SilentlyContinue
# The SFX stub is a GUI-subsystem exe: PowerShell's `&` would not wait
- # for it, so drive it explicitly and wait for its exit code.
+ # for it, so drive it explicitly. Under -y the extractor is silent on
+ # every channel (no stdout, stderr, or error box), so report the exit
+ # code and the usual causes instead of promising captured output.
+ # Bound the wait like pm's timeout=600: a stuck post-install must fail
+ # loudly instead of hanging the bootstrap.
$sfx = [System.Diagnostics.Process]::Start($sfxPath, "-o`"$extractDir`" -y")
- $sfx.WaitForExit()
- if ($sfx.ExitCode) { Fail "failed to extract pinned git archive" }
+ if (-not $sfx.WaitForExit(600000)) {
+ $sfx.Kill()
+ $sfx.WaitForExit()
+ Fail "pinned git self-extractor timed out after 600s"
+ }
+ if ($sfx.ExitCode) {
+ Fail "pinned git self-extractor failed with exit code $($sfx.ExitCode); under -y the GUI stub is silent - check disk full, path-length limits, or antivirus locks"
+ }
# PortableGit roots cmd\git.exe directly; still flatten a single
# wrapper dir if a layout ever arrives wrapped.
$inner = @(Get-ChildItem $extractDir)
diff --git a/tests/pm/test_store_extract.py b/tests/pm/test_store_extract.py
index ea0870f47f7..2f00fd2417e 100644
--- a/tests/pm/test_store_extract.py
+++ b/tests/pm/test_store_extract.py
@@ -42,28 +42,98 @@ 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_staging_needs_no_external_decompressor(tmp_path):
- """Git staging on either side must not route through tar/bzip2 (#122512).
-
- The old .tar.bz2 pin died on Windows 10 boxes whose System32 tar.exe
- cannot run the bzip2 filter ("unable to run program bzip2 -d").
- """
- from pathlib import Path
+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
- # pm pins the self-extracting PortableGit archive for every Windows target.
for target in ("win32-x64", "win32-arm64"):
assert Git().fetch_url("2.53.0+3", target).endswith(".7z.exe")
- # The pre-PM bootstrap stages the same pin and never shells to tar.exe.
- installer = Path(__file__).resolve().parents[2] / "scripts" / "install.ps1"
- text = installer.read_text(encoding="utf-8-sig")
- assert "$inboxTar" not in text
- assert "System32\\tar.exe" not in text
- # A corrupt payload fails loudly instead of being silently unpacked.
- junk = tmp_path / "git.7z.exe"
- junk.write_bytes(b"not an archive")
- with pytest.raises(OSError):
- Git().unpack(junk, tmp_path / "out", "win32-x64")
+
+
+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."""
+ import subprocess
+ from pathlib import Path
+ import pm.packages
+ from 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)
+
+ 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")
+ staged = tmp_path / "scratch" / "tree"
+ Git().unpack(archive, staged, "win32-x64")
+ argv = calls[0]
+ assert 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
+
+
+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
+ import pm.packages
+ from pm.packages import Git
+
+ monkeypatch.setattr(pm.packages, "_HOST_IS_WINDOWS", True, raising=False)
+
+ 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_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
+ import pm.packages
+ from pm.packages import Git
+
+ calls = []
+
+ 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")
+ assert not calls, "the guard must refuse before any execution"
def test_target_uses_shared_native_arch(monkeypatch):
diff --git a/website/docs/reference/package-management.md b/website/docs/reference/package-management.md
index 52f6a72adf7..c40a5d86190 100644
--- a/website/docs/reference/package-management.md
+++ b/website/docs/reference/package-management.md
@@ -543,7 +543,7 @@ not substitutes for an installed application's update mechanism.
| `pm update --target TARGET` | Resolve versions for the specified target. |
| `pm update --uv` / `--npm` | Also refresh the Python or npm dependency resolution. |
| `pm update --termux [--check]` | Repin the termux pool archives the rolling pool has retired (the runtime-lib pin table and the bionic lock rows). `--check` reports without writing and exits 1 when a pin is retired. |
-| `pm install --target TARGET NAME...` | Stage explicit cross-target packages without recording them as the host's installed runtime. |
+| `pm install --target TARGET NAME...` | Stage explicit cross-target packages without recording them as the host's installed runtime. The win32 `git` target pins a self-extracting PortableGit archive: staging it runs the vendor extractor and therefore requires a Windows host. |
| `pm bundle --out DIR [--ref REF]` | Stage a source snapshot, native tools, facts, and Python dependencies. It does not produce a signed desktop installer. |
The complete desktop builder also builds the JavaScript surfaces, generates