diff options
| author | Jesse Vincent <jesse@fsck.com> | 2026-10-22 21:19:08 +0000 |
|---|---|---|
| committer | Jesse Vincent <jesse@fsck.com> | 2026-10-22 21:19:08 +0000 |
| commit | 3cbe5575758e409ac083c694cfc45ccfde50c0cd (patch) | |
| tree | e84d7571f7bbd0665f7f20f61ff18b680e9cdee2 | |
| parent | 0f9581f6c1525b51dc1600b14d24f3db1f601446 (diff) | |
fix(hooks): fall back to zsh, or exit quietly, when bash is missing on Unix (#2503)fix/2503-run-hook-no-bash
On a Unix system without bash, the harness can't honour shell:"bash" and
runs run-hook.cmd with zsh or sh. $BASH is unset there, so the wrapper's
exec "${BASH:-bash}" fails with "command not found: bash" (zsh) or
"exec: bash: not found" (sh), the hook exits 127, and the bootstrap is
lost on every session start.
Keep $BASH first, so the empty-PATH case from #2310 still works, then look
for bash on PATH, then zsh, which runs session-start unchanged (its output
is byte-identical to bash's, including for a SKILL.md full of backslashes,
quotes, tabs and CRs). With neither shell, exit 0 quietly, as the Windows
half already does.
Reproduced with Claude Code 2.1.308 in an alpine container with zsh and no
bash: the hook exited 127 and no bootstrap reached the model before the
change; after it the hook exits 0 and the bootstrap arrives.
Tests run the wrapper under real zsh and under a non-bash sh with a PATH
that holds only zsh, and under that sh with neither shell. They skip where
zsh is absent or sh is bash (Git Bash, macOS).
Co-authored-by: Twot <twotnguyen@users.noreply.github.com>
Claude-Session: https://claude.ai/code/session_01SHANcr4A71nzuU9et4Fnba
| -rwxr-xr-x | hooks/run-hook.cmd | 17 | ||||
| -rwxr-xr-x | tests/hooks/test-session-start.sh | 43 |
2 files changed, 59 insertions, 1 deletions
diff --git a/hooks/run-hook.cmd b/hooks/run-hook.cmd index a2f7c48..928aaba 100755 --- a/hooks/run-hook.cmd +++ b/hooks/run-hook.cmd @@ -59,4 +59,19 @@ case "$0" in esac SCRIPT_NAME="$1" shift -exec "${BASH:-bash}" "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@" + +# Without bash installed, the harness runs this file with zsh or sh and $BASH +# is unset (#2503). session-start runs unchanged under zsh, so fall back to +# it, and otherwise exit quietly like the Windows half: the plugin still +# works, just without SessionStart context injection. +if [ -n "${BASH:-}" ]; then + shell="$BASH" +elif command -v bash >/dev/null 2>&1; then + shell="bash" +elif command -v zsh >/dev/null 2>&1; then + shell="zsh" +else + exit 0 +fi + +exec "$shell" "${SCRIPT_DIR}/${SCRIPT_NAME}" "$@" diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh index 6ddf011..10bd94e 100755 --- a/tests/hooks/test-session-start.sh +++ b/tests/hooks/test-session-start.sh @@ -247,6 +247,49 @@ assert_command_output \ CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ "$bash_bin" "$WRAPPER_UNDER_TEST" session-start +# On a Unix system with zsh but no bash (#2503), the harness can't honour +# shell:"bash" and runs run-hook.cmd with zsh or sh. $BASH is unset there, so +# the wrapper must find zsh, which runs session-start unchanged. sh only +# counts when it isn't bash (Git Bash's and macOS's sh are bash, which sets +# $BASH and never reaches the fallback). +posix_sh="$(command -v sh)" +if [[ -n "$(env -i "$posix_sh" -c 'echo "${BASH:-}"')" ]]; then + posix_sh="" +fi +no_bash_bin="$TEST_ROOT/no-bash/bin" +mkdir -p "$no_bash_bin" +if zsh_bin="$(command -v zsh)"; then + ln -s "$zsh_bin" "$no_bash_bin/zsh" + for runner_bin in "$zsh_bin" ${posix_sh:+"$posix_sh"}; do + assert_command_output \ + "run-hook.cmd run by ${runner_bin##*/} without bash dispatches session-start through zsh" \ + "nested" \ + "" \ + "" \ + "$(make_home "no-bash-${runner_bin##*/}")" \ + PATH="$no_bash_bin" \ + CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$runner_bin" "$WRAPPER_UNDER_TEST" session-start + done +else + echo " [SKIP] run-hook.cmd without bash: zsh not installed" +fi + +# With neither bash nor zsh, exit 0 quietly like the Windows half: the plugin +# still works, just without the bootstrap. +if [[ -n "$posix_sh" ]]; then + no_shell_rc=0 + no_shell_out="$(env -i PATH="$TEST_ROOT/no-shell" HOME="$TEST_ROOT" CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ + "$posix_sh" "$WRAPPER_UNDER_TEST" session-start 2>&1)" || no_shell_rc=$? + if [[ "$no_shell_rc" -eq 0 && -z "$no_shell_out" ]]; then + pass "run-hook.cmd with neither bash nor zsh exits 0 with no output" + else + fail "run-hook.cmd with neither bash nor zsh exits 0 with no output (rc=$no_shell_rc, out=$no_shell_out)" + fi +else + echo " [SKIP] run-hook.cmd with neither bash nor zsh: sh is bash here" +fi + # Run by bare filename from inside hooks/ ($0 has no slash), the plugin root # must still resolve. bare_name_home="$(make_home bare-name)" |
