Skip to content

The agent that grows with you

fix(launchers): every publication collects retired launcher copies

Teknium687e00e089f0
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index bfd02ef9882..51c4143ba14 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -557,7 +557,12 @@ def stage_launcher(name: str, repo_root: Path, out_dir: Path) -> Path | None:
store_python = resolve_store_python(repo_root, publication=True)
if store_python is not None:
path = mint_launcher(name, repo_root, out_dir, store_python, None)
- if path is not None and path.suffix == ".cmd":
+ if path is None:
+ return None
+ # Any publication collects copies retired by an earlier one, whatever
+ # launcher form it writes now (distlib may be back since).
+ _collect_retired(Path(out_dir) / f"{name}.exe")
+ if path.suffix == ".cmd":
# cmd.exe prefers .exe. An older launcher must not shadow the
# newly published command when distlib is unavailable.
if not _retire_exe(Path(out_dir) / f"{name}.exe"):
@@ -566,6 +571,13 @@ def stage_launcher(name: str, repo_root: Path, out_dir: Path) -> Path | None:
return None
+def _collect_retired(exe: Path) -> None:
+ """Delete aside copies of ``exe`` whose process has since exited."""
+ for leftover in exe.parent.glob(f"{exe.name}.*.deleteme"):
+ with suppress(OSError):
+ leftover.unlink()
+
+
def _retire_exe(exe: Path) -> bool:
"""Remove ``exe``; a running one is renamed aside instead.
@@ -575,9 +587,6 @@ def _retire_exe(exe: Path) -> bool:
plain unlink left every repinned update owing launchers -- and the install
stamp -- forever. The aside copy goes on the next publication once its
process has exited."""
- for leftover in exe.parent.glob(f"{exe.name}.*.deleteme"):
- with suppress(OSError):
- leftover.unlink()
try:
exe.unlink(missing_ok=True)
return True
diff --git a/tests/hermes_cli/test_source_launcher_publication.py b/tests/hermes_cli/test_source_launcher_publication.py
index 153c0db4def..33092e8be56 100644
--- a/tests/hermes_cli/test_source_launcher_publication.py
+++ b/tests/hermes_cli/test_source_launcher_publication.py
@@ -2,10 +2,12 @@
import json
import os
from pathlib import Path
+import queue
import shlex
import shutil
import subprocess
import sys
+import threading
import pytest
@@ -390,30 +392,39 @@ def test_repin_without_distlib_retires_stale_native_launcher(tmp_path, monkeypat
local = repo / ".hermes/bin"
original = Path(_launchers.ensure_install_launchers(repo, local)[0])
assert original.suffix == ".exe"
- process = None
- if running:
- # The launcher a repin retires is the one running `hermes update`.
- process = subprocess.Popen([str(original)], cwd=tmp_path, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, text=True, encoding="utf-8")
- assert any(line.strip() == "ready" for line in iter(process.stdout.readline, ""))
new_python = home / "tools" / "repinned" / "python.exe"
new_python.parent.mkdir()
new_python.touch()
(home / "tools/facts.json").write_text(
json.dumps({"packages": {"python": {"entry": "repinned"}}}), encoding="utf-8")
+ real_maker = _launchers._load_script_maker
monkeypatch.setattr(_launchers, "_load_script_maker", lambda: None)
+ process = None
try:
+ if running:
+ # The launcher a repin retires is the one running `hermes update`.
+ process = subprocess.Popen([str(original)], cwd=tmp_path, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, text=True, encoding="utf-8")
+ first = queue.Queue()
+ threading.Thread(target=lambda: first.put(process.stdout.readline()), daemon=True).start()
+ assert first.get(timeout=60).strip() == "ready"
result = _launchers.stage_launcher("hermes", repo, local)
finally:
if process is not None:
- process.communicate("", timeout=30)
+ try:
+ process.communicate("", timeout=30)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.communicate()
assert result == local / "hermes.cmd"
assert not original.exists() # cmd.exe must not run the old exe first
assert result is not None and str(new_python) in result.read_text(encoding="utf-8-sig")
if process is not None:
assert process.returncode == 0 # the retired launcher finished its own run
- assert _launchers.stage_launcher("hermes", repo, local) == local / "hermes.cmd"
- assert not list(local.glob("hermes.exe*")) # the aside copy is collected once it exits
+ # distlib is back for the next publication: the aside copy still goes.
+ monkeypatch.setattr(_launchers, "_load_script_maker", real_maker)
+ assert _launchers.stage_launcher("hermes", repo, local) == local / "hermes.exe"
+ assert not list(local.glob("hermes.exe.*.deleteme"))
@pytest.mark.platforms("windows")