diff --git a/CHANGELOG.md b/CHANGELOG.md index dcaeaaf9..5b2c45c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to ForgeLM are documented here. ### Fixed +- **The CI failure-masking guard explains the bash 5.2 / 5.3 difference for a bare `exit` in an ERR trap.** + `tools/check_ci_failure_masking.py` used to report that such a trap exits with the status of the command + before it. That holds on bash 5.2 only: bash 5.3, the shell on `ubuntu-26.04`, which `ubuntu-latest` now + resolves to, keeps the status the trap was entered with. The report now names both behaviours and still flags + the shape on every version, because the runner image decides which bash a workflow gets. The same difference + had turned the nightly compatibility run and every pull-request `test` job red since the 2026-11-05 nightly + run; they pass again. ForgeLM's runtime behaviour is unchanged (#1075). - CI/CD examples capture ForgeLM and `jq` statuses separately: a JSON parser's exit 4 cannot be mistaken for an approval hold, and a multi-stage approval hold retains ForgeLM's exit 4. The guide's JSON-reference section links use the compiled heading IDs and preserve the page route and heading on reload diff --git a/tests/test_check_ci_failure_masking.py b/tests/test_check_ci_failure_masking.py index 89c83abb..4c8a0ea5 100644 --- a/tests/test_check_ci_failure_masking.py +++ b/tests/test_check_ci_failure_masking.py @@ -18,7 +18,7 @@ import sys import time from pathlib import Path -from typing import Optional +from typing import Optional, Tuple import pytest @@ -282,7 +282,8 @@ class TestReviewedShapes: # `|| true` is refused inside substitutions and trap actions too. ("v=$(git describe || true)\npytest", "discards its failure"), ("trap 'rm -f x || true' EXIT\npytest", "discards its failure"), - # On ERR a bare `exit` uses the status of the command before it, and `set +e` lets the step go on. + # On ERR a bare `exit` uses the status of the command before it (bash 5.2; 5.3 keeps the step's), + # and `set +e` lets the step go on. ("trap 'echo x; exit' ERR\npytest\necho done", "bare `exit` after another command"), ("f() { echo x; exit; }\ntrap f ERR\npytest\necho done", "bare `exit` after another command"), ("trap 'set +e' ERR\npytest\necho done", "turns errexit off"), @@ -1142,22 +1143,40 @@ def test_a_root_given_another_way_scans_the_same(self, guard, tmp_path, capsys, assert guard.main(["--root", given]) == 0, capsys.readouterr().out -def _bash5() -> Optional[str]: - """A bash of version 5 or newer on Linux or macOS, as on GitHub's Linux runners; None otherwise. +def _bash5() -> Optional[Tuple[str, Tuple[int, int]]]: + """A bash of version 5 or newer on Linux or macOS, as on GitHub's Linux runners: (path, (major, minor)). - Windows' bash (Git Bash) runs in another environment, so the comparison stays on POSIX systems. + None otherwise. Windows' bash (Git Bash) runs in another environment, so the comparison stays on POSIX systems. """ path = shutil.which("bash") if sys.platform != "win32" else None if path is None: return None try: - version = subprocess.run([path, "-c", "echo ${BASH_VERSINFO[0]}"], capture_output=True, text=True, timeout=30) + probe = subprocess.run( + [path, "-c", 'echo "${BASH_VERSINFO[0]} ${BASH_VERSINFO[1]}"'], capture_output=True, text=True, timeout=30 + ) except (OSError, subprocess.SubprocessError): return None - return path if version.stdout.strip().isdigit() and int(version.stdout) >= 5 else None - - -_BASH = _bash5() + parts = probe.stdout.split() + if len(parts) != 2 or not all(part.isdigit() for part in parts): + return None + version = (int(parts[0]), int(parts[1])) + return (path, version) if version[0] >= 5 else None + + +_BASH_FOUND = _bash5() +_BASH = _BASH_FOUND[0] if _BASH_FOUND else None +_BASH_VERSION = _BASH_FOUND[1] if _BASH_FOUND else (0, 0) + +# Shapes whose status under `bash -e` depends on the bash version: the first (major, minor) from which the step +# fails instead of going on. The guard's verdict does not move with the version, because a workflow that says +# `ubuntu-latest` runs whichever bash the image ships (5.2 on ubuntu-24.04, 5.3 on ubuntu-26.04), so it reports +# what an older bash would let through. +_STEP_FAILS_FROM_BASH = { + # bash 5.2 takes a bare `exit` in an ERR trap from the trap's last command (`echo`, status 0); 5.3 keeps the + # status the trap was entered with. + "trap 'echo x; exit' ERR\npytest\necho done": (5, 3), +} # Shapes where the guard's verdict must equal bash's. Each required program is a # stub that exits 1, ci/t.sh masks `pytest` and ci/env.sh turns errexit off. A @@ -1194,7 +1213,8 @@ def _bash5() -> Optional[str]: ("pytest &\nwait", "background"), ("set +e\npytest\necho done", "errexit is off"), ("if pytest; then echo ok; fi", "condition"), - # ERR traps: a bare `exit` there uses the last command's status, and `set +e` lets the step go on. + # ERR traps: a bare `exit` there uses the last command's status on bash 5.2 (5.3 keeps the step's; see + # `_STEP_FAILS_FROM_BASH`), and `set +e` lets the step go on. ("trap 'echo x; exit' ERR\npytest\necho done", "bare `exit` after another command"), ("trap 'set +e' ERR\npytest\necho done", "turns errexit off"), ("trap 'exit' ERR\npytest\necho done", None), @@ -1272,14 +1292,28 @@ def test_verdicts_match_bash(guard, tmp_path, script, expected): [_BASH, "--noprofile", "--norc", "-e", str(step)], cwd=tmp_path, env=env, capture_output=True, timeout=30 ) findings = guard.scan_script(script, "run", guard.GITHUB_DEFAULT, tmp_path)[0] + fixed_from = _STEP_FAILS_FROM_BASH.get(script) + masked = expected is not None and not (fixed_from is not None and _BASH_VERSION >= fixed_from) + assert result.returncode == (0 if masked else 1), (script, _BASH_VERSION, result.returncode, result.stderr) if expected is None: - assert result.returncode == 1, (script, result.returncode, result.stderr) assert findings == [], findings else: - assert result.returncode == 0, (script, result.returncode, result.stderr) assert any(expected in finding for finding in findings), findings +def test_version_dependent_shapes_are_shapes_the_guard_reports(): + """A shape listed as bash-version dependent must still be one the guard flags, or the table rots.""" + expected_by_script = dict(_AGAINST_BASH) + for script in _STEP_FAILS_FROM_BASH: + assert expected_by_script.get(script) is not None, script + + +def test_err_trap_message_names_the_bash_versions(guard, tmp_path): + """The report cannot say a bare `exit` in an ERR trap always masks the step: bash 5.3 keeps the step's status.""" + findings = guard.scan_script("trap 'echo x; exit' ERR\npytest\necho done", "run", guard.GITHUB_DEFAULT, tmp_path)[0] + assert any("bash 5.2" in finding and "5.3 keeps the step's" in finding for finding in findings), findings + + @pytest.mark.skipif( not (sys.platform.startswith("linux") and os.environ.get("CI")), reason="only CI's Linux runners must have bash 5" ) diff --git a/tools/check_ci_failure_masking.py b/tools/check_ci_failure_masking.py index 2d5ea644..d97a0d8f 100644 --- a/tools/check_ci_failure_masking.py +++ b/tools/check_ci_failure_masking.py @@ -666,7 +666,8 @@ def _reraising_exits( *entry_status*); after ``||`` it holds the failed command's non-zero status; and ``rc=$?`` as the first command keeps it for a later ``exit "$rc"``, unless something assigns ``rc`` again. *bare_is_status* reads a bare ``exit`` as - ``exit $?``, as bash does in an ERR trap. + ``exit $?``, as bash 5.2 does in an ERR trap; bash 5.3 keeps the status the + trap was entered with, so this is the stricter of the two readings. """ found: set = set() if not action.items: @@ -1631,9 +1632,11 @@ def trap_problem( *entry_status* says whether ``$?`` still holds the status the trap was entered with when *handler* starts: true for an EXIT/ERR action itself and for a function it calls first. Except on EXIT, where it keeps the entry - status, bash reads a bare ``exit`` as ``exit $?``, and turning errexit off - keeps a failing command from stopping the step. DEBUG and RETURN run between - the script's commands, so ``set -n`` there stops it. + status, bash reads a bare ``exit`` as ``exit $?`` (on ERR that is bash 5.2; + 5.3 keeps the entry status there too, and the guard keeps the stricter + reading), and turning errexit off keeps a failing command from stopping the + step. DEBUG and RETURN run between the script's commands, so ``set -n`` + there stops it. """ label = _trap_label(kind) if self.has_required(handler): @@ -1662,11 +1665,15 @@ def trap_problem( if id(exit_command) in reraises or (code is None and kind == "EXIT") or (number or 0) % 256: continue if code is None: - where = "on ERR it exits" if kind == "ERR" else "it exits" - return ( - f"sets {specific} that runs a bare `exit` after another command; {where} with that command's " - "status instead of the step's" + # bash 5.2 takes a bare `exit` in an ERR trap from the last command the trap ran; 5.3 keeps the + # status the trap was entered with. The guard still reports it: the runner image, and with it the + # bash version, moves under a workflow that says `ubuntu-latest`. + where = ( + "on ERR, bash 5.2 exits with that command's status instead of the step's (5.3 keeps the step's)" + if kind == "ERR" + else "it exits with that command's status instead of the step's" ) + return f"sets {specific} that runs a bare `exit` after another command; {where}" if code in ("$?", "${?}"): return ( f"sets {label} that runs `exit {code}` after another command, so it exits with that "