Fix: Hook commands fail - stdin JSON not parsed#1061
Fix: Hook commands fail - stdin JSON not parsed#1061thewoolleyman wants to merge 1 commit intoruvnet:mainfrom
Conversation
…stdin JSON Claude Code passes hook context as JSON via stdin, not as shell environment variables. All generated hook commands referenced nonexistent variables like $TOOL_INPUT_file_path, $PROMPT, $TOOL_SUCCESS, etc., causing every hook (PreToolUse, PostToolUse, UserPromptSubmit, SessionStart, Notification) to silently fail with empty arguments. The fix introduces .claude/hooks/hook-bridge.sh, a bridge script that reads the stdin JSON with jq, extracts the relevant fields, and forwards them as proper CLI arguments. All hook commands in settings.json and the settings-generator now route through this bridge script. Changes: - Add .claude/hooks/hook-bridge.sh bridge script - Update settings-generator.ts to emit bridge-based hook commands - Update static .claude/settings.json template - Update executor.ts to create .claude/hooks/ dir and copy bridge on init Co-Authored-By: claude-flow <ruv@ruv.net>
|
Human-generated comment: After installing claude flow, and starting to use it via Claude Code, I kept getting warnings/errors like I asked Claude Flow to identify the fix, and it determined it was in the claude flow hooks. So I had it create this MR with the fix. I'm not getting the errors anymore locally. But this does introduce JQ as a dependency, so I don't know if that is a problem. If it is, I can try to have it updated to remove the JQ dependency. Thanks... |
Claude Code passes hook context as JSON via stdin, not as shell environment variables. All hooks now use hook-bridge.sh which reads stdin JSON with jq and forwards fields as CLI arguments. Ref: ruvnet/claude-flow#1061 Co-Authored-By: claude-flow <ruv@ruv.net>
|
what OS and terminal do you use ? I'm having the same issue on windows terminal. by adding bash -c in every hooks with the type "command" I've been able to resolve the issue without jq dependency |
I think this was on my Mac OS |
Summary
All Claude Code hooks generated by
claude-flow initfail silently because they reference shell environment variables ($TOOL_INPUT_file_path,$PROMPT,$TOOL_SUCCESS, etc.) that Claude Code never sets. Claude Code passes hook context as JSON via stdin, not as environment variables.This causes errors like:
UserPromptSubmit hook errorPostToolUse:Bash hook errorPreToolUse:Edit hook errorEvery hook that needs tool input data is affected.
Root Cause
The
settings-generator.tsand static.claude/settings.jsontemplate generate hook commands like:But
$TOOL_INPUT_file_pathis never set. Claude Code actually pipes JSON to stdin:{ "hook_event_name": "PreToolUse", "tool_name": "Edit", "tool_input": { "file_path": "/path/to/file.ts" } }Fix
Introduces
.claude/hooks/hook-bridge.sh, a bridge script that:jqAll hook commands now route through the bridge:
Files changed
v3/@claude-flow/cli/.claude/hooks/hook-bridge.sh- Bridge scriptv3/@claude-flow/cli/src/init/settings-generator.ts- Generator uses bridgev3/@claude-flow/cli/src/init/executor.ts- Init copies bridge scriptv3/@claude-flow/cli/.claude/settings.json- Static template uses bridgeTest plan
echo '{"prompt":"test"}' | .claude/hooks/hook-bridge.sh routeworksecho '{"tool_input":{"file_path":"/tmp/test.ts"}}' | .claude/hooks/hook-bridge.sh pre-editworksecho '{}' | .claude/hooks/hook-bridge.sh pre-editexits cleanly (no error)npx @claude-flow/cli@latest init --forcecreates.claude/hooks/hook-bridge.shUserPromptSubmit hook errororPostToolUse:Bash hook errorin Claude CodeNote
Requires
jqto be installed. This is standard on macOS (via Xcode CLI tools) and most Linux distributions. Windows users need WSL or Git Bash (which was already effectively required by the existing bash-syntax hooks).🤖 Generated with claude-flow