diff --git a/psakeFile.ps1 b/psakeFile.ps1 index 593b7eb..e0329a1 100644 --- a/psakeFile.ps1 +++ b/psakeFile.ps1 @@ -16,7 +16,33 @@ task Init { task Test -Depends Init, Analyze, Pester -description 'Run test suite' task Analyze -depends Build { - $analysis = Invoke-ScriptAnalyzer -Path $settings.ModuleOutDir -Recurse -Verbose:$false -Settings ([IO.Path]::Combine($env:BHModulePath, 'ScriptAnalyzerSettings.psd1')) + # PSScriptAnalyzer crashes intermittently on an internal race of its own, unrelated to the + # code being analyzed, and a re-run always succeeds. See psake/PowerShellBuild#136. + $analyzerParameters = @{ + Path = $settings.ModuleOutDir + Recurse = $true + Verbose = $false + Settings = [IO.Path]::Combine($env:BHModulePath, 'ScriptAnalyzerSettings.psd1') + ErrorAction = 'Stop' + } + $maximumAttempt = 3 + for ($attempt = 1; $attempt -le $maximumAttempt; $attempt++) { + try { + $analysis = Invoke-ScriptAnalyzer @analyzerParameters + break + } catch { + $errorRecord = $_ + if ($attempt -eq $maximumAttempt) { + throw $errorRecord + } + Write-Warning ( + "PSScriptAnalyzer failed on attempt $attempt of $maximumAttempt and will be retried. " + + "[$($errorRecord.FullyQualifiedErrorId)] $($errorRecord.Exception.Message)" + ) + Start-Sleep -Seconds 2 + } + } + $errors = $analysis | Where-Object {$_.Severity -eq 'Error'} $warnings = $analysis | Where-Object {$_.Severity -eq 'Warning'} if (@($errors).Count -gt 0) {