everything-claude-code/docs/fixes/apply-hook-fix.sh
suusuu0927 527c18b99f fix: resolve Claude Code Bash hook "cannot execute binary file" on Windows
Root cause in ~/.claude/settings.local.json (user-global):
1. UTF-8 BOM + CRLF line endings left by patch_settings_cl_v2_simple.ps1
2. Double-wrapped command "\"bash.exe\" \"wrapper.sh\"" broke Windows
   argument splitting on the space in "Program Files", making bash.exe
   try to execute itself as a script.

Fix:
- Rewrite settings.local.json as UTF-8 (no BOM), LF, with the hook command
  pointing directly at observe-wrapper.sh and passing "pre"/"post" as a
  positional arg so HOOK_PHASE is populated correctly in observe.sh.

Docs:
- docs/fixes/HOOK-FIX-20260421.md — full root-cause analysis.
- docs/fixes/apply-hook-fix.sh — idempotent applier script.
2026-04-21 14:59:24 +09:00

61 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# Apply ECC hook fix to ~/.claude/settings.local.json.
#
# - Creates a timestamped backup next to the original.
# - Rewrites the file as UTF-8 (no BOM), LF line endings.
# - Routes hook commands directly at observe-wrapper.sh with a "pre"/"post" arg.
#
# Related fix doc: docs/fixes/HOOK-FIX-20260421.md
set -euo pipefail
TARGET="${1:-$HOME/.claude/settings.local.json}"
WRAPPER="${ECC_OBSERVE_WRAPPER:-$HOME/.claude/skills/continuous-learning/hooks/observe-wrapper.sh}"
if [ ! -f "$WRAPPER" ]; then
echo "[hook-fix] wrapper not found: $WRAPPER" >&2
exit 1
fi
mkdir -p "$(dirname "$TARGET")"
if [ -f "$TARGET" ]; then
ts="$(date +%Y%m%d-%H%M%S)"
cp "$TARGET" "$TARGET.bak-hookfix-$ts"
echo "[hook-fix] backup: $TARGET.bak-hookfix-$ts"
fi
# Convert wrapper path to forward-slash form for JSON.
wrapper_fwd="$(printf '%s' "$WRAPPER" | tr '\\\\' '/')"
# Write the new config as UTF-8 (no BOM), LF line endings.
printf '%s\n' '{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "'"$wrapper_fwd"' pre"
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "'"$wrapper_fwd"' post"
}
]
}
]
}
}' > "$TARGET"
echo "[hook-fix] wrote: $TARGET"
echo "[hook-fix] restart the claude CLI for changes to take effect"