Skip to content

fix(init): anchor a preserved-hook insert on the nearest present predecessor - #1728

Merged
c-vigo merged 10 commits into
devfrom
bugfix/1725-hook-insert-anchor-fallback
Sep 25, 2026
Merged

c-vigo merged 10 commits into
devfrom
bugfix/1725-hook-insert-anchor-fallback

Conversation

@c-vigo

@c-vigo c-vigo commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Description

Two defects in how a new scaffolded hook reaches a consumer's preserved .pre-commit-config.yaml, both exposed by the 1.17.0 shellcheck-composite-actions row 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 with DEVKIT_FEATURES_DISABLED=actionlint had 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. Now template_hook_anchors returns 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 whatever hook_block_range prefers — the sentinel range where one exists — so an insert after a present actionlint still 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 of hook_block_range now 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_range matches sentinel ids by prefix, so a # >>> devkit:shellcheck-composite-actions marker would satisfy a lookup for shellcheck — 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 shellcheck fallback. 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 to preserved-hook-insert: actionlint for that reason, with docstrings saying why.

Type of Change

  • feat -- New feature
  • fix -- Bug fix
  • docs -- Documentation only
  • chore -- Maintenance task (deps, config, etc.)
  • refactor -- Code restructuring (no behavior change)
  • test -- Adding or updating tests
  • ci -- CI/CD pipeline changes
  • build -- Build system or dependency changes
  • revert -- Reverts a previous commit
  • style -- Code style (formatting, whitespace)

Modifiers

  • Breaking change (!) -- This change breaks backward compatibility

Changes Made

assets/init-workspace.sh

  • template_hook_anchor → template_hook_anchors (all predecessors, nearest first); insert_missing_hook_blocks walks them and anchors on the first present, warning only when none is
  • hook_block_range structural 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-sentinelled

tests/test_scaffold_preserved_hooks.py

  • test_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, and test_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 fix

docs/MIGRATION.md, CHANGELOG.md (+ generated mirror)

  • Consumer prose: the block lands after the nearest predecessor the file still carries, and an opt-out no longer withholds the hooks behind it; ### Fixed entry with three sub-bullets (anchor walk, comment run, fold side effect). The inserted_hook_blocks() row-order comment is corrected: order is position, not delivery (review finding)

Changelog Entry

Fixed

  • Preserved-config hook inserts survive a disabled anchor and keep their
    rationale
    (#1725)
    • A new scaffolded hook was inserted into a preserved .pre-commit-config.yaml
      after the single hook the template places immediately before it, so a consumer
      pinned below 1.17.0 with DEVKIT_FEATURES_DISABLED=actionlint had no anchor
      for shellcheck-composite-actions and lost the hook to a warning on every
      upgrade, 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 warning
      is 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
    • An un-sentinelled template entry was extracted from its - 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 of
      whole-line comments directly above the entry too, stopping at a blank line, a
      non-comment line or a # >>> devkit: / # <<< devkit: sentinel, so no copy
      can swallow the previous block's text or half a sentinel pair
    • The retired-block fold reads the same extraction, so a replacement block
      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

  • Tests pass locally (just test)
  • Manual testing performed (describe below)

Manual Testing Details

  • End-to-end with the real script: 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 after shellcheck, nothing on stderr about anchors, YAML parses with the template's hook order; --preview reports the planned insert and writes nothing
  • uv 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 skipped
  • bats -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 bare python3 without PyYAML in the plain nix develop shell; green in CI)
  • shellcheck -x clean; prek run --all-files → 55 hooks passed

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly (edit docs/templates/, then run just docs)
  • I have updated CHANGELOG.md in the [Unreleased] section (and pasted the entry above)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

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

@c-vigo
c-vigo merged commit 641b6ed into dev Sep 25, 2026
15 checks passed
@c-vigo
c-vigo deleted the bugfix/1725-hook-insert-anchor-fallback branch September 25, 2026 21:31
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.

1 participant