Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell): stop PowerShell wrapper leaking a stray exit-code line to stdout (#3425)
Worktrunk Boteba35d039c32
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snapindex 78875998d..6abf074b1 100644--- a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap+++ b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap@@ -77,13 +77,21 @@ if ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {Remove-Item $execFile -ErrorAction SilentlyContinue}- # Propagate exit code so $? and $LASTEXITCODE are consistent for scripts/CI+ # Propagate the exit code so scripts/CI can read $LASTEXITCODE.+ # Do NOT `return $exitCode`: in a PowerShell function `return <value>`+ # writes the value to the output (success) stream, so it would append a+ # stray exit-code line (e.g. `0`) after the real command output — that+ # corrupts captures like `$out = wt list --format json`. Setting+ # $global:LASTEXITCODE is what propagates the failure; the Write-Error+ # below surfaces a visible error record. Note: because this is a simple+ # function, Write-Error does NOT set the caller's $?, so don't rely on+ # $? reflecting a wt failure.$global:LASTEXITCODE = $exitCodeif ($exitCode -ne 0) {- # Write error to set $? = $false without throwing+ # Surface a visible error record without throwing (does not set the+ # caller's $? from a simple function; $LASTEXITCODE carries the code)Write-Error "wt exited with code $exitCode" -ErrorAction SilentlyContinue}- return $exitCode}# Tab completion - generate clap's completer script and eval itdiff --git a/templates/powershell.ps1 b/templates/powershell.ps1index a3091d899..53f86d277 100644--- a/templates/powershell.ps1+++ b/templates/powershell.ps1@@ -73,13 +73,21 @@ if ((Get-Command {{ cmd }} -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BINRemove-Item $execFile -ErrorAction SilentlyContinue}- # Propagate exit code so $? and $LASTEXITCODE are consistent for scripts/CI+ # Propagate the exit code so scripts/CI can read $LASTEXITCODE.+ # Do NOT `return $exitCode`: in a PowerShell function `return <value>`+ # writes the value to the output (success) stream, so it would append a+ # stray exit-code line (e.g. `0`) after the real command output — that+ # corrupts captures like `$out = wt list --format json`. Setting+ # $global:LASTEXITCODE is what propagates the failure; the Write-Error+ # below surfaces a visible error record. Note: because this is a simple+ # function, Write-Error does NOT set the caller's $?, so don't rely on+ # $? reflecting a wt failure.$global:LASTEXITCODE = $exitCodeif ($exitCode -ne 0) {- # Write error to set $? = $false without throwing+ # Surface a visible error record without throwing (does not set the+ # caller's $? from a simple function; $LASTEXITCODE carries the code)Write-Error "wt exited with code $exitCode" -ErrorAction SilentlyContinue}- return $exitCode}# Tab completion - generate clap's completer script and eval itdiff --git a/tests/integration_tests/shell_powershell.rs b/tests/integration_tests/shell_powershell.rsindex c0db15b6f..3e291ed8d 100644--- a/tests/integration_tests/shell_powershell.rs+++ b/tests/integration_tests/shell_powershell.rs@@ -120,3 +120,64 @@ wt remove -D test --force);}}++/// Regression test: the wrapper must not emit a stray exit-code line to stdout.+///+/// The wrapper used to end with `return $exitCode`. In a PowerShell function,+/// `return <value>` writes the value to the output (success) stream, so after+/// the real `wt` output the function appended a bare exit-code line (e.g. `0`).+/// That corrupts any capture of a command's output, e.g. `$out = wt list+/// --format json`. Exit-code propagation is handled by `$global:LASTEXITCODE`+/// (the `Write-Error` only surfaces a visible error record — it does not set+/// the caller's `$?` from a simple function), so the `return` was pure stdout+/// pollution.+///+/// The mock must exit with a real code so `$LASTEXITCODE` is set — a `.ps1`+/// that only calls `Write-Output` leaves `$LASTEXITCODE` unset, and the old+/// `return $null` emitted nothing, hiding the bug.+#[test]+fn test_powershell_wrapper_no_stray_exit_code_on_stdout() {+ let temp_dir = tempfile::tempdir().unwrap();+ let mock_bin = temp_dir.path().join("mock-wt.ps1");+ // Emit a single distinctive line, then exit 0 like a real native binary.+ std::fs::write(&mock_bin, "Write-Output 'MOCK_OUTPUT_LINE'\nexit 0\n").unwrap();++ let init = ShellInit::with_prefix(Shell::PowerShell, "wt".to_string());+ let wrapper = init.generate().unwrap();++ let mock_bin_escaped = mock_bin.display().to_string().replace('\'', "''");+ let script = format!(+ r#"+$env:WORKTRUNK_BIN = '{mock_bin_escaped}'+{wrapper}+wt list+"#+ );++ let output = Command::new("pwsh")+ .args(["-NoProfile", "-NonInteractive", "-Command", &script])+ .output()+ .expect("Failed to run pwsh");++ let stdout = String::from_utf8_lossy(&output.stdout);+ let stderr = String::from_utf8_lossy(&output.stderr);++ assert!(+ output.status.success(),+ "pwsh command failed.\nstdout: {stdout}\nstderr: {stderr}",+ );++ // The mock's line is the only thing that should reach stdout. Before the+ // fix, a stray `0` (the exit code) followed it.+ let lines: Vec<&str> = stdout+ .lines()+ .map(|l| l.trim())+ .filter(|l| !l.is_empty())+ .collect();+ assert_eq!(+ lines,+ vec!["MOCK_OUTPUT_LINE"],+ "wrapper leaked extra stdout (likely a stray exit-code line from `return`).\n\+ stdout: {stdout}\nstderr: {stderr}",+ );+}