Skip to content

Commit eb560f0

Browse files
committed
Capture stderr separately from stdout
Redirect stderr to a temp file instead of merging with stdout via 2>&1. Prevents az/gh CLI warnings from corrupting JSON output before ConvertFrom-Json parsing. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 856c1a7 commit eb560f0

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

tracer/tools/Get-AzureDevOpsBuildAnalysis.ps1

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,26 @@ function Invoke-AzDevOpsApi {
9999
$cmdDisplay = "az $($azArgs -join ' ')"
100100
Write-Verbose "Executing: $cmdDisplay"
101101

102-
$output = & az @azArgs 2>&1
103-
104-
if ($LASTEXITCODE -ne 0) {
105-
$errorMsg = @"
102+
# Capture stderr separately so az CLI warnings don't corrupt the JSON output
103+
$stderrFile = [System.IO.Path]::GetTempFileName()
104+
try {
105+
$output = & az @azArgs 2>$stderrFile
106+
107+
if ($LASTEXITCODE -ne 0) {
108+
$stderr = Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue
109+
$errorMsg = @"
106110
Azure DevOps API call failed
107111
Command: $cmdDisplay
108112
Area: $Area
109113
Resource: $Resource
110114
Exit Code: $LASTEXITCODE
111-
Error: $output
115+
Error: $stderr
112116
"@
113-
throw $errorMsg
117+
throw $errorMsg
118+
}
119+
}
120+
finally {
121+
Remove-Item -Path $stderrFile -Force -ErrorAction SilentlyContinue
114122
}
115123

116124
$json = $output | ConvertFrom-Json
@@ -128,9 +136,16 @@ function Get-BuildIdFromPR {
128136

129137
Write-Verbose "Resolving build ID from PR #$PRNumber..."
130138

131-
$checks = & gh pr checks $PRNumber --json name,link 2>&1
132-
if ($LASTEXITCODE -ne 0) {
133-
throw "Failed to get PR checks: $checks"
139+
$stderrFile = [System.IO.Path]::GetTempFileName()
140+
try {
141+
$checks = & gh pr checks $PRNumber --json name,link 2>$stderrFile
142+
if ($LASTEXITCODE -ne 0) {
143+
$stderr = Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue
144+
throw "Failed to get PR checks: $stderr"
145+
}
146+
}
147+
finally {
148+
Remove-Item -Path $stderrFile -Force -ErrorAction SilentlyContinue
134149
}
135150

136151
$checksJson = $checks | ConvertFrom-Json
@@ -322,9 +337,15 @@ try {
322337

323338
if ($PSCmdlet.ParameterSetName -eq 'ByCurrentBranch') {
324339
Write-Verbose "No arguments provided. Detecting PR for current branch..."
325-
$prOutput = & gh pr view --json number -q .number 2>&1
326-
if ($LASTEXITCODE -ne 0) {
327-
throw "No PR found for current branch. Specify -PullRequest or -BuildId."
340+
$stderrFile = [System.IO.Path]::GetTempFileName()
341+
try {
342+
$prOutput = & gh pr view --json number -q .number 2>$stderrFile
343+
if ($LASTEXITCODE -ne 0) {
344+
throw "No PR found for current branch. Specify -PullRequest or -BuildId."
345+
}
346+
}
347+
finally {
348+
Remove-Item -Path $stderrFile -Force -ErrorAction SilentlyContinue
328349
}
329350
$PullRequest = [int]$prOutput
330351
Write-Verbose "Detected PR #$PullRequest for current branch."

0 commit comments

Comments
 (0)