From 377647333d6d70c794d9e90d6391d1fa0468ec17 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Mon, 17 Apr 2017 11:17:37 -0700 Subject: [PATCH] Stop clearing LastExitCode The clearing of $LastExitCode in our Exec function is masking failures of windows commands. Cannot understand why this is the case but easy to demonstrate in practice. --- build/scripts/build-utils.ps1 | 26 +++++++++------ build/scripts/cibuild.ps1 | 2 +- build/scripts/test-build-correctness.ps1 | 40 +++++++++++------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/build/scripts/build-utils.ps1 b/build/scripts/build-utils.ps1 index 304de622cb5..c8c66eb70a2 100644 --- a/build/scripts/build-utils.ps1 +++ b/build/scripts/build-utils.ps1 @@ -11,29 +11,35 @@ $ErrorActionPreference="Stop" # fails. # # Original sample came from: http://jameskovacs.com/2010/02/25/the-exec-problem/ -function Exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd) { - # Clear LastExitCode before invoking the script so a windows command failure - # before doesn't carry over. Can happen if the expression is pure powershell - # and not wrapping a windows command. - $lastexitcode = 0 - - $output = & $cmd +function Exec([scriptblock]$cmd, [switch]$echo = $false) { + if ($echo) { + & $cmd + } + else { + $output = & $cmd + } # Need to check both of these cases for errors as they represent different items # - $?: did the powershell script block throw an error # - $lastexitcode: did a windows command executed by the script block end in error if ((-not $?) -or ($lastexitcode -ne 0)) { - Write-Host $output - throw $errorMessage + if (-not $echo) { + Write-Host $output + } + throw "Command failed to execute: $cmd" } } +function Exec-Echo([scriptblock]$cmd) { + Exec $cmd -echo:$true +} + # Handy function for executing Invoke-Expression and reliably throwing an # error if the expression, or the command it invoked, fails # # Original sample came from: http://jameskovacs.com/2010/02/25/the-exec-problem/ function Exec-Expression([string]$expr) { - Exec { Invoke-Expression $expr } + Exec { Invoke-Expression $expr } -echo:$true } # Ensure that NuGet is installed and return the path to the diff --git a/build/scripts/cibuild.ps1 b/build/scripts/cibuild.ps1 index 4b8599273b2..a7916ead713 100644 --- a/build/scripts/cibuild.ps1 +++ b/build/scripts/cibuild.ps1 @@ -87,7 +87,7 @@ try { Redirect-Temp if ($testBuildCorrectness) { - Exec { & ".\build\scripts\test-build-correctness.ps1" $repoDir $configDir } + Exec-Echo { & ".\build\scripts\test-build-correctness.ps1" -config $buildConfiguration } exit 0 } diff --git a/build/scripts/test-build-correctness.ps1 b/build/scripts/test-build-correctness.ps1 index e60cf3535b5..85b00ec69ce 100644 --- a/build/scripts/test-build-correctness.ps1 +++ b/build/scripts/test-build-correctness.ps1 @@ -11,51 +11,47 @@ [CmdletBinding(PositionalBinding=$false)] param( - [string]$sourcePath = $null, - [string]$binariesPath = $null + [string]$config = "", + [string]$msbuild = "" ) Set-StrictMode -version 2.0 $ErrorActionPreference="Stop" -function Get-PackagesPath { - $packagesPath = $env:NUGET_PACKAGES - if ($packagesPath -eq $null) { - $packagesPath = Join-Path $env:UserProfile ".nuget\packages\" - } - - return $packagesPath -} - -Push-Location $sourcePath try { + . (Join-Path $PSScriptRoot "build-utils.ps1") + Push-Location $repoDir # Need to parse out the current NuGet package version of Structured Logger - [xml]$deps = Get-Content (Join-Path $sourcePath "build\Targets\Dependencies.props") + [xml]$deps = Get-Content (Join-Path $repoDir "build\Targets\Dependencies.props") $structuredLoggerVersion = $deps.Project.PropertyGroup.MicrosoftBuildLoggingStructuredLoggerVersion - $packagesPath = Get-PackagesPath - $structuredLoggerPath = Join-Path $packagesPath "Microsoft.Build.Logging.StructuredLogger\$structuredLoggerVersion\lib\net46\StructuredLogger.dll" - $logPath = Join-Path $binariesPath "build.xml" + $structuredLoggerPath = Join-Path (Get-PackagesDir) "Microsoft.Build.Logging.StructuredLogger\$structuredLoggerVersion\lib\net46\StructuredLogger.dll" + $configDir = Join-Path $binariesDir $config + $logPath = Join-Path $configDir "build.xml" + + if ($msbuild -eq "") { + $msbuild = Ensure-MSBuild + } Write-Host "Building Roslyn.sln with logging support" - Exec { & msbuild /v:m /m /logger:StructuredLogger`,$structuredLoggerPath`;$logPath /nodeReuse:false /p:DeployExtension=false Roslyn.sln } + Exec-Echo { & $msbuild /v:m /m /logger:StructuredLogger`,$structuredLoggerPath`;$logPath /nodeReuse:false /p:DeployExtension=false Roslyn.sln } Write-Host "" # Verify the state of our various build artifacts Write-Host "Running BuildBoss" - $buildBossPath = Join-Path $binariesPath "Exes\BuildBoss\BuildBoss.exe" - Exec { & $buildBossPath Roslyn.sln Compilers.sln src\Samples\Samples.sln CrossPlatform.sln "build\Targets" $logPath } + $buildBossPath = Join-Path $configDir "Exes\BuildBoss\BuildBoss.exe" + Exec-Echo { & $buildBossPath Roslyn.sln Compilers.sln src\Samples\Samples.sln CrossPlatform.sln "build\Targets" $logPath } Write-Host "" # Verify the state of our project.jsons Write-Host "Running RepoUtil" - $repoUtilPath = Join-Path $binariesPath "Exes\RepoUtil\RepoUtil.exe" - Exec { & $repoUtilPath verify } + $repoUtilPath = Join-Path $configDir "Exes\RepoUtil\RepoUtil.exe" + Exec-Echo { & $repoUtilPath verify } Write-Host "" # Verify the state of our generated syntax files Write-Host "Checking generated compiler files" - Exec { & (Join-Path $PSScriptRoot "generate-compiler-code.ps1") -test } + Exec-Echo { & (Join-Path $PSScriptRoot "generate-compiler-code.ps1") -test } exit 0 } -- GitLab