-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix source-generator package delivery and preview restore (Roslyn packaging) #4041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcschier
merged 2 commits into
OPCFoundation:master
from
marcschier:marcschier/review-fix/source-generator-packaging
Jul 22, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,299 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Validates the source-generator NuGet payloads and builds clean consumers. | ||
|
|
||
| .PARAMETER PackageDirectory | ||
| Directory containing the packed source-generator NuGet packages. | ||
| #> | ||
|
|
||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string] $PackageDirectory | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| function Assert-Condition | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [bool] $Condition, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $Message | ||
| ) | ||
|
|
||
| if (-not $Condition) | ||
| { | ||
| throw $Message | ||
| } | ||
| } | ||
|
|
||
| function Get-PackageInfo | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string] $PackageId | ||
| ) | ||
|
|
||
| $packagePattern = "^$([Regex]::Escape($PackageId))\.(?<version>[0-9].+)\.nupkg$" | ||
| $package = Get-ChildItem -Path $PackageDirectory -Filter "$PackageId*.nupkg" -File -Recurse | | ||
| Where-Object { $_.Name -notlike "*.snupkg" -and $_.Name -match $packagePattern } | | ||
| Sort-Object LastWriteTimeUtc -Descending | | ||
| Select-Object -First 1 | ||
|
|
||
| Assert-Condition ($null -ne $package) "Package '$PackageId' was not found in '$PackageDirectory'." | ||
|
|
||
| $archive = [IO.Compression.ZipFile]::OpenRead($package.FullName) | ||
| try | ||
| { | ||
| $entries = @($archive.Entries | ForEach-Object FullName) | ||
| $nuspecEntries = @($archive.Entries | | ||
| Where-Object { $_.FullName.EndsWith(".nuspec", [StringComparison]::OrdinalIgnoreCase) }) | ||
| Assert-Condition ( | ||
| $nuspecEntries.Count -eq 1 | ||
| ) "Package '$($package.Name)' must contain exactly one nuspec." | ||
| $nuspecEntry = $nuspecEntries[0] | ||
|
|
||
| $reader = [IO.StreamReader]::new($nuspecEntry.Open()) | ||
| try | ||
| { | ||
| [xml] $nuspec = $reader.ReadToEnd() | ||
| } | ||
| finally | ||
| { | ||
| $reader.Dispose() | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| $archive.Dispose() | ||
| } | ||
|
|
||
| [PSCustomObject] @{ | ||
| Id = $PackageId | ||
| Path = $package.FullName | ||
| Version = [string] $nuspec.package.metadata.version | ||
| Entries = $entries | ||
| Dependencies = @($nuspec.SelectNodes("//*[local-name()='dependency']")) | ||
| } | ||
| } | ||
|
|
||
| function Test-PackageContents | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [PSCustomObject] $Package, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $GeneratorAssembly | ||
| ) | ||
|
|
||
| $analyzerPath = "analyzers/dotnet/cs/" | ||
| $requiredAssemblies = @( | ||
| $GeneratorAssembly, | ||
| "Opc.Ua.SourceGeneration.Core.dll", | ||
| "Opc.Ua.Types.dll", | ||
| "SourceGenerator.Foundations.Contracts.dll", | ||
| "SourceGenerator.Foundations.Windows.dll" | ||
| ) | ||
|
|
||
| foreach ($assembly in $requiredAssemblies) | ||
| { | ||
| Assert-Condition ( | ||
| $Package.Entries -contains "$analyzerPath$assembly" | ||
| ) "Package '$($Package.Id)' is missing '$analyzerPath$assembly'." | ||
| } | ||
|
|
||
| $dllEntries = @($Package.Entries | | ||
| Where-Object { $_.EndsWith(".dll", [StringComparison]::OrdinalIgnoreCase) }) | ||
| Assert-Condition ($dllEntries.Count -gt 0) "Package '$($Package.Id)' contains no assemblies." | ||
| Assert-Condition ( | ||
| @($dllEntries | Where-Object { -not $_.StartsWith( | ||
| $analyzerPath, | ||
| [StringComparison]::OrdinalIgnoreCase) }).Count -eq 0 | ||
| ) "Package '$($Package.Id)' contains assemblies outside '$analyzerPath'." | ||
| Assert-Condition ( | ||
| @($dllEntries | Where-Object { | ||
| [IO.Path]::GetFileName($_).StartsWith( | ||
| "Microsoft.CodeAnalysis", | ||
| [StringComparison]::OrdinalIgnoreCase) | ||
| }).Count -eq 0 | ||
| ) "Package '$($Package.Id)' must not ship Microsoft.CodeAnalysis host assemblies." | ||
| Assert-Condition ( | ||
| $Package.Dependencies.Count -eq 0 | ||
| ) "Package '$($Package.Id)' must carry its analyzer runtime closure privately." | ||
| } | ||
|
|
||
| function Invoke-DotNet | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string[]] $Arguments | ||
| ) | ||
|
|
||
| & dotnet @Arguments | ||
| if ($LASTEXITCODE -ne 0) | ||
| { | ||
| throw "dotnet $($Arguments -join ' ') failed with exit code $LASTEXITCODE." | ||
| } | ||
| } | ||
|
|
||
| function Get-MSBuildProperty | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string] $ProjectPath, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $Configuration, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $PropertyName | ||
| ) | ||
|
|
||
| $output = & dotnet msbuild $ProjectPath ` | ||
| "-getProperty:$PropertyName" ` | ||
| "-p:Configuration=$Configuration" ` | ||
| -nologo | ||
| if ($LASTEXITCODE -ne 0) | ||
| { | ||
| throw "Could not read MSBuild property '$PropertyName' from '$ProjectPath'." | ||
| } | ||
|
|
||
| return [string]($output | | ||
| Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | | ||
| Select-Object -Last 1).Trim() | ||
| } | ||
|
|
||
| function Test-ConfigurationPackageIds | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string] $ProjectPath | ||
| ) | ||
|
|
||
| $releaseId = Get-MSBuildProperty $ProjectPath "Release" "PackageId" | ||
| $debugId = Get-MSBuildProperty $ProjectPath "Debug" "PackageId" | ||
| Assert-Condition ( | ||
| $debugId -eq "$releaseId.Debug" | ||
| ) "Debug package '$debugId' must use the release ID '$releaseId' with a '.Debug' suffix." | ||
| } | ||
|
|
||
| function Test-CleanConsumer | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [PSCustomObject] $Package, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $ValidationRoot | ||
| ) | ||
|
|
||
| $consumerName = $Package.Id.Split(".")[-1] | ||
| $consumerDirectory = Join-Path $ValidationRoot $consumerName | ||
| New-Item -ItemType Directory -Path $consumerDirectory | Out-Null | ||
| $projectPath = Join-Path $consumerDirectory "$consumerName.csproj" | ||
| $expectedDiagnosticSuppression = if ($Package.Id.EndsWith( | ||
| ".Stack", | ||
| [StringComparison]::Ordinal)) | ||
| { | ||
| " <NoWarn>STACKGEN001</NoWarn>" | ||
| } | ||
| else | ||
| { | ||
| "" | ||
| } | ||
| @" | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
| $expectedDiagnosticSuppression | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="$($Package.Id)" Version="$($Package.Version)" | ||
| PrivateAssets="all" /> | ||
| </ItemGroup> | ||
| </Project> | ||
| "@ | Set-Content -Path $projectPath -Encoding utf8 | ||
| @" | ||
| namespace SourceGeneratorConsumer; | ||
|
|
||
| public static class ConsumerMarker | ||
| { | ||
| public static int Value => 42; | ||
| } | ||
| "@ | Set-Content -Path (Join-Path $consumerDirectory "ConsumerMarker.cs") -Encoding utf8 | ||
|
|
||
| $nugetConfig = Join-Path $ValidationRoot "NuGet.Config" | ||
| $packagesPath = Join-Path $ValidationRoot "packages" | ||
| Invoke-DotNet @( | ||
| "restore", | ||
| $projectPath, | ||
| "--configfile", | ||
| $nugetConfig, | ||
| "--packages", | ||
| $packagesPath, | ||
| "--nologo" | ||
| ) | ||
| Invoke-DotNet @( | ||
| "build", | ||
| $projectPath, | ||
| "--configuration", | ||
| "Release", | ||
| "--no-restore", | ||
| "--nologo" | ||
| ) | ||
| } | ||
|
|
||
| Add-Type -AssemblyName System.IO.Compression.FileSystem | ||
|
|
||
| $resolvedPackageDirectory = (Resolve-Path $PackageDirectory).Path | ||
| $PackageDirectory = $resolvedPackageDirectory | ||
| $repoRoot = Split-Path $PSScriptRoot -Parent | ||
| $validationRoot = Join-Path $repoRoot "artifacts\source-generator-consumer" | ||
|
marcschier marked this conversation as resolved.
Outdated
|
||
| Test-ConfigurationPackageIds ( | ||
| Join-Path $repoRoot "tools\Opc.Ua.SourceGeneration\Opc.Ua.SourceGeneration.csproj") | ||
| Test-ConfigurationPackageIds ( | ||
| Join-Path $repoRoot "tools\Opc.Ua.SourceGeneration.Stack\Opc.Ua.SourceGeneration.Stack.csproj") | ||
| $modelPackage = Get-PackageInfo "OPCFoundation.NetStandard.Opc.Ua.SourceGeneration" | ||
| $stackPackage = Get-PackageInfo "OPCFoundation.NetStandard.Opc.Ua.SourceGeneration.Stack" | ||
|
|
||
| Test-PackageContents $modelPackage "Opc.Ua.SourceGeneration.dll" | ||
| Test-PackageContents $stackPackage "Opc.Ua.SourceGeneration.Stack.dll" | ||
|
|
||
| Remove-Item -Path $validationRoot -Recurse -Force -ErrorAction SilentlyContinue | ||
| New-Item -ItemType Directory -Path $validationRoot | Out-Null | ||
| try | ||
| { | ||
| "<Project />" | Set-Content -Path (Join-Path $validationRoot "Directory.Build.props") -Encoding utf8 | ||
| "<Project />" | Set-Content -Path (Join-Path $validationRoot "Directory.Build.targets") -Encoding utf8 | ||
| @" | ||
| <Project> | ||
| <PropertyGroup> | ||
| <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally> | ||
| </PropertyGroup> | ||
| </Project> | ||
| "@ | Set-Content -Path (Join-Path $validationRoot "Directory.Packages.props") -Encoding utf8 | ||
| $escapedPackageDirectory = [System.Security.SecurityElement]::Escape($PackageDirectory) | ||
| @" | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <packageSources> | ||
| <clear /> | ||
| <add key="source-generator-packages" value="$escapedPackageDirectory" /> | ||
| </packageSources> | ||
| </configuration> | ||
| "@ | Set-Content -Path (Join-Path $validationRoot "NuGet.Config") -Encoding utf8 | ||
|
|
||
| Test-CleanConsumer $modelPackage $validationRoot | ||
| Test-CleanConsumer $stackPackage $validationRoot | ||
| } | ||
| finally | ||
| { | ||
| Remove-Item -Path $validationRoot -Recurse -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| Write-Host "Validated source-generator package contents and clean consumers." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.