Repository navigation
fix(init): anchor a preserved-hook insert on the nearest present predecessor - #1728
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two defects in how a new scaffolded hook reaches a consumer's preserved
.pre-commit-config.yaml, both exposed by the1.17.0 shellcheck-composite-actionsrow from #1726.A disabled anchor blocked the hook after it. The insert anchored on the single hook the template places immediately before the new one. For the composite hook that is
actionlint, so a consumer pinned below 1.17.0 withDEVKIT_FEATURES_DISABLED=actionlinthad no anchor and was skipped with a warning on every upgrade, forever — while a fresh scaffold with the same opt-out does get the hook, since it sits outside the actionlint sentinels. Nowtemplate_hook_anchorsreturns every predecessor nearest-first, the insert walks them and anchors on the first one present in the consumer's file, and the warning fires only when none is. The chosen anchor's range is still whateverhook_block_rangeprefers — the sentinel range where one exists — so an insert after a presentactionlintstill lands after# <<< devkit:actionlint, outside what the feature's excision deletes.The inserted block lost its rationale. An un-sentinelled template entry was extracted byte-exact from its
- repo:line, so the consumer's copy carried none of the prose above it. The structural branch ofhook_block_rangenow sweeps upward over the contiguous run of whole-line comments, stopping at a blank line, a non-comment line, or either sentinel marker, so it can neither swallow the previous block's text nor half a sentinel pair. Only the range's start moves; the anchor path reads the end.Why not sentinel-wrap the composite entry instead (the alternative noted on the issue): sentinels mean feature-gated excision, and the composite hook belongs to no group. Decisively,
hook_block_rangematches sentinel ids by prefix, so a# >>> devkit:shellcheck-composite-actionsmarker would satisfy a lookup forshellcheck— breaking the very fallback that anchors on it. Verified against a fixture; recorded in the function's docstring so nobody takes that route later.Behaviour change to be aware of: a pin at or below 1.16.0 whose config lacks the actionlint block now gains the composite hook through the
shellcheckfallback. That is the fix, not a #1651 regression: those pins have never seen the composite hook (its row is 1.17.0); the actionlint row itself still stands down at 1.16.0. Two existing assertions were narrowed topreserved-hook-insert: actionlintfor that reason, with docstrings saying why.Type of Change
feat-- New featurefix-- Bug fixdocs-- Documentation onlychore-- Maintenance task (deps, config, etc.)refactor-- Code restructuring (no behavior change)test-- Adding or updating testsci-- CI/CD pipeline changesbuild-- Build system or dependency changesrevert-- Reverts a previous commitstyle-- Code style (formatting, whitespace)Modifiers
!) -- This change breaks backward compatibilityChanges Made
assets/init-workspace.shtemplate_hook_anchor→template_hook_anchors(all predecessors, nearest first);insert_missing_hook_blockswalks them and anchors on the first present, warning only when none ishook_block_rangestructural branch sweeps the comment run above the- repo:line (three stop conditions); the docstring records the sentinel prefix-match trap and why an ungrouped hook stays un-sentinelledtests/test_scaffold_preserved_hooks.pytest_a_disabled_anchor_does_not_block_the_hook_after_it(pin 1.16.0 + actionlint disabled ⇒[shellcheck, shellcheck-composite-actions, typos], no warning),test_the_walk_stops_at_the_nearest_present_predecessor(after the actionlint sentinel end), a seed lacking every predecessor still warns and writes nothing,test_the_inserted_block_carries_the_template_comment_above_it, andtest_the_folded_block_carries_the_template_comment(the retired-block fold shares the extractor; the consumer's own comment survives above the template's). Each RED before its fixdocs/MIGRATION.md,CHANGELOG.md(+ generated mirror)### Fixedentry with three sub-bullets (anchor walk, comment run, fold side effect). Theinserted_hook_blocks()row-order comment is corrected: order is position, not delivery (review finding)Changelog Entry
Fixed
rationale (#1725)
.pre-commit-config.yamlafter the single hook the template places immediately before it, so a consumer
pinned below 1.17.0 with
DEVKIT_FEATURES_DISABLED=actionlinthad no anchorfor
shellcheck-composite-actionsand lost the hook to a warning on everyupgrade, forever — while a fresh scaffold with the same opt-out does ship it.
The insert now walks every predecessor the template declares, nearest first,
and anchors on the first one the file carries (here
shellcheck); the warningis reached only when the file carries not one of them. A present anchor still
wins, and still contributes its sentinel range where it has one, so an
insert after a bracketed block lands past its closing sentinel rather than
inside the range a feature excision deletes
- repo:line down,so the prose above it — why the hook exists, what it deliberately does not
cover — stayed in the template and the consumer's copy arrived as an
unexplained
entry:. The structural extraction now takes the run ofwhole-line comments directly above the entry too, stopping at a blank line, a
non-comment line or a
# >>> devkit:/# <<< devkit:sentinel, so no copycan swallow the previous block's text or half a sentinel pair
now arrives with the template's rationale as well — which is what you want when
a hook's form changed under you. Your own comment above the retired block is
yours and is untouched, so an adoption PR shows the two comment blocks stacked
Testing
just test)Manual Testing Details
DEVKIT_VERSION=1.16.0,DEVKIT_FEATURES_DISABLED=actionlint, config = template minus the actionlint sentinel block and the composite block →Inserted the 'shellcheck-composite-actions' hook after 'shellcheck' …, the block lands with its template comment right aftershellcheck, nothing on stderr about anchors, YAML parses with the template's hook order;--previewreports the planned insert and writes nothinguv run pytest tests/test_scaffold_preserved_hooks.py tests/test_flake_hooks.py -q→ 110 passed (32 + 78); whole Python suite minus image/integration → 1301 passed, 1 skippedbats -j 4 --filter '1660|1651|preserved|hook|1717|1725' tests/bats/init-workspace.bats→ 48 ok; the one failure is the known environmental one (the [FEATURE] Ship actionlint to consumers: the workflow-lint hook plus a managed actionlint.yaml config #1660 opt-out test shells out to a barepython3without PyYAML in the plainnix developshell; green in CI)shellcheck -xclean;prek run --all-files→ 55 hooks passedChecklist
docs/templates/, then runjust docs)CHANGELOG.mdin the[Unreleased]section (and pasted the entry above)Additional Notes
Side effect, pinned by its own test and recorded in the changelog: the retired-block fold (Case 1) shares the extractor, so a folded replacement block now also carries the template's rationale comment, stacked under the consumer's own. The sentinel prefix match itself is left as-is and documented; hardening it to an exact-id match is #1727.
Refs: #1725