Skip to content

The agent that grows with you

fix(code-health): a failing measurement tool exits 2, not with a traceback

Tekniumf970c226f365
diff --git a/scripts/code_health/cli.py b/scripts/code_health/cli.py
index c783a7960c1..8d1fbae07b7 100644
--- a/scripts/code_health/cli.py
+++ b/scripts/code_health/cli.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import argparse
import json
import re
+import subprocess
import sys
import time
from dataclasses import asdict
@@ -118,3 +119,8 @@ def main(argv: list[str] | None = None) -> int:
except RuntimeError as exc:
print(f"code health: {exc}", file=sys.stderr)
return 2
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
+ # A measuring tool (npm install of the pinned TypeScript, ...) failed: that is a broken
+ # run, not a finding, so it must not exit 1 behind a traceback.
+ print(f"code health: measurement tooling failed: {exc}", file=sys.stderr)
+ return 2
diff --git a/tests/scripts/test_code_health_cli.py b/tests/scripts/test_code_health_cli.py
index 2a52405a24c..c24ee13cc59 100644
--- a/tests/scripts/test_code_health_cli.py
+++ b/tests/scripts/test_code_health_cli.py
@@ -151,3 +151,20 @@ def test_json_stdout_is_json_on_every_path(tmp_path, capsys, case, switch, files
assert isinstance(data, list) and len(data) == findings, (case, out)
if case in ("no measured change", "enforcement off"):
assert "code health:" in err # the human explanation moves to stderr
+
+
+# --- measurement tooling failures are errors, not findings ------------------------------------
+
+
+def test_tooling_failure_exits_2_without_a_traceback(tmp_path, monkeypatch, capsys):
+ repo, base = _ratchet_repo(tmp_path)
+ lock = {"packages": {"node_modules/typescript": {"version": "0.0.0-not-published"}}}
+ _commit(repo, {"package-lock.json": json.dumps(lock), "web/a.ts": "export const a = 1;\n"})
+ if not shutil.which("npm") or not shutil.which("node"):
+ pytest.skip("needs node + npm to reach the npm install path")
+ monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
+ monkeypatch.setenv("npm_config_offline", "true") # a real npm failure, without the network
+ monkeypatch.chdir(repo)
+ assert cli.main(["--base", base, "--head", "HEAD"]) == 2
+ err = capsys.readouterr().err
+ assert err.startswith("code health:") and "npm" in err