Skip to content

The agent that grows with you

fix(install): refuse occupied Windows checkout before Git provisioning

ethernetdcfe8f2d7561
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 5460926ff2c..45254d33cdc 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -552,6 +552,15 @@ function Stage-Prerequisites {
}
function Stage-Repository {
+ # Refuse an occupied non-checkout before provisioning Git. This check
+ # needs no tool download and must not overwrite a user's existing files.
+ if (-not (Test-Path (Join-Path $InstallDir ".git")) -and (Test-Path -LiteralPath $InstallDir)) {
+ $item = Get-Item -LiteralPath $InstallDir -Force
+ $empty = $item.PSIsContainer -and -not $item.LinkType -and -not (Get-ChildItem -LiteralPath $InstallDir -Force | Select-Object -First 1)
+ if (-not $empty) {
+ Fail "$InstallDir exists and is not a Hermes git checkout. Move it aside, or install elsewhere with -InstallDir <path>."
+ }
+ }
if (-not (Ensure-Git)) { Fail "no pinned Git artifact for this Windows architecture" }
# An interrupted clone from an older installer can leave a .git with no
# initial commit, where stash/checkout abort ("You do not have the initial
@@ -620,15 +629,9 @@ function Stage-Repository {
Log "not fast-forwardable; reset to origin/$Branch"
}
} else {
- # Moving a clone onto an existing directory would nest it, so a
- # pre-existing destination must be empty (taken over) or we refuse:
- # whatever lives there is not ours. Mirrors scripts/install.sh.
+ # Moving a clone onto an existing directory would nest it. The
+ # preflight above already refused nonempty or linked destinations.
if (Test-Path -LiteralPath $InstallDir) {
- $item = Get-Item -LiteralPath $InstallDir -Force
- $empty = $item.PSIsContainer -and -not $item.LinkType -and -not (Get-ChildItem -LiteralPath $InstallDir -Force | Select-Object -First 1)
- if (-not $empty) {
- Fail "$InstallDir exists and is not a Hermes git checkout. Move it aside, or install elsewhere with -InstallDir <path>."
- }
Remove-Item -LiteralPath $InstallDir -Force
}
Log "cloning $RepoUrl ($Branch) into $InstallDir"
diff --git a/tests/scripts/install/test_install_ps1_json_failure_frame.py b/tests/scripts/install/test_install_ps1_json_failure_frame.py
index c890bed3a23..220f18b8137 100644
--- a/tests/scripts/install/test_install_ps1_json_failure_frame.py
+++ b/tests/scripts/install/test_install_ps1_json_failure_frame.py
@@ -14,15 +14,21 @@ INSTALLER = Path(__file__).resolve().parents[3] / "scripts" / "install.ps1"
def test_fail_inside_a_stage_emits_one_json_frame_with_the_reason(tmp_path):
powershell = shutil.which("powershell")
assert powershell
- # A clone from a path that does not exist makes the repository stage hit
- # the installer's own Fail helper (not a thrown exception).
- env = dict(os.environ, HERMES_REPO_URL=str(tmp_path / "no-such-repo"),
- HERMES_INSTALL_DIR=str(tmp_path / "install"), HERMES_HOME=str(tmp_path / "home"))
+ # A nonempty non-checkout refuses before any clone retries, through Fail.
+ install_dir = tmp_path / "install"
+ install_dir.mkdir()
+ (install_dir / "user-file").write_text("preserve me", encoding="utf-8")
+ tools = tmp_path / "empty-tools"
+ env = dict(os.environ, HERMES_HOME=str(tmp_path / "home"),
+ HERMES_RUNTIME_DIR=str(tools))
result = subprocess.run(
- [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(INSTALLER), "-Stage", "repository", "-Json"],
- env=env, capture_output=True, text=True, timeout=120)
+ [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(INSTALLER),
+ "-Stage", "repository", "-Json", "-InstallDir", str(install_dir)],
+ env=env, capture_output=True, text=True, timeout=30)
frames = [json.loads(line) for line in result.stdout.splitlines() if line.startswith("{")]
assert result.returncode == 1
assert len(frames) == 1, result.stdout
assert frames[0]["ok"] is False and frames[0]["stage"] == "repository"
- assert "git clone failed" in frames[0]["reason"]
+ assert "exists and is not a Hermes git checkout" in frames[0]["reason"]
+ assert (install_dir / "user-file").read_text(encoding="utf-8") == "preserve me"
+ assert not tools.exists()