fix: wrap hook commands in bash -c for paths with spaces - #47
Conversation
Problem: Claude Code hooks fail when $CLAUDE_PROJECT_DIR contains spaces (e.g., "Groove Jones Dropbox"). The shell splits the path on spaces even when quoted in settings.json. Solution: Wrap all commands using $CLAUDE_PROJECT_DIR in bash -c '...' so bash handles variable expansion with proper quoting. Changes: - Wrapped 25 hooks using $CLAUDE_PROJECT_DIR with bash -c - Preserved all timeout values - Left $HOME hooks unchanged (no spaces in home paths) Pattern applied: Before: python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/script.py" After: bash -c 'python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/script.py"'
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
.claude/settings.json (1)
249-249: Redundant bash wrapping.This command wraps
bashinsidebash -c, creating two bash processes. While functional, it could be simplified:-"command": "bash -c 'bash \"$CLAUDE_PROJECT_DIR/.claude/plugins/braintrust-tracing/hooks/stop_hook.sh\"'" +"command": "bash -c '\"$CLAUDE_PROJECT_DIR/.claude/plugins/braintrust-tracing/hooks/stop_hook.sh\"'"Alternatively, if the script has a proper shebang, you could execute it directly. However, if explicit bash invocation is required for compatibility, the current approach is acceptable.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting
⛔ Files ignored due to path filters (1)
.claude/hooks/package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (1)
.claude/settings.json
🔇 Additional comments (7)
.claude/settings.json (7)
4-4: LGTM: statusLine command properly wrapped.The
bash -cwrapping with escaped quotes correctly handles spaces in$CLAUDE_PROJECT_DIR.
12-76: LGTM: PreToolUse hooks properly wrapped.All PreToolUse hooks referencing
$CLAUDE_PROJECT_DIRare consistently wrapped inbash -cwith proper quoting.
101-106: LGTM: SessionStart hooks properly wrapped.All SessionStart hooks using
$CLAUDE_PROJECT_DIRare correctly wrapped inbash -cwith proper quoting.Also applies to: 125-130
145-145: LGTM: UserPromptSubmit hooks properly wrapped.All UserPromptSubmit hooks using
$CLAUDE_PROJECT_DIRare correctly wrapped inbash -c.Also applies to: 150-150, 155-155
176-186: LGTM: PostToolUse hooks properly wrapped.All PostToolUse hooks using
$CLAUDE_PROJECT_DIRare correctly wrapped inbash -cwith proper quoting.Also applies to: 224-224, 234-234
277-277: LGTM: Trailing newline added.Good practice to end JSON files with a newline.
87-87: Address the inconsistency between$HOMEand$CLAUDE_PROJECT_DIRpath wrapping.All
$CLAUDE_PROJECT_DIRcommands are wrapped inbash -c, but$HOMEpaths (lines 87, 97, 116, 141, 160, 171, 196, 205, 215, 263, 267, 271) are not. If the wrapping is necessary to prevent quote stripping when paths contain spaces,$HOMEcould face the same issue. Either wrap$HOMEcommands consistently, or document why they're exempt from this treatment.
|
Hey @carmandale - nice fix! This properly handles the spaced paths issue. There are merge conflicts with Thanks! — v3 bot 🤖 |
Summary
$CLAUDE_PROJECT_DIRinbash -c '...'to handle paths containing spacesProblem
When
$CLAUDE_PROJECT_DIRcontains spaces, hooks fail because Claude Code strips quotes before shell execution, causing word splitting:Solution
Wrap commands in
bash -c '...'so bash handles variable expansion with proper quoting:Changes
.claude/settings.json- Wrapped 25 hooks using$CLAUDE_PROJECT_DIR$HOMEunchanged (macOS home paths never have spaces)Testing
python3 -c "import json; json.load(open('.claude/settings.json'))"grep 'CLAUDE_PROJECT_DIR' .claude/settings.json | grep -v "bash -c" | wc -lreturns 0Risk
Low - Pure find-and-replace transformation. ~5ms overhead per hook is negligible.
🤖 Generated with Claude Code
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.