Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell): PowerShell wrapper swallows -D flag as -Debug (#1057)
Maximilian Roos516f90bac41e
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snapindex 36fb496f0..cfb102059 100644--- a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap+++ b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap@@ -13,12 +13,14 @@ expression: outputif ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {# wt wrapper function - uses temp file for directives+ #+ # IMPORTANT: This function must remain a "simple function" (no [CmdletBinding()] or+ # [Parameter()] attributes). Advanced functions add common parameters like -Debug,+ # -Verbose, -ErrorAction, etc. that intercept short flags: e.g., -D is consumed as+ # -Debug, -V as -Verbose, instead of being passed through to wt.exe. Using $args+ # (automatic variable for simple functions) ensures all arguments reach the binary+ # unchanged.function wt {- param(- [Parameter(ValueFromRemainingArguments = $true)]- [string[]]$Arguments- )-# Use WORKTRUNK_BIN if set (for testing dev builds), otherwise find via Get-Command# Select-Object -First 1 handles case where multiple binaries match (e.g., wt.exe from Windows Terminal)if ($env:WORKTRUNK_BIN) {@@ -34,7 +36,7 @@ if ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {# WORKTRUNK_SHELL tells the binary to use PowerShell-compatible escaping$env:WORKTRUNK_DIRECTIVE_FILE = $directiveFile$env:WORKTRUNK_SHELL = "powershell"- & $wtBin @Arguments+ & $wtBin @args$exitCode = $LASTEXITCODE}finally {diff --git a/templates/powershell.ps1 b/templates/powershell.ps1index 891db7640..13b422f94 100644--- a/templates/powershell.ps1+++ b/templates/powershell.ps1@@ -9,12 +9,14 @@if ((Get-Command {{ cmd }} -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {# wt wrapper function - uses temp file for directives+ #+ # IMPORTANT: This function must remain a "simple function" (no [CmdletBinding()] or+ # [Parameter()] attributes). Advanced functions add common parameters like -Debug,+ # -Verbose, -ErrorAction, etc. that intercept short flags: e.g., -D is consumed as+ # -Debug, -V as -Verbose, instead of being passed through to wt.exe. Using $args+ # (automatic variable for simple functions) ensures all arguments reach the binary+ # unchanged.function {{ cmd }} {- param(- [Parameter(ValueFromRemainingArguments = $true)]- [string[]]$Arguments- )-# Use WORKTRUNK_BIN if set (for testing dev builds), otherwise find via Get-Command# Select-Object -First 1 handles case where multiple binaries match (e.g., wt.exe from Windows Terminal)if ($env:WORKTRUNK_BIN) {@@ -30,7 +32,7 @@ if ((Get-Command {{ cmd }} -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN# WORKTRUNK_SHELL tells the binary to use PowerShell-compatible escaping$env:WORKTRUNK_DIRECTIVE_FILE = $directiveFile$env:WORKTRUNK_SHELL = "powershell"- & $wtBin @Arguments+ & $wtBin @args$exitCode = $LASTEXITCODE}finally {diff --git a/tests/integration_tests/shell_powershell.rs b/tests/integration_tests/shell_powershell.rsindex db19db4ef..c0db15b6f 100644--- a/tests/integration_tests/shell_powershell.rs+++ b/tests/integration_tests/shell_powershell.rs@@ -5,10 +5,9 @@#![cfg(feature = "shell-integration-tests")]-use std::path::Path;use std::process::Command;-use worktrunk::shell::Shell;+use worktrunk::shell::{Shell, ShellInit};/// Test that the PowerShell config_line() actually works when evaluated.///@@ -18,7 +17,7 @@ use worktrunk::shell::Shell;#[test]fn test_powershell_config_line_evaluates_correctly() {// Use CARGO_BIN_EXE_wt which Cargo sets to the wt binary path during tests- let wt_bin = Path::new(env!("CARGO_BIN_EXE_wt"));+ let wt_bin = std::path::Path::new(env!("CARGO_BIN_EXE_wt"));let bin_dir = wt_bin.parent().expect("Failed to get binary directory");// Build a script that:@@ -70,3 +69,54 @@ if ($cmd -and $cmd.CommandType -eq 'Function') {{stderr);}++/// Regression test: PowerShell wrapper must not consume short flags like -D.+///+/// When the wrapper function uses `[Parameter(ValueFromRemainingArguments)]`, PowerShell+/// promotes it to an "advanced function" which adds common parameters (-Debug, -Verbose,+/// etc.). The `-D` flag is then consumed as `-Debug` instead of being passed to the binary.+/// The fix uses `$args` (simple function automatic variable) for transparent passthrough.+#[test]+fn test_powershell_wrapper_passes_short_flags_through() {+ // Create a .ps1 mock that prints each argument on its own line.+ // Using .ps1 (not a shell script) so this works on Windows too.+ let temp_dir = tempfile::tempdir().unwrap();+ let mock_bin = temp_dir.path().join("mock-wt.ps1");+ std::fs::write(&mock_bin, "foreach ($a in $args) { Write-Output $a }\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 remove -D test --force+"#+ );++ 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}",+ );++ // Each argument should appear as a separate line in the mock's output.+ // If -D were consumed as -Debug (advanced function), it would be missing.+ let lines: Vec<&str> = stdout.lines().map(|l| l.trim()).collect();+ for expected in ["remove", "-D", "test", "--force"] {+ assert!(+ lines.contains(&expected),+ "Expected argument {expected:?} to be passed through to binary.\n\+ Got lines: {lines:?}\nstdout: {stdout}\nstderr: {stderr}",+ );+ }+}