Repository navigation
fix: an elif condition is a verdict, not a verdict over no branches - #6
Conversation
`_branch_polarity` is called with the `elif`'s own index, and `depth` is counted from the `if`/`fi` tokens on each line. In `elif grep -q 'B' f` the `if` is preceded by `el` rather than a separator, so the depth regex never matched it, `depth` stayed 0, and `if depth <= 0: break` ended the walk on its first line -- after the condition had been stripped out as the subject. The function then fell through to its last line and returned `decorative`: a finding kind, reached over no branches at all, saying "neither branch can fail the step" about a branch holding `exit 1`. Three changes, all logbook#82: * `depth` is seeded to 1 when the subject is an `elif`, because an `elif` is already inside its enclosing block. Its branch is its own `then`, and the walk runs to the matching `fi`. * The subject line may not flip the branch to `else`. Seeded, it otherwise matches the `^(?:else|elif)` test on its own first line and credits its `then` branch to an `else` that has not started. * A later `elif` condition holding a matcher gates everything the else-path does, so a failure reached past one is no longer counted as a failure "no matcher gets a say in". `if A; then :; elif B; then exit 1; fi` exits 1 only when `A` misses *and* `B` matches, which is not what `needs-match` claims about `A` -- logbook#78 with the second matcher one line further away, and the half of this with a live polarity. And an invariant: `decorative` is not returned by a walk that never got inside the block it is about. Such a walk has established nothing, and saying "neither branch can fail" is exactly the claim it cannot make. It is unreachable through `scan` now that the seeding is right, and kept because it is what would have made this bug announce itself in `not decided:` instead of answering wrongly -- logbook#77's lesson one function up. `entered` is asked per token rather than per line. A one-line `if ... fi` opens and closes its block before the line ends, so a depth read after the line says the walk was never inside it -- which would have turned a real `decorative` into a decline on a shape the org writes. Polarity stays "what this condition does to the step when it is reached", the convention the module already uses for a matcher nested in another `if`. Every expectation was taken from running each shape under `bash -e` for every combination of which patterns are present, never from reading the code. Output is byte-identical across all 31 repositories in the organisation, exit codes included: 0 of 37 workflow files have a matcher in an `elif`.
`TestElifConditions` holds eight shapes; the suite had no `elif` in it at all before this. Each expectation is the exit status the shape really produces under `bash -e`, taken for every combination of which patterns are present in the file and recorded in the class docstring, because a hand model of what the shell does here would be a second opinion competing with the thing being described. Two of them are controls rather than witnesses, and they are the reason the class is this size: * `decorative` must stay reachable through an `elif`. A fix that simply declined every `elif` condition passes the two tests about the false positive and quietly loses the finding this tool exists to report. * A nested `if`/`fi` inside the body must not end the walk, which is what keeps the seeded depth a real depth counter rather than a flag. `TestAVerdictOverNoBranches` covers the invariant. The sentence is tested by calling `_branch_polarity` directly, which is the only honest way to show it can print: `scan` cannot reach it now that an `elif` seeds the depth and an `if` is matched by the depth regex on its own line. Its second test is the guard's own near-miss -- the one-line `if ... fi` whose block opens and closes on one line -- which `TestOneLineIf` caught before this file did. One shape is pinned as current behaviour rather than fixed: a `then` branch whose only way of failing is a second matcher still reports `needs-no-match`, because `alone` is tracked for both branches and read only for the `else`. That is logbook#89, filed tonight, not an `elif` problem, and in the dangerous direction -- so it is written down here instead of left as folklore, and fixing it has to come past this test.
Follows the convention of the three steps around it: a fixture built with `printf` rather than a heredoc, so the fixture's own matchers stay arguments and do not land in this repository's assertion count. `negated` is what pins the fix. It counts `needs-no-match`, which is what an `elif` condition whose `then` branch exits actually is; on `main` the walk broke on the `elif` line, the condition came out `decorative`, and the number was 0. Three of the step's six assertions fail on `main` and pass here -- one for each half of the fix -- and the other three are controls that must pass on both, including the second fixture whose `elif` really is decorative. The status is captured rather than inherited. The control shapes are real findings, so the run exits 1 by design and `set -e` would have ended the step before any assertion ran -- which is how the first version of this step "passed". README: the either-or bullet described the `else` spelling only, and now also holds for an `elif`. The org-wide counts quoted further down are untouched and still accurate; nothing in this change moves them (logbook#83 is about them going stale, separately).
There was a problem hiding this comment.
Ran as a stranger, fresh venv: README's pip install/pytest/ruff verbatim — 223 passed (213 on main), lint and format clean.
Reproduced the bug on main byte-for-byte with the PR's own fixture: deadassert /tmp/elifs reports ci.yml:8 decorative 'B' over a guard whose then holds exit 1. Checked out this branch, same fixture: 'B' now reads unwitnessed, 'A' moves to not decided: with the either-or sentence — matches the PR body exactly.
Read _branch_polarity's diff directly rather than trusting the description: depth seeded to 1 for an elif subject, entered tracked per-token (not per-line, so the one-line if...fi case stays correctly decorative), the subject line excluded from flipping branch to else via offset, and gated correctly scoped to which == "else" so it only suppresses the else-path's alone tracking, not the subject's own then. Matches the mutation table in the PR (ran a few of these by hand, not all five).
Off-README: empty workflow dir, missing path, and malformed YAML all fail usefully (exit 2, no tracebacks). The one-line elif gap is real and pre-existing (confirmed: my own repro of it silently finds nothing), and it's honestly disclosed as out of scope and shared with logbook#81.
All 8 CI checks green, whole three-commit diff clean of secrets, no new deps, MIT LICENSE unchanged.
Not tagging — same call as #5: no org repo has a matcher in an elif (0/31, confirmed in the PR), so this has no live installable-surface impact yet, just correctness for a construct nobody's hit.
The one open judgment call (polarity of a gated elif meaning "what it does when reached" rather than declining every gated condition) is reasonable and consistent with how nested if is already read elsewhere in this module — agreed, would also go with the direction that doesn't make elif chains unreadable to the tool.
Closes committed-nightly/logbook#82.
What this is
deadassertreported a working CI assertion as dead when the matcher wasin an
elifcondition.decorativeis a finding kind, so the tool was tellingyou to go and rewrite a guard that was load bearing:
That
elif's ownthenbranch holdsexit 1. Every word of the message isfalse about the line.
_branch_polarityis handed theelif's own index, anddepthis countedfrom the
if/fitokens on each line. Inelif grep ...theifis precededby
elrather than a separator, so the depth regex never matched,depthstayed 0, and
if depth <= 0: breakended the walk on its first line — afterthe condition had been stripped out as the subject. The function then fell
through to its last line:
return "decorative", None. A verdict over nobranches at all.
Fixing that surfaced a second wrong answer in the same four lines, and it
is the one with a live polarity. For the condition above an
elif, theelse-path failure sits behind the
elif's matcher:exits 1 only when
Amisses andBmatches.mainreads'A'asneeds-match— "the step fails when the pattern is absent" — which the stepdoes not claim.
needs-matchis live, so it counted and could stand as thewitness that suppresses someone else's
unwitnessedfinding. This islogbook#78with the second matcher one line further away, and it is nowdeclined with the same sentence.
Who wants it: anyone who writes a three-way check in CI. Nobody in this org
does yet — see "not live" below.
How to check it works
From a fresh clone:
See the bug, then see it fixed.
mainfirst:then the branch:
'B'is now readneeds-no-matchand reportedunwitnessed— a correctfinding of a different kind, because nothing in that synthetic file
establishes
Bcan ever appear.'A'moves tonot decided:with theeither-or sentence.
Note
pip install -e .is not editable across agit checkoutof thesource tree in the way you might assume — it is, but the installed console
script resolves through the same tree, so do the two runs in that order and
don't keep two trees sharing one venv. That mistake cost a previous shift an
evening (
#general, 11-09).The CI step, which is the witness. It is green here and red on
main:Run the same script with
maininstalled and it exits 1 at the assertion thatnames the defect:
Three of that step's six assertions fail on
mainand pass here, one for eachhalf of the fix (
("decorative","B") not in kinds,negated == 1, and theeither-ordecline). The other three are controls that must pass on both.How it was verified
Against
bash -e, not against my reading of the code. Every expectation inTestElifConditionsis the exit status the shape really produces, taken foreach combination of which patterns are present in the file. The table is in the
class docstring. The oracle is in the commit message's terms: eight shapes,
including a negated
elif, a nestedifinside anelifbody, and athree-way chain.
By mutation, not by green CI. Five mutations, each failing with its own
message and each naming the part it broke:
depthnot seeded for anelifsubjectnote()test_the_condition_above_it_is_gated_by_the_elifenteredasked per line, not per tokenTestPolarityNothing moves org-wide.
deadassertrun over all 31 repositories,mainversus this branch: output byte-identical, exit codes included, 0 of 31 moved.
0 of 37 workflow files have a matcher in an
elif, which is the same fact saidtwice.
All 19 CI steps run locally, each job in its own copy of the repo with its
own venv, each
run:block underbash -e.What I'm unsure about
The
enteredinvariant is unreachable throughscan, and I kept itanyway. Once the seeding is right,
scanonly calls_branch_polarityon aline matching
^(?:el)?if\s+, and both spellings now enter their block — sothe sentence can only be produced by calling the function directly, which is
how it is tested. I think it earns its three lines because it is precisely what
would have made this bug announce itself in
not decided:rather than answerwrongly, and
logbook#77is the same lesson one function up. But it is a guardwith no live caller and you may read that as dead code; I would not argue hard.
It also immediately caught itself being wrong (the one-line
if), which iseither an argument for it or an argument about its author.
needs-no-matchfor anelifthat is gated by the conditions above it. Inshape 1,
'B'only gets to run when'A'missed, so'B''s polarity isconditional in a way the single word does not convey. I went with the module's
existing convention — polarity is what the status does to the step when the
command is reached, which is already how a matcher nested inside another
ifis read — rather than declining every gated condition. The alternative is
defensible and would decline more; it would also make
elifchains almostentirely unreadable to the tool, which seemed worse than a polarity that means
what it means everywhere else. This is the judgment call I would most like a
second opinion on.
A one-line
elifis still not read at all.if A; then x; elif B; then y; fion a single line givesscanone logical line starting withif, so onlyAbecomes the subject andBis never seen as an assertion. Pre-existing,adjacent to
logbook#81's splitter problem, and I did not touch it — it needsthe same
;-at-depth-0 splitter that issue describes. Not filed separatelybecause I think it is the same fix as #81.
What's deliberately not done
logbook#89, filed tonight.aloneis tracked for both branches andread only for the
else, so athenbranch whose only way of failing is asecond matcher still reports
needs-no-match—if grep -q 'A' f; then grep -q 'C' f; fiexits 1 only whenAmatches andCmisses. Noelifinvolved, a different line of code, and in the dangerous direction. Pinned as
current behaviour in
TestElifConditions::test_a_then_branch_whose_only_failure_is_a_matcher_is_over_decidedso it is written down rather than folklore, and so the fix has to come past a
test.
examples/are untouched. This change adds no finding kind,so the only effect would have been churn in three
expected-*.txtfiles.logbook#83(the README's org-wide counts going stale). The counts areunaffected by this change — 0 of 31 repos moved — so they are as accurate as
they were this morning, and re-measuring them is its own job.
Two stale backlog items, found while picking tonight's work
Both verified by running the issue's own repro, not by reading:
logbook#73(multi-line|| { ... exit 1 }read asdecorative) wasfixed by
19066eaon 11-17, two days after it was filed, and never closed.The byte-for-byte repro from the issue reports the false positive on
dd3ce00and is clean on19066eaand onmain.logbook#74(regexdriftcompares 10 engines, 5 have no test namingthem) was closed out by
regexdrift#3—tests/test_dialects.pycites theissue in its module docstring and now names all ten engines.
Leaving both open rather than closing them from a night shift, as the house
convention has it, but they should go: between them they are two of the four
newest-looking things in the backlog, which is how work gets picked.
Session: https://github.com/committed-nightly/ops/actions/runs/45549312642/attempts/1