From 96c397f618f57a4b715dcf97f11878d631c7ae0d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 05:43:20 -0500 Subject: [PATCH] fix(pkgs/id): port the #598 exit-code fix to the pkgs/id update-nixpkgs-inputs copies + anti-drift guard (#2783) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pkgs/id copies never received #598: failed updates exited 0 and the wrapper re-ran the whole update after a genuine failure. Porting the root scripts/ copies verbatim makes both pairs byte-identical. Anti-drift guard: scripts/tests/test_update_nixpkgs_inputs.py asserts byte-identity of both pairs and pins the #598 contract for BOTH copies (failing update exits 1, up-to-date exits 0, wrapper runs the body exactly once on genuine failure and falls back to python3 only on 126/127). These tests also supply the stub-nix harness #603 asks for, for both copies. Negative controls: pre-fix pkgs/id .py exits 0 under a failing stub nix; the pre-fix wrapper paired with a genuinely-failing .py executes the update twice. Verified on the laptop: python3 -m unittest discover -s scripts/tests -p test_update_nixpkgs_inputs.py — 6 tests OK. Root copies untouched; pkgs/id just recipes unchanged. --- pkgs/id/scripts/update-nixpkgs-inputs.py | 9 + pkgs/id/scripts/update-nixpkgs-inputs.sh | 13 +- scripts/tests/test_update_nixpkgs_inputs.py | 220 ++++++++++++++++++++ 3 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 scripts/tests/test_update_nixpkgs_inputs.py diff --git a/pkgs/id/scripts/update-nixpkgs-inputs.py b/pkgs/id/scripts/update-nixpkgs-inputs.py index 7e0620c705..5483459320 100755 --- a/pkgs/id/scripts/update-nixpkgs-inputs.py +++ b/pkgs/id/scripts/update-nixpkgs-inputs.py @@ -313,6 +313,15 @@ def main(): if len(refs) > 1: print_combined_summary(all_ref_data) + # A run that failed to update any input is a failure, even though the + # summaries above printed normally: callers (&& chains, CI, cron) must + # not proceed as if the inputs were updated. + failed_total = sum( + 1 for rd in all_ref_data for r in rd["results"] if r["status"] == "failed" + ) + if failed_total: + sys.exit(1) + if __name__ == "__main__": main() diff --git a/pkgs/id/scripts/update-nixpkgs-inputs.sh b/pkgs/id/scripts/update-nixpkgs-inputs.sh index a2e2eae9cd..eaba340f01 100755 --- a/pkgs/id/scripts/update-nixpkgs-inputs.sh +++ b/pkgs/id/scripts/update-nixpkgs-inputs.sh @@ -1,6 +1,15 @@ #!/usr/bin/env bash -# Wrapper: tries uv (via .py shebang) first, falls back to python3 +# Wrapper: tries uv (via .py shebang) first, falls back to python3 only +# when the script cannot be executed at all (exit 126/127, e.g. uv is +# missing) — never after a genuine update failure, which would re-run +# the whole update and mask the first run's results. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT="$SCRIPT_DIR/update-nixpkgs-inputs.py" -"$SCRIPT" "$@" || python3 "$SCRIPT" "$@" +"$SCRIPT" "$@" +status=$? +if [ "$status" -eq 126 ] || [ "$status" -eq 127 ]; then + python3 "$SCRIPT" "$@" + status=$? +fi +exit "$status" diff --git a/scripts/tests/test_update_nixpkgs_inputs.py b/scripts/tests/test_update_nixpkgs_inputs.py new file mode 100644 index 0000000000..c941285873 --- /dev/null +++ b/scripts/tests/test_update_nixpkgs_inputs.py @@ -0,0 +1,220 @@ +#!/usr/bin/env python3 +"""Contract tests for update-nixpkgs-inputs (both copies) — #2783, #603. + +The repo carries two copies of this tool: the fleet copy at scripts/ +and the pkgs/id copy at pkgs/id/scripts/, which the pkgs/id just +recipes run. #598 fixed a silent-failure bug (failed updates exited 0; +the wrapper re-ran the whole update after a genuine failure) in the +root copy only, and the fix silently never reached pkgs/id — the +drift this file exists to prevent: + +- The two .py copies must stay byte-identical, and so must the two + .sh wrappers. A fix that lands in one copy and not the other fails + here instead of shipping silently. +- The #598 exit-code contract is pinned for BOTH .py copies: exit 1 + after the summaries when any input update failed, 0 when all + succeed / are already up-to-date. +- The wrapper contract is pinned for BOTH .sh copies: fall back to + python3 only when the script cannot be executed at all (126/127), + propagate the exit status, and never re-run after a genuine failure + (the update body executes exactly once). + +Run: python3 -m unittest discover -s scripts/tests -p 'test_update_nixpkgs_inputs.py' +""" + +import json +import os +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +COPIES = { + "root": ROOT / "scripts", + "pkgs/id": ROOT / "pkgs" / "id" / "scripts", +} + +FLAKE_LOCK = { + "nodes": { + "root": {"inputs": {"nixpkgs": "nixpkgs"}}, + "nixpkgs": { + "original": {"type": "github", "owner": "NixOS", "repo": "nixpkgs"}, + "locked": { + "type": "github", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0123456789abcdef0123456789abcdef01234567", + }, + }, + }, + "root": "root", + "version": 7, +} + + +def _write_executable(path: Path, content: str) -> None: + path.write_text(content) + path.chmod(0o755) + + +class TestCopiesCannotDrift(unittest.TestCase): + def test_py_copies_byte_identical(self): + root = (COPIES["root"] / "update-nixpkgs-inputs.py").read_bytes() + pkgs_id = (COPIES["pkgs/id"] / "update-nixpkgs-inputs.py").read_bytes() + self.assertEqual( + root, + pkgs_id, + "scripts/ and pkgs/id/scripts/ update-nixpkgs-inputs.py diverged — " + "port the change to both copies (see #2783)", + ) + + def test_sh_copies_byte_identical(self): + root = (COPIES["root"] / "update-nixpkgs-inputs.sh").read_bytes() + pkgs_id = (COPIES["pkgs/id"] / "update-nixpkgs-inputs.sh").read_bytes() + self.assertEqual( + root, + pkgs_id, + "scripts/ and pkgs/id/scripts/ update-nixpkgs-inputs.sh diverged — " + "port the change to both copies (see #2783)", + ) + + +class ScriptTree: + """One copy of the tool staged as /scripts/ + /flake.lock, + + with a stub `nix` (and a stub `uv`, so the .py shebang executes the + body under python3) on PATH. The stub nix appends one line to a + counter file per invocation and exits with the configured status. + """ + + def __init__(self, src_dir: Path, nix_status: int): + self._tmp = tempfile.TemporaryDirectory() + base = Path(self._tmp.name) + self.repo_dir = base / "repo" + scripts = self.repo_dir / "scripts" + scripts.mkdir(parents=True) + shutil.copy2(src_dir / "update-nixpkgs-inputs.py", scripts) + shutil.copy2(src_dir / "update-nixpkgs-inputs.sh", scripts) + (self.repo_dir / "flake.lock").write_text(json.dumps(FLAKE_LOCK)) + self.bin = base / "bin" + self.bin.mkdir() + self.counter = base / "nix-calls" + _write_executable( + self.bin / "nix", + "#!/bin/sh\n" + f'echo call >> "{self.counter}"\n' + f"exit {nix_status}\n", + ) + # The .py shebang is `env -S uv run --script`; this stub makes + # the wrapper's direct-execution attempt run the body under + # python3, so wrapper behaviour is testable without uv. + _write_executable( + self.bin / "uv", + "#!/bin/sh\n" + '[ "$1" = run ] && shift\n' + '[ "$1" = --script ] && shift\n' + 'exec python3 "$@"\n', + ) + self.env = dict(os.environ, PATH=f"{self.bin}:{os.environ['PATH']}") + + def cleanup(self): + self._tmp.cleanup() + + def nix_calls(self) -> int: + if not self.counter.exists(): + return 0 + return len(self.counter.read_text().splitlines()) + + def run_py(self) -> subprocess.CompletedProcess: + return subprocess.run( + [sys.executable, str(self.repo_dir / "scripts" / "update-nixpkgs-inputs.py")], + env=self.env, + capture_output=True, + text=True, + timeout=60, + ) + + def run_sh(self) -> subprocess.CompletedProcess: + return subprocess.run( + ["bash", str(self.repo_dir / "scripts" / "update-nixpkgs-inputs.sh")], + env=self.env, + capture_output=True, + text=True, + timeout=60, + ) + + +class TestExitCodeContract(unittest.TestCase): + def _check_copy(self, label: str, src_dir: Path): + with self.subTest(copy=label, case="failed update exits 1"): + tree = ScriptTree(src_dir, nix_status=1) + try: + r = tree.run_py() + self.assertIn("Failed: 1", r.stdout) + self.assertEqual(r.returncode, 1, r.stdout + r.stderr) + self.assertEqual(tree.nix_calls(), 1) + finally: + tree.cleanup() + with self.subTest(copy=label, case="up-to-date exits 0"): + tree = ScriptTree(src_dir, nix_status=0) + try: + r = tree.run_py() + self.assertIn("Failed: 0", r.stdout) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + finally: + tree.cleanup() + + def test_root_copy(self): + self._check_copy("root", COPIES["root"]) + + def test_pkgs_id_copy(self): + self._check_copy("pkgs/id", COPIES["pkgs/id"]) + + +class TestWrapperContract(unittest.TestCase): + def _check_copy(self, label: str, src_dir: Path): + with self.subTest(copy=label, case="genuine failure runs once, exits 1"): + tree = ScriptTree(src_dir, nix_status=1) + try: + r = tree.run_sh() + self.assertEqual(r.returncode, 1, r.stdout + r.stderr) + self.assertEqual( + tree.nix_calls(), + 1, + "wrapper re-ran the update after a genuine failure", + ) + finally: + tree.cleanup() + with self.subTest(copy=label, case="success exits 0"): + tree = ScriptTree(src_dir, nix_status=0) + try: + r = tree.run_sh() + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertEqual(tree.nix_calls(), 1) + finally: + tree.cleanup() + with self.subTest(copy=label, case="cannot-execute falls back to python3"): + tree = ScriptTree(src_dir, nix_status=1) + try: + # No exec bit: direct execution fails 126, the wrapper + # must fall back to python3 exactly once and still + # propagate the genuine failure status. + (tree.repo_dir / "scripts" / "update-nixpkgs-inputs.py").chmod(0o644) + r = tree.run_sh() + self.assertEqual(r.returncode, 1, r.stdout + r.stderr) + self.assertEqual(tree.nix_calls(), 1) + finally: + tree.cleanup() + + def test_root_copy(self): + self._check_copy("root", COPIES["root"]) + + def test_pkgs_id_copy(self): + self._check_copy("pkgs/id", COPIES["pkgs/id"]) + + +if __name__ == "__main__": + unittest.main()