summaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'hooks')
-rwxr-xr-xhooks/run-hook.cmd52
-rwxr-xr-xhooks/session-start48
2 files changed, 74 insertions, 26 deletions
diff --git a/hooks/run-hook.cmd b/hooks/run-hook.cmd
index ceec3a7..a2f7c48 100755
--- a/hooks/run-hook.cmd
+++ b/hooks/run-hook.cmd
@@ -15,32 +15,48 @@ if "%~1"=="" (
exit /b 1
)
+setlocal
set "HOOK_DIR=%~dp0"
+set "BASH_EXE="
-REM Try Git for Windows bash in standard locations
-if exist "C:\Program Files\Git\bin\bash.exe" (
- "C:\Program Files\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
- exit /b %ERRORLEVEL%
-)
-if exist "C:\Program Files (x86)\Git\bin\bash.exe" (
- "C:\Program Files (x86)\Git\bin\bash.exe" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
- exit /b %ERRORLEVEL%
-)
+REM Git for Windows in its standard system-wide locations
+if exist "C:\Program Files\Git\bin\bash.exe" set "BASH_EXE=C:\Program Files\Git\bin\bash.exe"
+if not defined BASH_EXE if exist "C:\Program Files (x86)\Git\bin\bash.exe" set "BASH_EXE=C:\Program Files (x86)\Git\bin\bash.exe"
-REM Try bash on PATH (e.g. user-installed Git Bash, MSYS2, Cygwin)
-where bash >nul 2>nul
-if %ERRORLEVEL% equ 0 (
- bash "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
- exit /b %ERRORLEVEL%
-)
+REM Per-user Git for Windows install (no admin rights needed). The defined
+REM check matters: with LOCALAPPDATA unset the path would collapse to
+REM \Programs\Git\... on the current drive, where any user can create it.
+if not defined BASH_EXE if defined LOCALAPPDATA if exist "%LOCALAPPDATA%\Programs\Git\bin\bash.exe" set "BASH_EXE=%LOCALAPPDATA%\Programs\Git\bin\bash.exe"
+
+REM bash on PATH (MSYS2, Cygwin, a non-default Git install). where.exe is
+REM called by full path and with $PATH: so neither it nor bash can come from
+REM the current directory. The filters use for-variable modifiers, which never
+REM re-expand the path, and skip extensionless matches and the WSL launchers
+REM (System32, Sysnative, the Store alias in WindowsApps), which fail when no
+REM Linux distro is installed.
+if not defined BASH_EXE if defined SystemRoot for /f "delims=" %%B in ('"%SystemRoot%\System32\where.exe" $PATH:bash 2^>nul') do if not defined BASH_EXE if not "%%~xB"=="" if /i not "%%~dpB"=="%SystemRoot%\System32\" if /i not "%%~dpB"=="%SystemRoot%\Sysnative\" if /i not "%%~dpB"=="%LOCALAPPDATA%\Microsoft\WindowsApps\" set "BASH_EXE=%%B"
REM No bash found - exit silently rather than error
REM (plugin still works, just without SessionStart context injection)
-exit /b 0
+if not defined BASH_EXE exit /b 0
+
+REM Run bash outside any parenthesized block: cmd expands %ERRORLEVEL%
+REM inside a block when it parses the block, which would lose the hook's
+REM exit code.
+"%BASH_EXE%" "%HOOK_DIR%%~1" %2 %3 %4 %5 %6 %7 %8 %9
+exit /b %ERRORLEVEL%
CMDBLOCK
# Unix: run the named script directly
-SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+# Splits $0 on its last / or \ (cmd passes Windows paths) instead of calling
+# dirname, and uses $BASH (bash's own absolute path, always set once bash is
+# running) instead of a PATH lookup for bash, so this keeps working when
+# Claude Code spawns the SessionStart hook with a broken/empty PATH
+# (anthropics/claude-code#43127).
+case "$0" in
+ */*|*\\*) SCRIPT_DIR="$(cd "${0%[/\\]*}" && pwd)" ;;
+ *) SCRIPT_DIR="$(pwd)" ;;
+esac
SCRIPT_NAME="$1"
shift
-exec bash "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@"
+exec "${BASH:-bash}" "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@"
diff --git a/hooks/session-start b/hooks/session-start
index 083cb23..63f82a6 100755
--- a/hooks/session-start
+++ b/hooks/session-start
@@ -3,12 +3,26 @@
set -euo pipefail
-# Determine plugin root directory
-SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+# Determine plugin root directory.
+# Splits $0 on its last / or \ (run-hook.cmd's cmd half passes a Windows
+# path), with no separator meaning the current directory. This avoids
+# dirname because a SessionStart:startup hook can run with a broken/empty
+# PATH (anthropics/claude-code#43127) before any PATH-repairing sibling hook
+# in the same startup batch takes effect.
+case "$0" in
+ */*|*\\*) SCRIPT_DIR="$(cd "${0%[/\\]*}" && pwd)" ;;
+ *) SCRIPT_DIR="$(pwd)" ;;
+esac
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
-# Read using-superpowers content
-using_superpowers_content=$(cat "${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md" 2>&1 || echo "Error reading using-superpowers skill")
+# Read using-superpowers content via bash's builtin $(< file) instead of
+# cat, for the same PATH-independence reason.
+skill_md="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
+if [ -r "$skill_md" ]; then
+ using_superpowers_content="$(<"$skill_md")"
+else
+ using_superpowers_content="Error reading using-superpowers skill"
+fi
# Escape string for JSON embedding using bash parameter substitution.
# Each ${s//old/new} is a single C-level pass - orders of magnitude
@@ -36,18 +50,36 @@ session_context="<EXTREMELY_IMPORTANT>\nYou have superpowers.\n\n**Below is the
#
# Uses printf instead of heredoc to work around bash 5.3+ heredoc hang.
# See: https://github.com/obra/superpowers/issues/571
+#
+# emit_json pipes printf through `cat` when cat is on PATH, to absorb EPIPE
+# on Windows. On Windows + Git Bash, the harness can close the hook's stdout
+# before printf finishes, and Git Bash's printf reports that EPIPE as
+# "printf: write error: Permission denied", which `set -euo pipefail` lets
+# escape as a startup diagnostic every session. Letting cat receive the EPIPE
+# keeps it out of the hook's error path (#1612). When PATH is broken and cat
+# can't be found (#2310), it falls back to printf alone so context still
+# gets injected.
+emit_json() {
+ # shellcheck disable=SC2059 # every caller passes a literal format string
+ if command -v cat >/dev/null 2>&1; then
+ printf "$@" | cat
+ else
+ printf "$@"
+ fi
+}
+
if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
# Cursor sets CURSOR_PLUGIN_ROOT (may also set CLAUDE_PLUGIN_ROOT)
- printf '{\n "additional_context": "%s"\n}\n' "$session_context" | cat
+ emit_json '{\n "additional_context": "%s"\n}\n' "$session_context"
elif [ -n "${CLAUDE_PLUGIN_ROOT:-}" ] && [ -z "${COPILOT_CLI:-}" ] && [ -z "${MUSE_PLUGIN_ROOT:-}" ]; then
# Claude Code sets CLAUDE_PLUGIN_ROOT without COPILOT_CLI/MUSE_PLUGIN_ROOT
- printf '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' "$session_context" | cat
+ emit_json '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' "$session_context"
elif [ -n "${MUSE_PLUGIN_ROOT:-}" ]; then
# Muse sets MUSE_PLUGIN_ROOT — try Claude-style nested output for Muse Spark
- printf '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' "$session_context" | cat
+ emit_json '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' "$session_context"
else
# Copilot CLI (sets COPILOT_CLI=1) or unknown platform — SDK standard format
- printf '{\n "additionalContext": "%s"\n}\n' "$session_context" | cat
+ emit_json '{\n "additionalContext": "%s"\n}\n' "$session_context"
fi
exit 0