Skip to content

Commit 27e4c99

Browse files
authored
Update GetPackageInfo aggregation to avoid race conditions with single file output (Azure#50539)
1 parent af2287d commit 27e4c99

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

eng/Directory.Build.Common.targets

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@
364364
<Target Name="RunApiCompat" DependsOnTargets="ValidateApiCompatForSrc" Condition="'$(ApiCompatVersion)' != ''">
365365
</Target>
366366

367-
<Target Name="GetPackageInfo">
367+
<Target Name="GetPackageInfo" Returns="@(PackageInfoLine)">
368368
<PropertyGroup>
369369
<PackageIsNewSdk>false</PackageIsNewSdk>
370370
<PackageIsNewSdk Condition="'$(IsClientLibrary)' == 'true' or '$(IsFunctionsLibrary)' == 'true'">true</PackageIsNewSdk>
@@ -380,9 +380,14 @@
380380

381381
<!-- Parse out the service directory based on the relative path to the sdk folder -->
382382
<ServiceDirectory><![CDATA[$([System.Text.RegularExpressions.Regex]::Replace($(PackageRootDirectory), '^.*[\\/]+sdk[\\/]+([^\\/]+).*$', '$1'))]]></ServiceDirectory>
383+
384+
<!-- Format package info line for output -->
385+
<PackageInfoText Condition="'$(IsShippingLibrary)' == 'true'">'$(PackageRootDirectory)' '$(ServiceDirectory)' '$(PackageId)' '$(VersionForProperties)' '$(PackageSdkType)' '$(PackageIsNewSdk)' '$(BaseOutputPath)'</PackageInfoText>
383386
</PropertyGroup>
384387

385-
<Message Condition="'$(IsShippingLibrary)' == 'true'" Text="'$(PackageRootDirectory)' '$(ServiceDirectory)' '$(PackageId)' '$(VersionForProperties)' '$(PackageSdkType)' '$(PackageIsNewSdk)' '$(BaseOutputPath)'" Importance="High" />
388+
<ItemGroup Condition="'$(IsShippingLibrary)' == 'true'">
389+
<PackageInfoLine Include="$(PackageInfoText)" />
390+
</ItemGroup>
386391
</Target>
387392

388393
<Target Name="GetCodeGenProjects" Returns="@(ProjectsToInclude)">

eng/scripts/Language-Settings.ps1

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,35 @@ function Get-AllPackageInfoFromRepo($serviceDirectory)
1616
# Save-Package-Properties.ps1
1717
$shouldAddDevVersion = Get-Variable -Name 'addDevVersion' -ValueOnly -ErrorAction 'Ignore'
1818
$ServiceProj = Join-Path -Path $EngDir -ChildPath "service.proj"
19-
Write-Host "dotnet msbuild /nologo /t:GetPackageInfo ""$ServiceProj"" /p:EnableCentralPackageVersions=False /p:ServiceDirectory=$serviceDirectory /p:AddDevVersion=$shouldAddDevVersion -tl:off"
19+
$outputFilePath = Join-Path ([System.IO.Path]::GetTempPath()) "package-info-$([System.Guid]::NewGuid()).txt"
20+
21+
Write-Host "dotnet msbuild /nologo /t:GetPackageInfo ""$ServiceProj"" /p:ServiceDirectory=$serviceDirectory /p:AddDevVersion=$shouldAddDevVersion /p:OutputProjectInfoListFilePath=""$outputFilePath"" -tl:off"
2022

21-
$msbuildOutput = dotnet msbuild `
23+
dotnet msbuild `
2224
/nologo `
2325
/t:GetPackageInfo `
2426
"$ServiceProj" `
25-
/p:EnableCentralPackageVersions=False `
2627
/p:ServiceDirectory=$serviceDirectory `
2728
/p:AddDevVersion=$shouldAddDevVersion `
28-
-tl:off
29+
/p:OutputProjectInfoListFilePath="$outputFilePath" `
30+
-tl:off | Out-Host
31+
32+
# Check if msbuild succeeded
33+
if ($LASTEXITCODE -ne 0) {
34+
# Clean up temp file before failing
35+
if (Test-Path $outputFilePath) {
36+
$null = Remove-Item $outputFilePath -Force -ErrorAction SilentlyContinue
37+
}
38+
throw "MSBuild failed with exit code $LASTEXITCODE"
39+
}
40+
41+
$packageInfoLines = @()
42+
if (Test-Path $outputFilePath) {
43+
$packageInfoLines = Get-Content $outputFilePath | Where-Object { $_ -and $_.Trim() }
44+
$null = Remove-Item $outputFilePath -Force -ErrorAction SilentlyContinue
45+
}
2946

30-
foreach ($projectOutput in $msbuildOutput)
47+
foreach ($projectOutput in $packageInfoLines)
3148
{
3249
if (!$projectOutput) {
3350
Write-Verbose "Get-AllPackageInfoFromRepo::projectOutput was null or empty, skipping"

eng/service.proj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,20 @@
133133
Targets="GetPackageInfo"
134134
BuildInParallel="$(BuildInParallel)"
135135
SkipNonexistentProjects="false"
136-
SkipNonexistentTargets="true" />
136+
SkipNonexistentTargets="true">
137+
<Output ItemName="PackageInfoLines" TaskParameter="TargetOutputs" />
138+
</MSBuild>
139+
140+
<!-- Deduplicate package info lines -->
141+
<RemoveDuplicates Inputs="@(PackageInfoLines)">
142+
<Output TaskParameter="Filtered" ItemName="PackageInfoLinesFiltered"/>
143+
</RemoveDuplicates>
144+
145+
<!-- Write aggregated package info to file if OutputProjectInfoListFilePath is specified -->
146+
<WriteLinesToFile Condition="'$(OutputProjectInfoListFilePath)' != ''"
147+
File="$(OutputProjectInfoListFilePath)"
148+
Lines="@(PackageInfoLinesFiltered)"
149+
Overwrite="true" />
137150
</Target>
138151

139152
<Target Name="GetCodeGenProjects">

0 commit comments

Comments
 (0)