Skip to content

Commit 82e1a34

Browse files
authored
fix(powershell): invalid working directory behavior (#589)
- Add a proper error message for this case. - Exit cleanup without throwing another exception.
1 parent f89fa5d commit 82e1a34

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "PowerShell: Invalid working directory produces cryptic error message and exits cleanup early."
4+
}

src/tasks/AWSPowerShellModuleScript/RunAWSPowerShellModuleScript.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,20 @@ try {
198198
'CONTINUE' { }
199199
'SILENTLYCONTINUE' { }
200200
default {
201-
Write-Error (Get-VstsLocString -Key 'PS_InvalidErrorActionPreference' -ArgumentList $input_errorActionPreference)
201+
Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidErrorActionPreference' -ArgumentList $input_errorActionPreference)
202202
}
203203
}
204204

205205
$input_failOnStderr = Get-VstsInput -Name 'failOnStderr' -AsBool
206206
$input_ignoreLASTEXITCODE = Get-VstsInput -Name 'ignoreLASTEXITCODE' -AsBool
207207
$input_workingDirectory = Get-VstsInput -Name 'workingDirectory' -Require
208-
Assert-VstsPath -LiteralPath $input_workingDirectory -PathType 'Container'
208+
try {
209+
Assert-VstsPath -LiteralPath $input_workingDirectory -PathType 'Container'
210+
}
211+
catch {
212+
Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidWorkingDirectory' -ArgumentList $input_workingDirectory)
213+
throw $_
214+
}
209215

210216
$scriptType = Get-VstsInput -Name 'scriptType' -Require
211217
$input_arguments = Get-VstsInput -Name 'arguments'
@@ -216,11 +222,11 @@ try {
216222
Assert-VstsPath -LiteralPath $input_filePath -PathType Leaf
217223
}
218224
catch {
219-
Write-Error (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath)
225+
Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath)
220226
}
221227

222228
if (!$input_filePath.ToUpperInvariant().EndsWith('.PS1')) {
223-
Write-Error (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath)
229+
Write-VstsTaskError (Get-VstsLocString -Key 'PS_InvalidFilePath' -ArgumentList $input_filePath)
224230
}
225231
}
226232
else {
@@ -365,8 +371,12 @@ try {
365371
}
366372
finally {
367373
if ($scriptType -And "$scriptType".ToUpperInvariant() -eq "INLINE") {
368-
Write-Host "Cleaning up temporary script file $input_filePath"
369-
Remove-Item -Path $input_filePath -Force
374+
if ($input_filePath -And (Test-Path -Path $input_filePath)) {
375+
Write-Host "Cleaning up temporary script file $input_filePath"
376+
Remove-Item -Path $input_filePath -Force
377+
} else {
378+
Write-Host "Temporary script file does not exist, nothing to clean: $input_filePath"
379+
}
370380
}
371381

372382
Trace-VstsLeavingInvocation $MyInvocation

src/tasks/AWSPowerShellModuleScript/task.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"PS_FormattedCommand": "Formatted command: {0}",
142142
"PS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '{0}'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'.",
143143
"PS_InvalidFilePath": "Invalid file path '{0}'. A path to a .ps1 file is required.",
144+
"PS_InvalidWorkingDirectory": "Invalid working directory path '{0}'.",
144145
"PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell.",
145146
"ConfiguringProxy": "Configuring proxy for AWSPowerShell module. Host {0}, port {1}",
146147
"ProxyConfigError": "Failed to configure proxy, error {0}",

0 commit comments

Comments
 (0)