Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(shell): treat PowerShell directive paths literally (#4680)

Wu Shuwenf7735f2ec804
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap
index 15babfc22..136894456 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_powershell.snap
@@ -44,8 +44,8 @@ if ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {
# Process the directive and clean it up even if cd throws.
try {
# cd file holds a raw path (no shell escaping needed)
- if ((Test-Path $cdFile) -and (Get-Item $cdFile).Length -gt 0) {
- $target = Get-Content -Path $cdFile -Raw
+ if ((Test-Path -LiteralPath $cdFile) -and (Get-Item -LiteralPath $cdFile).Length -gt 0) {
+ $target = Get-Content -LiteralPath $cdFile -Raw
if ($target) {
Set-Location -LiteralPath $target
if ($exitCode -eq 0) {
@@ -56,7 +56,7 @@ if ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {
}
finally {
- Remove-Item $cdFile -ErrorAction SilentlyContinue
+ Remove-Item -LiteralPath $cdFile -ErrorAction SilentlyContinue
}
# Propagate the exit code so scripts/CI can read $LASTEXITCODE.
diff --git a/templates/powershell.ps1 b/templates/powershell.ps1
index 459a1257f..8cf256e06 100644
--- a/templates/powershell.ps1
+++ b/templates/powershell.ps1
@@ -40,8 +40,8 @@ if ((Get-Command {{ cmd }} -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN
# Process the directive and clean it up even if cd throws.
try {
# cd file holds a raw path (no shell escaping needed)
- if ((Test-Path $cdFile) -and (Get-Item $cdFile).Length -gt 0) {
- $target = Get-Content -Path $cdFile -Raw
+ if ((Test-Path -LiteralPath $cdFile) -and (Get-Item -LiteralPath $cdFile).Length -gt 0) {
+ $target = Get-Content -LiteralPath $cdFile -Raw
if ($target) {
Set-Location -LiteralPath $target
if ($exitCode -eq 0) {
@@ -52,7 +52,7 @@ if ((Get-Command {{ cmd }} -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN
}
finally {
- Remove-Item $cdFile -ErrorAction SilentlyContinue
+ Remove-Item -LiteralPath $cdFile -ErrorAction SilentlyContinue
}
# Propagate the exit code so scripts/CI can read $LASTEXITCODE.
diff --git a/tests/integration_tests/shell_powershell.rs b/tests/integration_tests/shell_powershell.rs
index a5189a9aa..78858e525 100644
--- a/tests/integration_tests/shell_powershell.rs
+++ b/tests/integration_tests/shell_powershell.rs
@@ -181,6 +181,61 @@ wt list
);
}
+/// A temp directory containing brackets must not turn directive-file operations
+/// into wildcard lookups, which silently skips the directory change and cleanup.
+#[test]
+fn test_powershell_wrapper_handles_literal_temp_paths() {
+ let temp_dir = worktrunk::testing::test_tempdir();
+ let temp_path = temp_dir.path().join("temp[1]");
+ let target_path = temp_dir.path().join("target");
+ std::fs::create_dir(&temp_path).unwrap();
+ std::fs::create_dir(&target_path).unwrap();
+ let target_path = dunce::canonicalize(target_path).unwrap();
+
+ let mock_bin = temp_dir.path().join("mock-wt.ps1");
+ std::fs::write(
+ &mock_bin,
+ r#"
+if ($env:COMPLETE) { exit 0 }
+[IO.File]::WriteAllText($env:WORKTRUNK_DIRECTIVE_CD_FILE, $env:WORKTRUNK_TEST_TARGET)
+exit 0
+"#,
+ )
+ .unwrap();
+
+ let wrapper = ShellInit::with_prefix(Shell::PowerShell, "wt".to_string())
+ .generate()
+ .unwrap();
+ let mock_bin_escaped = mock_bin.display().to_string().replace('\'', "''");
+ let script = format!(
+ r#"
+$ErrorActionPreference = 'Stop'
+$env:WORKTRUNK_BIN = '{mock_bin_escaped}'
+{wrapper}
+wt switch feature
+Write-Output (Get-Location).Path
+exit $LASTEXITCODE
+"#
+ );
+
+ let output = Command::new("pwsh")
+ .args(["-NoProfile", "-NonInteractive", "-Command", &script])
+ .env("TMP", &temp_path)
+ .env("TEMP", &temp_path)
+ .env("TMPDIR", &temp_path)
+ .env("WORKTRUNK_TEST_TARGET", &target_path)
+ .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}",
+ );
+ assert_eq!(stdout.trim(), target_path.to_str().unwrap());
+ assert_eq!(std::fs::read_dir(&temp_path).unwrap().count(), 0);
+}
+
/// The installed `pwsh` decides where install writes on Unix: with
/// `XDG_CONFIG_HOME` pointing away from `~/.config`, the profile lands where
/// PowerShell loads it.