Skip to content

Return exit code 1 from export hooks when files are modified#1725

Merged
nathanjmcdougall merged 3 commits intomainfrom
copilot/modify-hooks-exit-code
Apr 1, 2026
Merged

Return exit code 1 from export hooks when files are modified#1725
nathanjmcdougall merged 3 commits intomainfrom
copilot/modify-hooks-exit-code

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Pre-commit convention: hooks that modify files must return exit code 1 so pre-commit knows to re-stage and re-run. Our three export hooks (export-functions, export-module-tree, export-skills-directory) always returned 0 after writing.

  • Export hooks: Compare generated content against existing file before writing. Write only on diff. Return 1 if modified, 0 if unchanged.
  • Refactor export-skills-directory.py: Extract _collect_skills() helper to stay within ruff's PLR0912 branch limit after adding the comparison logic.
  • Skill docs: Update usethis-prek-hook-bespoke-create skill to document this exit code convention for future hook authors.

Core pattern applied to each hook:

try:
    existing = output_file.read_text(encoding="utf-8")
except FileNotFoundError:
    existing = None

modified = content != existing
if modified:
    output_file.parent.mkdir(parents=True, exist_ok=True)
    output_file.write_text(content, encoding="utf-8")

return 1 if modified else 0

Modify export-functions.py, export-module-tree.py, and
export-skills-directory.py to compare new content with existing file
content and return exit code 1 when the file is actually changed. This
follows the pre-commit convention that hooks modifying files should
signal the change via a non-zero exit code.

Also update the usethis-prek-hook-bespoke-create skill to document this
principle.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/76cfba7e-2df3-47e6-92d3-248a8eb6332a

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Update hooks to return exit code 1 as required Return exit code 1 from export hooks when files are modified Apr 1, 2026
Copilot AI requested a review from nathanjmcdougall April 1, 2026 11:58
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 1, 2026 12:00
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 1, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/modify-hooks-exit-code (b94e9a1) with main (b0c8531)

Open in CodSpeed

@nathanjmcdougall nathanjmcdougall merged commit c08fe12 into main Apr 1, 2026
10 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/modify-hooks-exit-code branch April 1, 2026 12:11
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.

hooks which modify code should return exit code 1

2 participants