Skip to content

fix: record the real exit code from the PowerShell prompt hook - #324

Merged
guan4tou2 merged 1 commit into
mainfrom
fix/powershell-hook-exit-code
Oct 13, 2026
Merged

guan4tou2 merged 1 commit into
mainfrom
fix/powershell-hook-exit-code

Conversation

@guan4tou2

Copy link
Copy Markdown
Owner

What

hooks/shell-hook.ps1: the prompt function read $LASTEXITCODE into a variable before reading $?. $? describes only the previous statement, so by then it described that assignment and was always $true. Every failing PowerShell command was recorded as exit_code: 0.

The fix is to read $? first (the two lines swap, plus a comment saying why).

How it was found

Not by a test. I ran a packaged Windows build against an isolated profile and drove the hook in a real PowerShell: cmd /c exit 3 was recorded with exit_code: 0. Confirmed in isolation on both Windows PowerShell 5.1 and pwsh 7 — the shipped order gives success=True, the swapped order gives success=False.

Test

test/powershell-hook-exit-code.test.ts starts an interactive PowerShell (pwsh and Windows PowerShell, whichever exist; nothing is declared if neither does), dot-sources the hook with a temp USERPROFILE, feeds commands on stdin, and reads the events the hook spools to ~/.redlog/pending (no RedLog is running, so there is no api-port). It checks:

  • a failing native command is recorded with its own code, a success as 0
  • a failing cmdlet is recorded non-zero
  • a native failure is not carried into the next successful command

All three fail on the old order (verified by running them before the fix), and all six (3 × 2 shells) pass after.

Checks

typecheck, verify:specs, verify:architecture, verify:i18n, npm test (343 files / 3503 tests passed, 33 skipped) — all green locally on Windows.

Not covered

  • CI is ubuntu-only and I have not seen this test run there. If pwsh is not on the runner the suite declares nothing rather than failing, so on CI it may be a no-op; the Windows run above is what exercised it.
  • Behaviour inside the built-in terminal's own PowerShell is the same hook; I did not add a separate check for it.

Refs #226

🤖 Generated with Claude Code

The prompt function read `$LASTEXITCODE` into a variable before reading `$?`.
`$?` describes only the previous statement, so by then it described that
assignment and was always true: every failing command was recorded as
exit_code 0. `cmd /c exit 3` and a missing path both came back 0.

Found by running a packaged Windows build; no test ran the hook. The new test
drives the hook in a real interactive PowerShell (pwsh and Windows PowerShell
5.1, whichever exist), reads the events it spools, and checks a failing native
command, a failing cmdlet, and that a failure is not carried into the next
successful command. All three fail on the old order.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8e58e154-f209-48d9-9a01-e3a3b8360a30
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant