Skip to content

fix(hooks): prevent stdin drain from skipping subsequent hooks#165

Open
acevif wants to merge 4 commits intocoderabbitai:mainfrom
acevif:fix/hooks-stdin-drain
Open

fix(hooks): prevent stdin drain from skipping subsequent hooks#165
acevif wants to merge 4 commits intocoderabbitai:mainfrom
acevif:fix/hooks-stdin-drain

Conversation

@acevif
Copy link
Copy Markdown

@acevif acevif commented Apr 8, 2026

Summary

Fixes #164

All hook phases (postCreate, preRemove, postRemove, postCd) suffered from the same bug: a hook that reads from stdin (e.g. cat, read) drains the heredoc that feeds hook lines, causing all subsequent hooks to be silently skipped.

  • run_hooks: add </dev/null to the hook subshell (...) </dev/null
  • run_hooks_export: add </dev/null to the eval call (eval "$hook" </dev/null)

Both fixes prevent the hook from inheriting the heredoc as stdin while preserving all existing behavior (env var exports, exit code propagation, subshell isolation).

Test plan

  • Regression test added for postCreate — verifies cat between two hooks does not skip the third
  • Same regression test added for preRemove and postRemove
  • Same regression test added for postCd (via run_hooks_export)
  • All four tests confirmed to fail before the fix and pass after
  • Full test suite passes (bats tests/ — 368 → 372 tests, all green)

Summary by CodeRabbit

  • Bug Fixes
    • Improved hook execution reliability by preventing stdin interference between sequential hooks, ensuring all configured hooks continue executing even when one attempts to read from standard input.

acevif added 4 commits April 9, 2026 01:55
Add </dev/null to hook subshell to prevent commands that read stdin
(e.g. cat, read) from consuming the heredoc that feeds hook lines,
which caused all subsequent hooks to be silently skipped.

Fixes coderabbitai#164
Verify that a hook running `cat` (which reads stdin) does not
prevent subsequent hooks from executing.
Add </dev/null to eval in run_hooks_export so hooks that read stdin
(e.g. cat, read) do not consume the heredoc feeding hook lines,
which caused subsequent postCd hooks to be silently skipped.

Also adds a regression test for the postCd case.
@acevif acevif requested a review from NatoBoram as a code owner April 8, 2026 17:11
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7a661e38-c57b-4704-ade6-bb0e084a659c

📥 Commits

Reviewing files that changed from the base of the PR and between c765e62 and 1a127be.

📒 Files selected for processing (2)
  • lib/hooks.sh
  • tests/hooks.bats

Walkthrough

Fixed a bug where hook execution would stop prematurely if any hook read from stdin by redirecting stdin from /dev/null in hook execution blocks. Added comprehensive test coverage verifying hook continuity across stdin-reading hooks in both standard and exported hook execution paths.

Changes

Cohort / File(s) Summary
Hook execution stdin handling
lib/hooks.sh
Added stdin redirection (</dev/null) to hook execution in run_hooks and run_hooks_export to prevent hooks from consuming the parent loop's stdin.
Hook execution test coverage
tests/hooks.bats
Added tests for postCreate, preRemove, postRemove (via run_hooks) and postCd (via run_hooks_export) verifying that subsequent hooks execute even when earlier hooks read from stdin.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hook that reads, once broke the chain,
Draining stdin like gentle rain,
Now /dev/null keeps things just right,
Each hook runs bright, the flow takes flight! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary fix: redirecting stdin from /dev/null to prevent stdin-draining hooks from skipping subsequent hooks.
Linked Issues check ✅ Passed The PR fully addresses issue #164 by adding </dev/null redirections to both run_hooks and run_hooks_export, and includes comprehensive regression tests verifying the fix works for all affected hook types.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the stdin-draining issue in hooks: modifications to stdin handling in lib/hooks.sh and corresponding regression tests in tests/hooks.bats, with no unrelated alterations.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

postCreate hooks silently stop if a hook reads from stdin

1 participant