Skip to content

fix: Windows hook detection and line ending handling#209

Closed
micah-loftin-mediware wants to merge 5 commits intortk-ai:masterfrom
micah-loftin-mediware:feature/fix-windows-hook-detection
Closed

fix: Windows hook detection and line ending handling#209
micah-loftin-mediware wants to merge 5 commits intortk-ai:masterfrom
micah-loftin-mediware:feature/fix-windows-hook-detection

Conversation

@micah-loftin-mediware
Copy link

Summary

  • Detects shell environment at runtime for Windows hook support
  • Normalizes line endings for bash hooks on Windows

Changes

  1. Added runtime shell detection to determine the correct hook extension (.sh vs .ps1)
  2. Implemented CRLF to LF conversion for bash hooks on Windows
  3. Updated settings initialization to handle both bash and PowerShell hooks

Testing

  • Verified hook execution on Windows with MSYS/Git Bash
  • Confirmed line ending normalization prevents bash execution errors
  • Tested with both .sh and .ps1 hook files

Related Issues

Fixes hook errors on Windows when using Git Bash/MSYS environments

🤖 Generated with Claude Code

Previously, RTK used compile-time OS checks (#[cfg(unix)]) to determine
whether to allow hook installation. This blocked hook installation on
Windows even when Claude Code runs in a bash environment (Git Bash/WSL).

Changes:
- Add is_bash_environment() runtime check (executes bash --version)
- Remove #[cfg(unix)] guards from run_default_mode() and run_hook_only_mode()
- Update ensure_hook_installed() to handle permissions cross-platform
- Update show_config() to display hook status on Windows
- Update test_default_mode_creates_hook_and_rtk_md() for cross-platform

Benefits:
- Claude Code on Windows can now use hook-based mode
- Automatic fallback to --claude-md mode if bash not found
- Better error messages guiding Windows users to install Git Bash/WSL

Testing:
- All 393 tests pass
- Cross-platform test now works on both Unix and Windows
- Zero clippy warnings introduced

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Windows Git auto-converts LF to CRLF by default, breaking bash shebangs.
Previously, installed hooks had CRLF line endings, causing hook errors.

Changes:
- Add .gitattributes to enforce LF for .sh, .rs, .md, .toml, .json files
- Add normalize_line_endings() to strip \r\n -> \n before writing hooks
- Convert hooks/rtk-rewrite.sh to LF line endings (10465 -> 10256 bytes)
- Add tests: test_hook_has_lf_line_endings, test_normalize_line_endings

Before:
  $ head -1 ~/.claude/hooks/rtk-rewrite.sh | od -c
  #   !   /   b   i   n   /   b   a   s   h  \r  \n   (CRLF - broken)

After:
  $ head -1 ~/.claude/hooks/rtk-rewrite.sh | od -c
  #   !   /   b   i   n   /   b   a   s   h  \n       (LF - works)

Testing:
- All 28 init tests pass (including 3 new tests)
- Verified hook installs with LF on Windows
- File size correct: 10256 bytes (not 10465 with CRLF)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add comprehensive debug logging to rtk-rewrite.sh hook controlled by
RTK_HOOK_DEBUG environment variable. When set to "1", logs hook
invocation details, environment variables, tool locations, and command
transformations to ~/.claude/hooks/rtk-rewrite.log.

Features:
- Conditional logging only when RTK_HOOK_DEBUG=1 (silent by default)
- PATH augmentation to include ~/.local/bin and ~/.cargo/bin
- Silent guards for missing dependencies (rtk, jq)
- Error trapping and logging for troubleshooting
- Logs hook context: PWD, BASH_VERSION, SHELL, HOME, USER
- Logs tool detection: rtk and jq locations
- Logs input JSON and extracted command

Also updated CLAUDE.md with RTK usage instructions covering all
supported commands and token savings metrics.

Verified: RTK metrics tracking works correctly (63.4% savings on git status)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add debug output to show when commands don't match any rewrite
patterns and log the rewritten command when a match is found.

Debug output (when RTK_HOOK_DEBUG=1):
- "No rewrite pattern matched, exiting" when command passes through
- "REWRITTEN: <cmd>" showing the transformed command

This helps troubleshoot hook behavior and verify command rewrites
are working correctly (e.g., gh pr view -> rtk gh pr view).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@micah-loftin-mediware micah-loftin-mediware deleted the feature/fix-windows-hook-detection branch February 19, 2026 23:12
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

Comments