diff options
| -rw-r--r-- | docs/windows/polyglot-hooks.md | 10 | ||||
| -rw-r--r-- | hooks/hooks.json | 1 | ||||
| -rwxr-xr-x | tests/hooks/test-run-hook-cmd-windows.sh | 18 | ||||
| -rwxr-xr-x | tests/hooks/test-session-start.sh | 14 |
4 files changed, 43 insertions, 0 deletions
diff --git a/docs/windows/polyglot-hooks.md b/docs/windows/polyglot-hooks.md index 3da01ae..d753d90 100644 --- a/docs/windows/polyglot-hooks.md +++ b/docs/windows/polyglot-hooks.md @@ -51,6 +51,7 @@ hooks/ { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start", + "powershell": "& \"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start", "shell": "bash", "async": false } @@ -63,6 +64,15 @@ hooks/ The path is quoted because `${CLAUDE_PLUGIN_ROOT}` may contain spaces. +### The `powershell` key + +GitHub Copilot (the CLI and VS Code) on Windows runs hook commands through +PowerShell regardless of the `shell` key (#2189), where a leading quoted path +parses as a string expression and the trailing bareword is an error +("Unexpected token 'session-start'"). Copilot uses the `powershell` key +instead of `command` when it is present, and it gives the same dispatch with +the `&` call operator. Claude Code ignores the key. + ## How `run-hook.cmd` Works at a High Level `run-hook.cmd` is a polyglot script: Windows treats the first block as batch diff --git a/hooks/hooks.json b/hooks/hooks.json index 6797513..8b33bbe 100644 --- a/hooks/hooks.json +++ b/hooks/hooks.json @@ -7,6 +7,7 @@ { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start", + "powershell": "& \"${CLAUDE_PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start", "shell": "bash", "async": false } diff --git a/tests/hooks/test-run-hook-cmd-windows.sh b/tests/hooks/test-run-hook-cmd-windows.sh index b58c6d1..63e2db8 100755 --- a/tests/hooks/test-run-hook-cmd-windows.sh +++ b/tests/hooks/test-run-hook-cmd-windows.sh @@ -207,6 +207,24 @@ else fi fi +# --- the hooks.json powershell form dispatches under PowerShell --- +# Copilot (CLI and VS Code) on Windows runs hook commands through PowerShell, +# where the quoted `command` form is a ParserError (#2189). It substitutes +# ${CLAUDE_PLUGIN_ROOT} into the string first; the plugin root here has a +# space, as real install paths can. +ps_plugin="$TEST_ROOT/plugin root" +make_plugin "$ps_plugin" real +cp "$ps_plugin/hooks/probe-ok" "$ps_plugin/hooks/session-start" +ps_command="$(sed -n 's/^ *"powershell": "\(.*\)",\{0,1\}$/\1/p' "$REPO_ROOT/hooks/hooks.json" | sed 's/\\"/"/g')" +ps_command="${ps_command//\$\{CLAUDE_PLUGIN_ROOT\}/$(cygpath -w "$ps_plugin")}" +RC=0 +OUT="$(cd "$TEST_ROOT" && powershell -NoProfile -NonInteractive -Command "$ps_command" 2>&1)" || RC=$? +if [[ -n "$ps_command" && "$RC" -eq 0 && "$OUT" == *probe-ran* ]]; then + pass "hooks.json powershell command runs session-start under PowerShell" +else + fail "hooks.json powershell command runs session-start under PowerShell (command=$ps_command, rc=$RC, out=$OUT)" +fi + # --- no usable bash: exit 0 quietly, without invoking the WSL launcher --- run_hook "$sandbox" "$TEST_ROOT" probe-ok \ LOCALAPPDATA="$NO_LA" PATH="$SYS32_PATH" diff --git a/tests/hooks/test-session-start.sh b/tests/hooks/test-session-start.sh index 6ddf011..2a27c78 100755 --- a/tests/hooks/test-session-start.sh +++ b/tests/hooks/test-session-start.sh @@ -147,6 +147,12 @@ echo "SessionStart hook output tests" # Windows dispatches via Git Bash (or fails with an actionable error) instead # of PowerShell/cmd.exe, whose parsers break on the quoted command string # (PowerShell ParserError; cmd.exe quote-stripping on paths with metacharacters). +# +# VS Code Copilot ignores shell:"bash" and runs the `command` string through +# PowerShell regardless (#2189). The optional `powershell` key supplies a +# PowerShell-native form of the same dispatch: the `&` call operator makes the +# quoted plugin-root path parse as a command invocation instead of a string +# expression followed by the bareword `session-start`. if node -e ' const hooks = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")); const entry = hooks.hooks.SessionStart[0].hooks[0]; @@ -158,6 +164,14 @@ if (!/run-hook\.cmd" session-start$/.test(entry.command)) { console.error(`unexpected SessionStart command shape: ${entry.command}`); process.exit(1); } +if (typeof entry.powershell !== "string") { + console.error(`SessionStart hook missing powershell key for VS Code Copilot (#2189)`); + process.exit(1); +} +if (!/^& "/.test(entry.powershell) || !/run-hook\.cmd" session-start$/.test(entry.powershell)) { + console.error(`unexpected SessionStart powershell shape: ${entry.powershell}`); + process.exit(1); +} ' "$REPO_ROOT/hooks/hooks.json"; then pass "hooks.json registers SessionStart with shell:bash dispatch" else |
