From 9771809baa4ee10769574ba59e8307010625ed51 Mon Sep 17 00:00:00 2001
From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com>
Date: Fri, 4 Dec 2026 16:43:43 -0500
Subject: [PATCH 1/2] ci(http-client-csharp): parallelize generation, cache
NuGet packages, remove redundant builds
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
---
.../eng/pipeline/templates/ci-stages.yml | 13 ++
.../eng/scripts/Generate.ps1 | 67 ++++-----
.../eng/scripts/Generation.psm1 | 141 +++++++++++++++++-
.../eng/scripts/Get-Spector-Coverage.ps1 | 89 +++++------
.../eng/scripts/Test-Packages.ps1 | 9 +-
5 files changed, 226 insertions(+), 93 deletions(-)
diff --git a/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml b/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
index 5755b96656b..afe5ce0609e 100644
--- a/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
+++ b/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
@@ -39,6 +39,19 @@ stages:
DependsOn: ${{ parameters.DependsOn }}
LanguageShortName: "csharp"
AdditionalInitializeSteps:
+ - pwsh: |
+ Write-Host "##vso[task.setvariable variable=NUGET_PACKAGES]$(Pipeline.Workspace)/.nuget/packages"
+ Write-Host "##vso[task.setvariable variable=DOTNET_NOLOGO]true"
+ Write-Host "##vso[task.setvariable variable=DOTNET_CLI_TELEMETRY_OPTOUT]true"
+ Write-Host "##vso[task.setvariable variable=DOTNET_SKIP_FIRST_TIME_EXPERIENCE]true"
+ displayName: "Configure NuGet cache and .NET CLI"
+ - task: Cache@2
+ displayName: "Cache NuGet packages"
+ inputs:
+ key: '"nuget" | "$(Agent.OS)" | eng/nuget.config | packages/http-client-csharp/generator/Packages.Data.props | packages/http-client-csharp/generator/Directory.Build.targets | packages/http-client-csharp/**/*.csproj, !packages/http-client-csharp/node_modules/**'
+ restoreKeys: |
+ "nuget" | "$(Agent.OS)"
+ path: $(NUGET_PACKAGES)
- task: UseDotNet@2
inputs:
displayName: "Install .NET 8"
diff --git a/packages/http-client-csharp/eng/scripts/Generate.ps1 b/packages/http-client-csharp/eng/scripts/Generate.ps1
index 895bc96690f..5302756bd72 100644
--- a/packages/http-client-csharp/eng/scripts/Generate.ps1
+++ b/packages/http-client-csharp/eng/scripts/Generate.ps1
@@ -14,30 +14,17 @@ $totalStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')
$solutionDir = Join-Path $packageRoot 'generator'
+$generationJobs = @()
+$generateSampleTypeSpec = (-not $LaunchOnly) -and ($null -eq $filter -or $filter -eq "Sample-TypeSpec")
+$SampleTypeSpecTestProject = Join-Path $packageRoot 'generator' 'TestProjects' 'Local' "Sample-TypeSpec"
+
if (-not $LaunchOnly) {
Refresh-Build
- if ($null -eq $filter -or $filter -eq "Sample-TypeSpec") {
-
- Write-Host "Generating SampleTypeSpec" -ForegroundColor Cyan
- $testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'
-
- $SampleTypeSpecTestProject = Join-Path $testProjectsLocalDir "Sample-TypeSpec"
- $SampleTypeSpecTestProject = $SampleTypeSpecTestProject
-
- Invoke (Get-TspCommand "$SampleTypeSpecTestProject/Sample-TypeSpec.tsp" $SampleTypeSpecTestProject)
-
- # exit if the generation failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
- }
-
- Write-Host "Building SampleTypeSpec" -ForegroundColor Cyan
- Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Sample-TypeSpec/src/SampleTypeSpec.csproj"
-
- # exit if the generation failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
+ if ($generateSampleTypeSpec) {
+ $generationJobs += @{
+ Name = "Sample-TypeSpec"
+ Commands = @(Get-TspCommand "$SampleTypeSpecTestProject/Sample-TypeSpec.tsp" $SampleTypeSpecTestProject)
}
}
}
@@ -62,38 +49,40 @@ foreach ($specFile in Get-Sorted-Specs) {
New-Item -ItemType Directory -Path $generationDir | Out-Null
}
- Write-Host "Generating $subPath" -ForegroundColor Cyan
-
- if ($folders.Contains("versioning")) {
- Generate-Versioning (Split-Path $specFile) $generationDir -generateStub $stubbed
+ if ($folders.Contains("versioning") -or $folders.Contains("srv-driven")) {
$spectorLaunchProjects.Add($($folders -join "-") + "-v1", $("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v1")
$spectorLaunchProjects.Add($($folders -join "-") + "-v2", $("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v2")
- continue
}
-
- # srv-driven contains two separate specs, for two separate clients. We need to generate both.
- if ($folders.Contains("srv-driven")) {
- Generate-Srv-Driven (Split-Path $specFile) $generationDir -generateStub $stubbed
- $spectorLaunchProjects.Add($($folders -join "-") + "-v1", $("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v1")
- $spectorLaunchProjects.Add($($folders -join "-") + "-v2", $("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))") + "/v2")
- continue
+ else {
+ $spectorLaunchProjects.Add(($folders -join "-"), ("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))"))
}
- $spectorLaunchProjects.Add(($folders -join "-"), ("TestProjects/Spector/$($subPath.Replace([System.IO.Path]::DirectorySeparatorChar, '/'))"))
if ($LaunchOnly) {
continue
}
-
- Invoke (Get-TspCommand $specFile $generationDir $stubbed)
- # exit if the generation failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
+ $generationJobs += @{
+ Name = $subPath
+ Commands = @(Get-SpecGenerationCommands $specFile $subPath $generationDir -generateStub $stubbed)
}
# TODO need to build but depends on https://github.com/Azure/autorest.csharp/issues/4463
}
+if ($generationJobs.Count -gt 0) {
+ Invoke-ParallelGeneration $generationJobs
+}
+
+if ($generateSampleTypeSpec) {
+ Write-Host "Building SampleTypeSpec" -ForegroundColor Cyan
+ Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Sample-TypeSpec/src/SampleTypeSpec.csproj"
+
+ # exit if the build failed
+ if ($LASTEXITCODE -ne 0) {
+ exit $LASTEXITCODE
+ }
+}
+
# only write new launch settings if no filter was passed in
if ($null -eq $filter) {
Write-Host "Writing new launch settings" -ForegroundColor Cyan
diff --git a/packages/http-client-csharp/eng/scripts/Generation.psm1 b/packages/http-client-csharp/eng/scripts/Generation.psm1
index c259a5ffa26..d14b81eb98b 100644
--- a/packages/http-client-csharp/eng/scripts/Generation.psm1
+++ b/packages/http-client-csharp/eng/scripts/Generation.psm1
@@ -107,7 +107,7 @@ function Compare-Paths {
return $normalizedPath1.Contains($normalizedPath2)
}
-function Generate-Srv-Driven {
+function Get-SrvDrivenCommands {
param (
[string]$specFilePath,
[string]$outputDir,
@@ -134,8 +134,23 @@ function Generate-Srv-Driven {
$v1SpecFilePath = $(Join-Path $specFilePath "old.tsp")
$v2SpecFilePath = $(Join-Path $specFilePath "main.tsp")
- Invoke (Get-TspCommand $v1SpecFilePath $v1Dir -generateStub $generateStub -libraryNameOverride $v1LibraryNameOverride)
- Invoke (Get-TspCommand $v2SpecFilePath $v2Dir -generateStub $generateStub -libraryNameOverride $v2LibraryNameOverride)
+ return @(
+ (Get-TspCommand $v1SpecFilePath $v1Dir -generateStub $generateStub -libraryNameOverride $v1LibraryNameOverride)
+ (Get-TspCommand $v2SpecFilePath $v2Dir -generateStub $generateStub -libraryNameOverride $v2LibraryNameOverride)
+ )
+}
+
+function Generate-Srv-Driven {
+ param (
+ [string]$specFilePath,
+ [string]$outputDir,
+ [bool]$generateStub = $false,
+ [bool]$createOutputDirIfNotExist = $true
+ )
+
+ foreach ($command in Get-SrvDrivenCommands $specFilePath $outputDir -generateStub $generateStub -createOutputDirIfNotExist $createOutputDirIfNotExist) {
+ Invoke $command
+ }
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
@@ -143,7 +158,7 @@ function Generate-Srv-Driven {
}
}
-function Generate-Versioning {
+function Get-VersioningCommands {
param (
[string]$specFilePath,
[string]$outputDir,
@@ -167,8 +182,10 @@ function Generate-Versioning {
$v1LibraryNameOverride = $namespaceRoot + ".V1"
$v2LibraryNameOverride = $namespaceRoot + ".V2"
- Invoke (Get-TspCommand $specFilePath $v1Dir -generateStub $generateStub -apiVersion "v1" -libraryNameOverride $v1LibraryNameOverride)
- Invoke (Get-TspCommand $specFilePath $v2Dir -generateStub $generateStub -apiVersion "v2" -libraryNameOverride $v2LibraryNameOverride)
+ $commands = @(
+ (Get-TspCommand $specFilePath $v1Dir -generateStub $generateStub -apiVersion "v1" -libraryNameOverride $v1LibraryNameOverride)
+ (Get-TspCommand $specFilePath $v2Dir -generateStub $generateStub -apiVersion "v2" -libraryNameOverride $v2LibraryNameOverride)
+ )
if ($outputFolders.Contains("removed")) {
$v2PreviewDir = $(Join-Path $outputDir "v2Preview")
@@ -176,7 +193,22 @@ function Generate-Versioning {
New-Item -ItemType Directory -Path $v2PreviewDir | Out-Null
}
$v2PreviewLibraryNameOverride = $namespaceRoot + ".V2Preview"
- Invoke (Get-TspCommand $specFilePath $v2PreviewDir -generateStub $generateStub -apiVersion "v2preview" -libraryNameOverride $v2PreviewLibraryNameOverride)
+ $commands += Get-TspCommand $specFilePath $v2PreviewDir -generateStub $generateStub -apiVersion "v2preview" -libraryNameOverride $v2PreviewLibraryNameOverride
+ }
+
+ return $commands
+}
+
+function Generate-Versioning {
+ param (
+ [string]$specFilePath,
+ [string]$outputDir,
+ [bool]$generateStub = $false,
+ [bool]$createOutputDirIfNotExist = $true
+ )
+
+ foreach ($command in Get-VersioningCommands $specFilePath $outputDir -generateStub $generateStub -createOutputDirIfNotExist $createOutputDirIfNotExist) {
+ Invoke $command
}
# exit if the generation failed
@@ -185,6 +217,97 @@ function Generate-Versioning {
}
}
+function Get-SpecGenerationCommands {
+ param (
+ [string]$specFile,
+ [string]$subPath,
+ [string]$outputDir,
+ [bool]$generateStub = $false,
+ [bool]$createOutputDirIfNotExist = $true
+ )
+
+ $folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)
+
+ if ($folders.Contains("versioning")) {
+ return Get-VersioningCommands (Split-Path $specFile) $outputDir -generateStub $generateStub -createOutputDirIfNotExist $createOutputDirIfNotExist
+ }
+
+ # srv-driven contains two separate specs, for two separate clients. We need to generate both.
+ if ($folders.Contains("srv-driven")) {
+ return Get-SrvDrivenCommands (Split-Path $specFile) $outputDir -generateStub $generateStub -createOutputDirIfNotExist $createOutputDirIfNotExist
+ }
+
+ return @(Get-TspCommand $specFile $outputDir $generateStub)
+}
+
+# Runs generation jobs concurrently. Each job is a hashtable with a Name and an ordered list of Commands;
+# commands within a job run sequentially, jobs run in parallel. Output is only printed for failed jobs.
+function Invoke-ParallelGeneration {
+ param (
+ [Parameter(Mandatory = $true)]
+ [object[]]$Jobs,
+ [int]$ThrottleLimit = 0
+ )
+
+ if ($ThrottleLimit -le 0) {
+ $ThrottleLimit = $env:CSHARP_GENERATION_PARALLELISM ? [int]$env:CSHARP_GENERATION_PARALLELISM : [Environment]::ProcessorCount
+ }
+ $ThrottleLimit = [Math]::Max(1, $ThrottleLimit)
+
+ Write-Host "Running $($Jobs.Count) generation job(s) with parallelism $ThrottleLimit" -ForegroundColor Cyan
+ $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
+ $workingDirectory = $packageRoot
+ $useShell = $IsLinux -or $IsMacOS
+
+ $results = $Jobs | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
+ $job = $_
+ $jobStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
+ $output = [System.Text.StringBuilder]::new()
+ $exitCode = 0
+
+ Set-Location $using:workingDirectory
+ foreach ($command in $job.Commands) {
+ [void]$output.AppendLine("> $command")
+ $lines = if ($using:useShell) { sh -c "$command 2>&1" } else { cmd /c "$command 2>&1" }
+ foreach ($line in $lines) {
+ [void]$output.AppendLine("$line")
+ }
+ if ($LASTEXITCODE -ne 0) {
+ $exitCode = $LASTEXITCODE
+ break
+ }
+ }
+
+ [pscustomobject]@{
+ Name = $job.Name
+ ExitCode = $exitCode
+ Output = $output.ToString()
+ Elapsed = $jobStopwatch.Elapsed
+ }
+ }
+
+ $failed = @()
+ foreach ($result in $results | Sort-Object Name) {
+ if ($result.ExitCode -ne 0) {
+ Write-Host "Failed to generate $($result.Name) (exit code $($result.ExitCode))" -ForegroundColor Red
+ Write-Host $result.Output
+ $failed += $result
+ }
+ else {
+ Write-Host "Generated $($result.Name) in $($result.Elapsed.ToString('mm\:ss\.ff'))"
+ }
+ }
+
+ Write-Host "Generation completed in $($stopwatch.Elapsed.ToString('hh\:mm\:ss\.ff'))" -ForegroundColor Cyan
+
+ if ($failed.Count -gt 0) {
+ $global:LASTEXITCODE = 1
+ throw "Generation failed for: $(($failed | ForEach-Object Name) -join ', ')"
+ }
+
+ $global:LASTEXITCODE = 0
+}
+
function Set-LaunchSettings {
param([object]$LaunchSettings)
@@ -200,4 +323,8 @@ Export-ModuleMember -Function "Refresh-Build"
Export-ModuleMember -Function "Compare-Paths"
Export-ModuleMember -Function "Generate-Srv-Driven"
Export-ModuleMember -Function "Generate-Versioning"
+Export-ModuleMember -Function "Get-SrvDrivenCommands"
+Export-ModuleMember -Function "Get-VersioningCommands"
+Export-ModuleMember -Function "Get-SpecGenerationCommands"
+Export-ModuleMember -Function "Invoke-ParallelGeneration"
Export-ModuleMember -Function "Set-LaunchSettings"
diff --git a/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1 b/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
index 8cd8dbc92e2..e14fe42c34f 100644
--- a/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
+++ b/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
@@ -1,14 +1,22 @@
#Requires -Version 7.0
+param(
+ # Skip rebuilding the emitter and generator, e.g. when the caller has already built them.
+ [switch] $SkipBuild
+)
+
Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
Import-Module "$PSScriptRoot\Spector-Helper.psm1" -DisableNameChecking -Force;
-Refresh-Build
+if (-not $SkipBuild) {
+ Refresh-Build
+}
$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')
$spectorCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'Spector.Tests' 'TestProjects.Spector.Tests.csproj'
$spectorRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'Spector'
-$coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage'
+$artifactsDir = Join-Path $packageRoot 'generator' 'artifacts'
+$coverageDir = Join-Path $artifactsDir 'coverage'
if (-not (Test-Path $coverageDir)) {
New-Item -ItemType Directory -Path $coverageDir | Out-Null
@@ -17,52 +25,47 @@ if (-not (Test-Path $coverageDir)) {
$specs = Get-Sorted-Specs
# generate all
- foreach ($specFile in $specs) {
- $subPath = Get-SubPath $specFile
-
- Write-Host "Regenerating $subPath" -ForegroundColor Cyan
- $outputDir = Join-Path $spectorRoot $subPath
-
- if ($subPath.Contains("versioning")) {
- # this will generate v1 and v2 so we only need to call it once for one of the versions
- Generate-Versioning ($specFile | Split-Path) $outputDir -createOutputDirIfNotExist $false
- continue
- }
+$generationJobs = @()
+$projectsToBuild = @($spectorCsproj)
+foreach ($specFile in $specs) {
+ $subPath = Get-SubPath $specFile
+ $folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)
+ $outputDir = Join-Path $spectorRoot $subPath
- if ($subPath.Contains("srv-driven")) {
- # this will generate v1 and v2 so we only need to call it once for one of the versions
- Generate-Srv-Driven ($specFile| Split-Path) $outputDir -createOutputDirIfNotExist $false
- continue
- }
+ $generationJobs += @{
+ Name = $subPath
+ Commands = @(Get-SpecGenerationCommands $specFile $subPath $outputDir -createOutputDirIfNotExist $false)
+ }
- $command = Get-TspCommand $specFile $outputDir
- Invoke $command
- # exit if the generation failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
- }
+ # versioning and srv-driven projects are built through their references from the Spector test project
+ if (-not ($folders.Contains("versioning") -or $folders.Contains("srv-driven"))) {
+ $projectsToBuild += Get-ChildItem -Path $outputDir -Recurse -Filter '*.csproj' | ForEach-Object FullName
+ }
+}
- # build the generated project
- Write-Host "Building $subPath" -ForegroundColor Cyan
- Get-ChildItem -Path $outputDir -Recurse -Filter '*.csproj' | ForEach-Object {
- $command = "dotnet build $_"
- Invoke $command
- # exit if the build failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
- }
- }
- }
+Invoke-ParallelGeneration $generationJobs
- # test all
- Write-Host "Generating Spector coverage" -ForegroundColor Cyan
- $command = "dotnet test $spectorCsproj"
- Invoke $command
- # exit if the testing failed
- if ($LASTEXITCODE -ne 0) {
- exit $LASTEXITCODE
- }
+# build all generated projects and the test project in a single parallel build
+Write-Host "Building generated Spector projects" -ForegroundColor Cyan
+$solutionPath = Join-Path $artifactsDir 'spector-coverage.slnx'
+$solutionProjects = $projectsToBuild | Sort-Object -Unique | ForEach-Object {
+ " "
+}
+Set-Content -Path $solutionPath -Value (@('') + $solutionProjects + @(''))
+Invoke "dotnet build $solutionPath"
+# exit if the build failed
+if ($LASTEXITCODE -ne 0) {
+ exit $LASTEXITCODE
+}
+# test all
+Write-Host "Generating Spector coverage" -ForegroundColor Cyan
+$command = "dotnet test $spectorCsproj --no-build"
+Invoke $command
+# exit if the testing failed
+if ($LASTEXITCODE -ne 0) {
+ exit $LASTEXITCODE
+}
# restore all
foreach ($specFile in $specs) {
$subPath = Get-SubPath $specFile
diff --git a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
index f8cc38fcb4d..175ec425735 100644
--- a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
+++ b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
@@ -38,14 +38,14 @@ try {
# enforce cop static-analysis rules on the generator sources
Invoke-LoggedCommand "./eng/scripts/Invoke-Cop.ps1" -GroupOutput
- # test the generator
- Invoke-LoggedCommand "dotnet test ./generator -p:RestoreConfigFile=`"$nugetConfigPath`" $ciNugetAuditArg" -GroupOutput
+ # test the generator; it was already built by `npm run build` above
+ Invoke-LoggedCommand "dotnet test ./generator --no-build -p:RestoreConfigFile=`"$nugetConfigPath`" $ciNugetAuditArg" -GroupOutput
Invoke-LoggedCommand "dotnet test ./playground-server.Tests -p:RestoreConfigFile=`"$nugetConfigPath`" $ciNugetAuditArg" -GroupOutput
Invoke-LoggedCommand "node --test ./playground-server.Tests/install-azure-generators.node-test.mjs ./playground-server.Tests/deploy.node-test.mjs" -GroupOutput
- Invoke-LoggedCommand "./eng/scripts/Get-Spector-Coverage.ps1" -GroupOutput
+ Invoke-LoggedCommand "./eng/scripts/Get-Spector-Coverage.ps1 -SkipBuild" -GroupOutput
}
finally {
Pop-Location
@@ -62,7 +62,8 @@ try {
Write-Host "Setting up workspace" -ForegroundColor Cyan
Invoke-LoggedCommand "pnpm setup:min" $packageRoot/../..
- Invoke-LoggedCommand "npm run build && npm run regen-docs" -GroupOutput
+ # Generate.ps1 builds the emitter and the generator projects needed for generation, so a full build is not needed here
+ Invoke-LoggedCommand "npm run regen-docs" -GroupOutput
# run E2E Test for TypeSpec emitter
Write-Host "Generating test projects ..."
& "$packageRoot/eng/scripts/Generate.ps1"
From fb8feaa0d54f993378d1ebef405c74666e1cd753 Mon Sep 17 00:00:00 2001
From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com>
Date: Fri, 4 Dec 2026 18:12:10 -0500
Subject: [PATCH 2/2] ci(http-client-csharp): fix nested generation race, run
Spector coverage once, install only .NET 10
- Merge generation jobs with nested output directories so parent and child specs run sequentially
- Collect Spector projects after generation
- Run Spector coverage only in the primary Linux test job
- Pass 2x processor count generation parallelism on CI
- Install only .NET 10 and avoid the cspell failure
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
---
.../eng/pipeline/templates/ci-stages.yml | 26 +++++++------------
.../eng/scripts/Generate.ps1 | 8 ++++--
.../eng/scripts/Generation.psm1 | 24 ++++++++++++++---
.../eng/scripts/Get-Spector-Coverage.ps1 | 17 +++++++++---
.../eng/scripts/Test-Packages.ps1 | 13 ++++++++--
5 files changed, 60 insertions(+), 28 deletions(-)
diff --git a/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml b/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
index afe5ce0609e..874eef4f27b 100644
--- a/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
+++ b/packages/http-client-csharp/eng/pipeline/templates/ci-stages.yml
@@ -5,6 +5,10 @@ parameters:
- name: DependsOn
type: object
default: []
+ # Spector coverage only runs in the Linux test job for this Node version, which is also the job that uploads coverage.
+ - name: PrimaryNodeVersion
+ type: string
+ default: "24.x"
stages:
- template: /eng/emitters/pipelines/templates/stages/emitter-stages.yml
@@ -37,6 +41,7 @@ stages:
TestArguments: -GenerationChecks
Condition: ${{ parameters.Condition }}
DependsOn: ${{ parameters.DependsOn }}
+ PrimaryNodeVersion: ${{ parameters.PrimaryNodeVersion }}
LanguageShortName: "csharp"
AdditionalInitializeSteps:
- pwsh: |
@@ -44,6 +49,10 @@ stages:
Write-Host "##vso[task.setvariable variable=DOTNET_NOLOGO]true"
Write-Host "##vso[task.setvariable variable=DOTNET_CLI_TELEMETRY_OPTOUT]true"
Write-Host "##vso[task.setvariable variable=DOTNET_SKIP_FIRST_TIME_EXPERIENCE]true"
+ $coverageJob = "Test_linux_${{ split(parameters.PrimaryNodeVersion, '.')[0] }}"
+ if ("$(System.PhaseName)" -eq $coverageJob -or "$(Agent.JobName)" -eq $coverageJob) {
+ Write-Host "##vso[task.setvariable variable=CSHARP_RUN_SPECTOR_COVERAGE]true"
+ }
displayName: "Configure NuGet cache and .NET CLI"
- task: Cache@2
displayName: "Cache NuGet packages"
@@ -53,22 +62,7 @@ stages:
"nuget" | "$(Agent.OS)"
path: $(NUGET_PACKAGES)
- task: UseDotNet@2
+ displayName: "Install .NET 10"
inputs:
- displayName: "Install .NET 8"
- performMultiLevelLookup: true
- useGlobalJson: false
- version: 8.x
- workingDirectory: $(Build.SourcesDirectory)/packages/http-client-csharp
- - task: UseDotNet@2
- inputs:
- displayName: "Install .NET 9"
- performMultiLevelLookup: true
- useGlobalJson: false
- version: 9.x
- workingDirectory: $(Build.SourcesDirectory)/packages/http-client-csharp
- - task: UseDotNet@2
- inputs:
- displayName: "Install .NET 10"
- performMultiLevelLookup: true
useGlobalJson: true
workingDirectory: $(Build.SourcesDirectory)/packages/http-client-csharp
diff --git a/packages/http-client-csharp/eng/scripts/Generate.ps1 b/packages/http-client-csharp/eng/scripts/Generate.ps1
index 5302756bd72..c96dddfc70c 100644
--- a/packages/http-client-csharp/eng/scripts/Generate.ps1
+++ b/packages/http-client-csharp/eng/scripts/Generate.ps1
@@ -2,7 +2,9 @@
param(
$filter,
[bool]$Stubbed = $true,
- [bool]$LaunchOnly = $false
+ [bool]$LaunchOnly = $false,
+ # Maximum number of concurrent generation jobs; 0 uses the default of Invoke-ParallelGeneration.
+ [int]$GenerationParallelism = 0
)
Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
@@ -24,6 +26,7 @@ if (-not $LaunchOnly) {
if ($generateSampleTypeSpec) {
$generationJobs += @{
Name = "Sample-TypeSpec"
+ OutputDir = $SampleTypeSpecTestProject
Commands = @(Get-TspCommand "$SampleTypeSpecTestProject/Sample-TypeSpec.tsp" $SampleTypeSpecTestProject)
}
}
@@ -63,6 +66,7 @@ foreach ($specFile in Get-Sorted-Specs) {
$generationJobs += @{
Name = $subPath
+ OutputDir = $generationDir
Commands = @(Get-SpecGenerationCommands $specFile $subPath $generationDir -generateStub $stubbed)
}
@@ -70,7 +74,7 @@ foreach ($specFile in Get-Sorted-Specs) {
}
if ($generationJobs.Count -gt 0) {
- Invoke-ParallelGeneration $generationJobs
+ Invoke-ParallelGeneration $generationJobs -ThrottleLimit $GenerationParallelism
}
if ($generateSampleTypeSpec) {
diff --git a/packages/http-client-csharp/eng/scripts/Generation.psm1 b/packages/http-client-csharp/eng/scripts/Generation.psm1
index d14b81eb98b..63f702ff987 100644
--- a/packages/http-client-csharp/eng/scripts/Generation.psm1
+++ b/packages/http-client-csharp/eng/scripts/Generation.psm1
@@ -240,7 +240,7 @@ function Get-SpecGenerationCommands {
return @(Get-TspCommand $specFile $outputDir $generateStub)
}
-# Runs generation jobs concurrently. Each job is a hashtable with a Name and an ordered list of Commands;
+# Runs generation jobs concurrently. Each job is a hashtable with a Name, an OutputDir and an ordered list of Commands;
# commands within a job run sequentially, jobs run in parallel. Output is only printed for failed jobs.
function Invoke-ParallelGeneration {
param (
@@ -254,6 +254,22 @@ function Invoke-ParallelGeneration {
}
$ThrottleLimit = [Math]::Max(1, $ThrottleLimit)
+ # Generation deletes every project file under its output directory, so a job whose output directory is
+ # nested inside another job's output directory is merged into that job and runs after it.
+ $mergedJobs = [System.Collections.Generic.List[hashtable]]::new()
+ foreach ($job in $Jobs | Sort-Object { $_.OutputDir ? ([System.IO.Path]::GetFullPath($_.OutputDir)).Length : 0 }) {
+ $root = $job.OutputDir ? [System.IO.Path]::GetFullPath($job.OutputDir).TrimEnd('\', '/') + [System.IO.Path]::DirectorySeparatorChar : $null
+ $parent = $root ? ($mergedJobs | Where-Object { $_.Root -and $root.StartsWith($_.Root, [System.StringComparison]::OrdinalIgnoreCase) } | Select-Object -First 1) : $null
+ if ($parent) {
+ $parent.Name = "$($parent.Name), $($job.Name)"
+ $parent.Commands = @($parent.Commands) + @($job.Commands)
+ }
+ else {
+ $mergedJobs.Add(@{ Name = $job.Name; Root = $root; Commands = @($job.Commands) })
+ }
+ }
+ $Jobs = $mergedJobs.ToArray()
+
Write-Host "Running $($Jobs.Count) generation job(s) with parallelism $ThrottleLimit" -ForegroundColor Cyan
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$workingDirectory = $packageRoot
@@ -278,7 +294,7 @@ function Invoke-ParallelGeneration {
}
}
- [pscustomobject]@{
+ @{
Name = $job.Name
ExitCode = $exitCode
Output = $output.ToString()
@@ -287,7 +303,7 @@ function Invoke-ParallelGeneration {
}
$failed = @()
- foreach ($result in $results | Sort-Object Name) {
+ foreach ($result in $results | Sort-Object { $_.Name }) {
if ($result.ExitCode -ne 0) {
Write-Host "Failed to generate $($result.Name) (exit code $($result.ExitCode))" -ForegroundColor Red
Write-Host $result.Output
@@ -302,7 +318,7 @@ function Invoke-ParallelGeneration {
if ($failed.Count -gt 0) {
$global:LASTEXITCODE = 1
- throw "Generation failed for: $(($failed | ForEach-Object Name) -join ', ')"
+ throw "Generation failed for: $(($failed | ForEach-Object { $_.Name }) -join ', ')"
}
$global:LASTEXITCODE = 0
diff --git a/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1 b/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
index e14fe42c34f..ba500acdaea 100644
--- a/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
+++ b/packages/http-client-csharp/eng/scripts/Get-Spector-Coverage.ps1
@@ -2,7 +2,9 @@
param(
# Skip rebuilding the emitter and generator, e.g. when the caller has already built them.
- [switch] $SkipBuild
+ [switch] $SkipBuild,
+ # Maximum number of concurrent generation jobs; 0 uses the default of Invoke-ParallelGeneration.
+ [int] $GenerationParallelism = 0
)
Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
@@ -26,7 +28,7 @@ $specs = Get-Sorted-Specs
# generate all
$generationJobs = @()
-$projectsToBuild = @($spectorCsproj)
+$projectDirs = @()
foreach ($specFile in $specs) {
$subPath = Get-SubPath $specFile
$folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)
@@ -34,16 +36,23 @@ foreach ($specFile in $specs) {
$generationJobs += @{
Name = $subPath
+ OutputDir = $outputDir
Commands = @(Get-SpecGenerationCommands $specFile $subPath $outputDir -createOutputDirIfNotExist $false)
}
# versioning and srv-driven projects are built through their references from the Spector test project
if (-not ($folders.Contains("versioning") -or $folders.Contains("srv-driven"))) {
- $projectsToBuild += Get-ChildItem -Path $outputDir -Recurse -Filter '*.csproj' | ForEach-Object FullName
+ $projectDirs += $outputDir
}
}
-Invoke-ParallelGeneration $generationJobs
+Invoke-ParallelGeneration $generationJobs -ThrottleLimit $GenerationParallelism
+
+# collect projects after generation since generation deletes and recreates the project files
+$projectsToBuild = @($spectorCsproj)
+foreach ($projectDir in $projectDirs) {
+ $projectsToBuild += Get-ChildItem -Path $projectDir -Recurse -Filter '*.csproj' | ForEach-Object FullName
+}
# build all generated projects and the test project in a single parallel build
Write-Host "Building generated Spector projects" -ForegroundColor Cyan
diff --git a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1 b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
index 175ec425735..c2ece0f90d7 100644
--- a/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
+++ b/packages/http-client-csharp/eng/scripts/Test-Packages.ps1
@@ -18,6 +18,10 @@ $nugetConfigPath = (Join-Path $repoRoot "eng/nuget.config")
$ciNugetAuditArg = $env:TF_BUILD ? "-p:NuGetAudit=false" : ""
$previousRestoreConfigFile = $env:RestoreConfigFile
$previousNuGetAudit = $env:NuGetAudit
+# On CI, oversubscribe generation since each job spends much of its time on process startup and I/O.
+$generationParallelism = $env:TF_BUILD ? [Environment]::ProcessorCount * 2 : 0
+# On CI, Spector coverage only runs in the job that sets CSHARP_RUN_SPECTOR_COVERAGE (see eng/pipeline/templates/ci-stages.yml).
+$runSpectorCoverage = (-not $env:TF_BUILD) -or ($env:CSHARP_RUN_SPECTOR_COVERAGE -eq 'true')
try {
# Ensure all dotnet processes, including those spawned by npm scripts and generation,
# restore through the repository's Azure DevOps feed instead of nuget.org.
@@ -45,7 +49,12 @@ try {
Invoke-LoggedCommand "node --test ./playground-server.Tests/install-azure-generators.node-test.mjs ./playground-server.Tests/deploy.node-test.mjs" -GroupOutput
- Invoke-LoggedCommand "./eng/scripts/Get-Spector-Coverage.ps1 -SkipBuild" -GroupOutput
+ if ($runSpectorCoverage) {
+ Invoke-LoggedCommand "./eng/scripts/Get-Spector-Coverage.ps1 -SkipBuild -GenerationParallelism $generationParallelism" -GroupOutput
+ }
+ else {
+ Write-Host "Skipping Spector coverage; it runs in a single CI job." -ForegroundColor Yellow
+ }
}
finally {
Pop-Location
@@ -66,7 +75,7 @@ try {
Invoke-LoggedCommand "npm run regen-docs" -GroupOutput
# run E2E Test for TypeSpec emitter
Write-Host "Generating test projects ..."
- & "$packageRoot/eng/scripts/Generate.ps1"
+ & "$packageRoot/eng/scripts/Generate.ps1" -GenerationParallelism $generationParallelism
Write-Host 'Code generation is completed.'
try {