From f832545e1861587a82651ef3bdb4e8f2dcf46dc9 Mon Sep 17 00:00:00 2001 From: Maxim Hayes Date: Fri, 10 Jan 2025 11:42:24 -0500 Subject: [PATCH] fix(powershell): invalid working directory behavior - Add a proper error message for this case. - Exit cleanup without throwing another exception. --- ...-7b05e6b7-10c6-409b-8e40-a1be42b59832.json | 4 ++++ .../RunAWSPowerShellModuleScript.ps1 | 22 ++++++++++++++----- src/tasks/AWSPowerShellModuleScript/task.json | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 .changes/next-release/Bug Fix-7b05e6b7-10c6-409b-8e40-a1be42b59832.json diff --git a/.changes/next-release/Bug Fix-7b05e6b7-10c6-409b-8e40-a1be42b59832.json b/.changes/next-release/Bug Fix-7b05e6b7-10c6-409b-8e40-a1be42b59832.json new file mode 100644 index 00000000..c4e5ead8 --- /dev/null +++ b/.changes/next-release/Bug Fix-7b05e6b7-10c6-409b-8e40-a1be42b59832.json @@ -0,0 +1,4 @@ +{ + "type": "Bug Fix", + "description": "PowerShell: Invalid working directory produces cryptic error message and exits cleanup early." +} \ No newline at end of file diff --git a/src/tasks/AWSPowerShellModuleScript/RunAWSPowerShellModuleScript.ps1 b/src/tasks/AWSPowerShellModuleScript/RunAWSPowerShellModuleScript.ps1 index c3167aed..764df925 100644 --- a/src/tasks/AWSPowerShellModuleScript/RunAWSPowerShellModuleScript.ps1 +++ b/src/tasks/AWSPowerShellModuleScript/RunAWSPowerShellModuleScript.ps1 @@ -198,14 +198,20 @@ try { 'CONTINUE' { } 'SILENTLYCONTINUE' { } default { - Write-Error (Get-VstsLocString -Key 'PS_InvalidErrorActionPreference' -ArgumentList $input_errorActionPreference) + Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidErrorActionPreference' -ArgumentList $input_errorActionPreference) } } $input_failOnStderr = Get-VstsInput -Name 'failOnStderr' -AsBool $input_ignoreLASTEXITCODE = Get-VstsInput -Name 'ignoreLASTEXITCODE' -AsBool $input_workingDirectory = Get-VstsInput -Name 'workingDirectory' -Require - Assert-VstsPath -LiteralPath $input_workingDirectory -PathType 'Container' + try { + Assert-VstsPath -LiteralPath $input_workingDirectory -PathType 'Container' + } + catch { + Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidWorkingDirectory' -ArgumentList $input_workingDirectory) + throw $_ + } $scriptType = Get-VstsInput -Name 'scriptType' -Require $input_arguments = Get-VstsInput -Name 'arguments' @@ -216,11 +222,11 @@ try { Assert-VstsPath -LiteralPath $input_filePath -PathType Leaf } catch { - Write-Error (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath) + Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath) } if (!$input_filePath.ToUpperInvariant().EndsWith('.PS1')) { - Write-Error (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath) + Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath) } } else { @@ -365,8 +371,12 @@ try { } finally { if ($scriptType -And "$scriptType".ToUpperInvariant() -eq "INLINE") { - Write-Host "Cleaning up temporary script file $input_filePath" - Remove-Item -Path $input_filePath -Force + if ($input_filePath -And (Test-Path -Path $input_filePath)) { + Write-Host "Cleaning up temporary script file $input_filePath" + Remove-Item -Path $input_filePath -Force + } else { + Write-Host "Temporary script file does not exist, nothing to clean: $input_filePath" + } } Trace-VstsLeavingInvocation $MyInvocation diff --git a/src/tasks/AWSPowerShellModuleScript/task.json b/src/tasks/AWSPowerShellModuleScript/task.json index ad22e820..143e879f 100644 --- a/src/tasks/AWSPowerShellModuleScript/task.json +++ b/src/tasks/AWSPowerShellModuleScript/task.json @@ -141,6 +141,7 @@ "PS_FormattedCommand": "Formatted command: {0}", "PS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '{0}'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'.", "PS_InvalidFilePath": "Invalid file path '{0}'. A path to a .ps1 file is required.", + "PS_InvalidWorkingDirectory": "Invalid working directory path '{0}'.", "PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell.", "ConfiguringProxy": "Configuring proxy for AWSPowerShell module. Host {0}, port {1}", "ProxyConfigError": "Failed to configure proxy, error {0}",