From 5db48aed976a82a4ec891951e336a5c169e4d294 Mon Sep 17 00:00:00 2001 From: jantari Date: Tue, 26 Sep 2023 18:37:53 +0200 Subject: [PATCH] silence exception when testing some invalid paths in pwsh 7 (fix #96) --- private/Split-ExecutableAndArguments.ps1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/private/Split-ExecutableAndArguments.ps1 b/private/Split-ExecutableAndArguments.ps1 index 641918a..2ee7946 100644 --- a/private/Split-ExecutableAndArguments.ps1 +++ b/private/Split-ExecutableAndArguments.ps1 @@ -40,7 +40,17 @@ ) } - $testPathRelative = Join-Path -Path $WorkingDirectory -ChildPath $testPath + # In PowerShell 7.3 and 7.4 Join-Path throws an exception (not a PowerShell error, a .NET exception) + # when it tries to combine certain invalid paths (e.g. Join-Path -Path 'C:\' -ChildPath 'C:\ /x:'). + # These exceptions are not influenced by ErrorAction, so we need a try-catch. We still set ErrorAction + # to Stop because whenever Join-Path doesn't succeed for any reason, we can always skip and continue + # in the loop because there won't be a new path to test in testPathRelative. See issue #96. + try { + $testPathRelative = Join-Path -Path $WorkingDirectory -ChildPath $testPath -ErrorAction Stop + } + catch [System.IO.IOException] { + continue + } if ( [System.IO.File]::Exists($testPathRelative) ) { return @(