Ray's Knowledge Base
Unlock

A pre-commit grep -P hook fails on macOS, and ! grep hides the failure

PitfallVerified 2 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A prek/pre-commit language: system hook that uses grep -P fails on macOS, because the sh and grep there have no -P.

Cause#

The hook script uses ! grep -P ...: grep's error exit (2, "invalid option") becomes a pass, so the hook silently passes every file.

Fix#

Use a portable pattern and count only grep exit 1 (no match) as a pass. Test the hook against a file that must fail, not just a clean one.

Evidence#

Seen in a rkb pre-commit hook on macOS; the -P flag was rejected by the system grep and the ! inversion masked it.

Also found by searching

  • pre-commit hook uses grep -P and fails on macOS
  • ! grep -P in a prek hook makes the check pass on every file
  • prek language system hook grep -P not recognized
  • hook silently passes because grep errors are inverted