Skip to content

Fix #906: ignore [[ ]]/[ ] test operands in compile_hooks.py's dynamic-dispatch detector - #1001

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/906
Oct 11, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/906

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #906

compile_hooks.py's _DYNAMIC_CALL_RE dynamic-dispatch detector matched && "$name" even when "$name" was a test operand inside [[ ... ]]/[ ... ], not a real call through a variable. This false-positived and silently disabled tree-shaking for the whole hook file -- the only symptom was the compiled file's byte size, which is exactly what happened during #899 (session-start-hook.sh grew from ~122 KB to 135,889 B).

What changed

  • _mask_bracket_tests blanks out every [[ ... ]]/[ ... ] test-expression span in an already-masked line before the dynamic-dispatch regex ever sees it. Brackets are counted, not matched on the first ], so a glob char-class inside the test (*[!0-9]*) does not prematurely close the span.
  • The span is carried across physical lines (carry_depth/carry_double), the same way _scan_line_braces already threads brace/quote/heredoc state -- bash continues a [[ ... ]]/[ ... ] test onto the next line not only after an explicit trailing backslash but also whenever a line simply ends on &&/||/| with no backslash at all. This repo's own scripts/run-consolidation.sh:301 line-wraps exactly that way, and a review pass on the first cut reproduced the original false positive through exactly this gap before it was fixed.
  • tree_shake's report now names the triggering physical line when dynamic dispatch disables shaking, and compile_hooks.py's CLI prints that reason to stderr for every build whenever shaking is disabled -- unconditionally, not only under --apply. (Note: this pull request's first commit message describes the print as --apply-specific; that line was wrong, the code has always printed unconditionally, which is the safer direction and was left as-is on review rather than amended.)

Adjacent (not filed)

_mask_bracket_tests's boundary check also matches a single-bracket glob like ls [abc]*, masking it as a test; checked whether this could hide a real dynamic-dispatch match and could not construct one, so this is judged cosmetic/unreachable only and is not filed as an issue.

Tests

6 new tests in tests/test_compile_hooks_900.py: a double-bracket false positive, a single-bracket false positive, confirmation that a real dynamic call after a test still bails, confirmation that the bail reason names the triggering line, and a multi-line bracket span split by bash's implicit line continuation. All watched red before their corresponding implementation, then green (70 passed in the full targeted file). Also re-ran the repo-wide scanner tests/test_locale_ranges_695.py (5 passed) and a Python-3.9 AST-compatibility parse check against the changed module, since it has scanner/cross-version exposure beyond its own test file.

Review

Two reviewers (Explore and oss:auditor) found the same correctness gap independently -- the multi-line bracket span above -- which is fixed in this pull request's second commit. A second finding (one new test would still pass against a no-op stub of the fix) was argued down: that test's job is the required positive control pairing the false-positive tests, per this repo's own CLAUDE.md rule pairing every must-not-fire case with a must-fire case, and it does guard against the fix accidentally blinding a real trailing call even though this particular fixture's call happens to sit outside the masked span regardless.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits October 8, 2026 14:54
…c-dispatch detector

`_DYNAMIC_CALL_RE` matched `&& "$name"` even when `"$name"` was a test
operand inside `[[ ... ]]`/`[ ... ]`, not a real call through a variable --
`[[ ]]`/`[ ]` cannot invoke an arbitrary command, so there was no call to
miss. This false positive forced tree-shaking off for the whole hook file
with no visible symptom besides the compiled size, exactly as #906
describes from #899's work on scripts/lib-lock.sh.

Added `_mask_bracket_tests`, applied to each masked line before the
dynamic-dispatch scan, to blank test-expression spans (bracket-counted so
a glob char-class like `*[!0-9]*` does not prematurely close the span).
Also made `tree_shake`'s report name the triggering physical line, and
`compile_hooks.py --apply` now prints the reason to stderr whenever
tree-shaking is disabled instead of staying silent.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
… lines

Self-review (Explore + oss:auditor) on the first cut found the same gap
independently: `_mask_bracket_tests` only masked a bracket-test span that
opened and closed on one physical line. Bash continues a `[[ ... ]]`/
`[ ... ]` test onto the next line not only after an explicit trailing
backslash but whenever a line simply ends on `&&`/`||`/`|` with no
backslash at all (this repo's own scripts/run-consolidation.sh:301 wraps
exactly that way) -- so a test split that way still false-positived as
dynamic dispatch, the exact bug class #906 was filed against.

_mask_bracket_tests now threads a (depth, double) carry across physical
lines, the same way _scan_line_braces already threads brace/quote/heredoc
state, and tree_shake's masking loop passes it through. Test added:
test_tree_shake_a_test_operand_split_across_lines_by_implicit_continuation_is_not_dynamic_dispatch.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit c54e96b into main Oct 11, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/906 branch October 11, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compile_hooks.py: && "$name" inside [[ ]] is read as a call through a variable and silently disables tree-shaking for the whole hook

1 participant