diff options
| author | Drew Ritter <drew@primeradiant.com> | 2026-11-26 00:46:47 -0800 |
|---|---|---|
| committer | Drew Ritter <drew@primeradiant.com> | 2026-11-26 00:46:47 -0800 |
| commit | 3d1bd8f963d1ef8ea1a01dc9b17729507c1ec6c4 (patch) | |
| tree | 90d677c590c236b58c4c1b7ca5a525c5b5687f2f | |
| parent | da0d65d55330d591b03b6b7d818dc7dd7746e4e1 (diff) | |
| parent | f27e901302c8294370ac77bfba148c7d72bc8b78 (diff) | |
Merge branch 'fix/wave1-run-hook-heredoc' into fix/wave1-public-docs-and-erratafix/wave1-public-docs-and-errata
| -rw-r--r-- | docs/windows/polyglot-hooks.md | 14 | ||||
| -rwxr-xr-x | hooks/session-start | 6 | ||||
| -rwxr-xr-x | tests/hooks/test-session-start.sh | 31 |
3 files changed, 34 insertions, 17 deletions
diff --git a/docs/windows/polyglot-hooks.md b/docs/windows/polyglot-hooks.md index 0379037..d6970e1 100644 --- a/docs/windows/polyglot-hooks.md +++ b/docs/windows/polyglot-hooks.md @@ -176,7 +176,12 @@ Your hook logic goes in the extensionless script file. A few portable patterns: nl=' ' tab=' ' -cr=$(printf '\r') +if [ -n "${BASH_VERSION:-}" ]; then + # shellcheck disable=SC3045 # bash builtin avoids a subshell fork (#2081) + printf -v cr '\r' +else + cr=$(printf '\r' 2>/dev/null || true) +fi replace_all() { rest=$1 @@ -202,8 +207,13 @@ escape_for_json() { [ -z "$cr" ] || replace_all "$escaped" "$cr" '\r' replace_all "$escaped" "$nl" '\n' replace_all "$escaped" "$tab" '\t' - printf '%s' "$escaped" + using_superpowers_escaped=$escaped } + +# The result is left in a variable instead of printed, because capturing +# output with $(...) forks. Call the function, then read the variable: +# escape_for_json "$content" +# json=$using_superpowers_escaped ``` ## Troubleshooting diff --git a/hooks/session-start b/hooks/session-start index 2ad81f0..fc71eb8 100755 --- a/hooks/session-start +++ b/hooks/session-start @@ -29,8 +29,10 @@ else cr=$(printf '\r' 2>/dev/null || true) fi -# Read using-superpowers content via POSIX read (no cat, no $(< file) subshell), -# dropping trailing blank lines as $(< file) did. +# Read using-superpowers content with the read builtin (no cat, for the same +# PATH-independence reason, and no $(< file) subshell). read returns 1 at end +# of file, hence || [ -n "$line" ]; the loop drops trailing newlines as +# $(< file) did. skill_md="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md" using_superpowers_content="Error reading using-superpowers skill" if [ -r "$skill_md" ]; then diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh index 9332f42..79aefb7 100755 --- a/tests/hooks/test-session-start.sh +++ b/tests/hooks/test-session-start.sh @@ -352,15 +352,22 @@ if [[ -n "$posix_sh" ]]; then CLAUDE_PLUGIN_ROOT="$REPO_ROOT" \ "$posix_sh" "$WRAPPER_UNDER_TEST" session-start fi +else + echo " [SKIP] run-hook.cmd/session-start sh-only tests: no non-bash/non-zsh POSIX sh found" +fi - # Adversarial escaping test under POSIX sh (backslashes, quotes, tabs, globs, CRLF) - tricky_root="$TEST_ROOT/tricky-plugin" - mkdir -p "$tricky_root/hooks" "$tricky_root/skills/using-superpowers" - cp "$HOOK_UNDER_TEST" "$tricky_root/hooks/session-start" - printf 'Line1: C:\\Users\\new\\tab"quote\\*?[foo]\\bar\r\nLine2:\twith\ttabs and ""quotes""\n\n' > "$tricky_root/skills/using-superpowers/SKILL.md" - tricky_json="$(env -i PATH="" HOME="$(make_home sh-tricky)" CLAUDE_PLUGIN_ROOT="$tricky_root" \ - "$posix_sh" "$tricky_root/hooks/session-start")" - if node -e ' +# Adversarial escaping test (backslashes, quotes, tabs, globs, CRLF) under +# every shell the wrapper can pick. bash gets CR through printf -v, the +# others through a command substitution. +tricky_root="$TEST_ROOT/tricky-plugin" +mkdir -p "$tricky_root/hooks" "$tricky_root/skills/using-superpowers" +cp "$HOOK_UNDER_TEST" "$tricky_root/hooks/session-start" +printf 'Line1: C:\\Users\\new\\tab"quote\\*?[foo]\\bar\r\nLine2:\twith\ttabs and ""quotes""\n\n' > "$tricky_root/skills/using-superpowers/SKILL.md" +for tricky_sh in ${posix_sh:+"$posix_sh"} "$BASH" ${zsh_bin:+"$zsh_bin"}; do + tricky_rc=0 + tricky_json="$(env -i PATH="" HOME="$(make_home "tricky-${tricky_sh##*/}")" CLAUDE_PLUGIN_ROOT="$tricky_root" \ + "$tricky_sh" "$tricky_root/hooks/session-start")" || tricky_rc=$? + if [[ "$tricky_rc" -eq 0 ]] && node -e ' const fs = require("fs"); const raw = fs.readFileSync(process.argv[1], "utf8").replace(/\n+$/, ""); const parsed = JSON.parse(process.argv[2]); @@ -370,13 +377,11 @@ if [[ -n "$posix_sh" ]]; then process.exit(1); } ' "$tricky_root/skills/using-superpowers/SKILL.md" "$tricky_json"; then - pass "POSIX sh (${posix_sh##*/}) round-trips backslashes, quotes, tabs, globs, and CRLF in JSON" + pass "${tricky_sh##*/} round-trips backslashes, quotes, tabs, globs, and CRLF in JSON" else - fail "POSIX sh (${posix_sh##*/}) round-trips backslashes, quotes, tabs, globs, and CRLF in JSON" + fail "${tricky_sh##*/} round-trips backslashes, quotes, tabs, globs, and CRLF in JSON (rc=$tricky_rc)" fi -else - echo " [SKIP] run-hook.cmd/session-start sh-only tests: no non-bash/non-zsh POSIX sh found" -fi +done # Run by bare filename from inside hooks/ ($0 has no slash), the plugin root # must still resolve. |
