Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CI/job_templates/build_drawing_libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ jobs:
--property:SixLaborsLicenseFile=$(sixLaborsLicense.secureFilePath)
versioningScheme: byEnvVar
versionEnvVar: AssemblyVersion
# Fail if a shipped DLL references System.Memory / Buffers / Unsafe / Vectors (etc.) at a higher
# assembly version than the nuspec dependency guarantees (FileLoadException on .NET Framework, OCR-372).
- task: PowerShell@2
displayName: Check System.* assembly references against nuspec
inputs:
targetType: 'filePath'
filePath: '$(Build.SourcesDirectory)/CI/scripts/Test-SystemAssemblyReferences.ps1'
arguments: >-
-NuspecPath '$(Build.SourcesDirectory)/NuGet/IronSoftware.Drawing.nuspec'
-BinRoot '$(Build.SourcesDirectory)/IronSoftware.Drawing/IronSoftware.Drawing.Common/bin/${{ parameters.buildConfiguration }}'
pwsh: true
# Publish Pipeline Artifacts
- task: CopyFiles@2
displayName: 'Copy tests.runsettings file'
Expand Down
166 changes: 166 additions & 0 deletions CI/scripts/Test-SystemAssemblyReferences.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<#
.SYNOPSIS
Fails the build if a DLL we ship references a System.* compatibility assembly at a higher
version than the NuGet dependency declared in the nuspec guarantees.

.DESCRIPTION
On .NET Framework an assembly reference to, for example, System.Memory 4.0.2.0 cannot be
satisfied by System.Memory 4.0.1.2 on disk; the app throws FileLoadException. This happened
in OCR-372: code compiled against System.Memory 4.6.x shipped in a package that only
guaranteed 4.5.5.

For every netstandard2.0 / .NET Framework dependency group in the nuspec, this script reads
the DLLs that group ships (from the nuspec <file> entries), lists their references to the
watched assemblies and checks each one against the assembly version inside the package
version declared for that group. A watched reference without a declared dependency also
fails.

.EXAMPLE
pwsh CI/scripts/Test-SystemAssemblyReferences.ps1 `
-NuspecPath NuGet/IronSoftware.Drawing.nuspec `
-BinRoot IronSoftware.Drawing/IronSoftware.Drawing.Common/bin/Release
#>
param(
[Parameter(Mandatory = $true)] [string] $NuspecPath,
# Folder that replaces "..\bin\$configuration$\" in the nuspec <file src> entries.
[Parameter(Mandatory = $true)] [string] $BinRoot,
[string] $PackagesRoot = $(if ($env:NUGET_PACKAGES) { $env:NUGET_PACKAGES } else { Join-Path $HOME '.nuget/packages' }),
[string[]] $Frameworks = @('netstandard2.0', 'net462', 'net472', 'net48'),
[string[]] $WatchedAssemblies = @(
'System.Memory',
'System.Buffers',
'System.Numerics.Vectors',
'System.Runtime.CompilerServices.Unsafe',
'System.Threading.Tasks.Extensions',
'Microsoft.Bcl.AsyncInterfaces',
'System.Text.Json',
'System.Text.Encodings.Web'
)
)

$ErrorActionPreference = 'Stop'

function Get-AssemblyReferences([string] $path) {
$stream = [IO.File]::OpenRead($path)
try {
$pe = [System.Reflection.PortableExecutable.PEReader]::new($stream)
$md = [System.Reflection.Metadata.PEReaderExtensions]::GetMetadataReader($pe)
foreach ($handle in $md.AssemblyReferences) {
$ref = $md.GetAssemblyReference($handle)
[pscustomobject]@{ Name = $md.GetString($ref.Name); Version = $ref.Version }
}
$pe.Dispose()
}
finally { $stream.Dispose() }
}

function Get-AssemblyVersion([string] $path) {
$stream = [IO.File]::OpenRead($path)
try {
$pe = [System.Reflection.PortableExecutable.PEReader]::new($stream)
$md = [System.Reflection.Metadata.PEReaderExtensions]::GetMetadataReader($pe)
$version = $md.GetAssemblyDefinition().Version
$pe.Dispose()
return $version
}
finally { $stream.Dispose() }
}

# Assembly version the given package version provides to consumers of our $tfm lib folder.
# The package lib folders can differ (System.Memory 4.6.3: net462 = 4.0.5.0, netstandard2.0 = 4.0.2.0).
# - .NET Framework group: the app resolves the package's own .NET Framework lib folder (the highest
# net4x folder not above our target, else netstandard2.0), so check against that one.
# - netstandard2.0 group: it can be consumed from any .NET Framework or netstandard app, so take the
# minimum across those lib folders to be safe for every consumer.
function Get-PackageAssemblyVersion([string] $id, [string] $version, [string] $assemblyName, [string] $tfm) {
$packageDir = Join-Path $PackagesRoot (Join-Path $id.ToLowerInvariant() $version.ToLowerInvariant())
if (-not (Test-Path $packageDir)) {
# On a clean agent only the version the build restored is in the packages folder, so a declared
# version that is missing usually means the nuspec and the compile reference disagree.
return $null
}
$candidates = 'net48', 'net472', 'net471', 'net47', 'net462', 'net461', 'net46', 'net45'
if ($tfm -match '^net4\d+$') {
$ours = [int]($tfm.Substring(3).PadRight(3, '0'))
$lib = @($candidates | Where-Object { [int]($_.Substring(3).PadRight(3, '0')) -le $ours }) + 'netstandard2.0' |
Where-Object { Test-Path (Join-Path $packageDir "lib/$_/$assemblyName.dll") } |
Select-Object -First 1
$versions = if ($lib) { Get-AssemblyVersion (Join-Path $packageDir "lib/$lib/$assemblyName.dll") }
}
else {
$versions = foreach ($lib in $candidates + 'netstandard2.0') {
$dll = Join-Path $packageDir "lib/$lib/$assemblyName.dll"
if (Test-Path $dll) { Get-AssemblyVersion $dll }
}
}
if (-not $versions) {
throw "No .NET Framework / netstandard2.0 assembly found for $id $version in '$packageDir'."
}
return ($versions | Sort-Object | Select-Object -First 1)
}

function ConvertTo-ShortFramework([string] $tfm) {
switch -Regex ($tfm) {
'^\.NETStandard(\d)\.(\d)$' { return "netstandard$($Matches[1]).$($Matches[2])" }
'^\.NETFramework(\d)\.(\d)(?:\.(\d))?$' { return "net$($Matches[1])$($Matches[2])$($Matches[3])" }
default { return $tfm.ToLowerInvariant() }
}
}

[xml] $nuspec = Get-Content -Raw -Path $NuspecPath
$failures = New-Object System.Collections.Generic.List[string]
$checked = 0

$files = $nuspec.SelectNodes("//*[local-name()='files']/*[local-name()='file']")
foreach ($group in $nuspec.SelectNodes("//*[local-name()='dependencies']/*[local-name()='group']")) {
$tfm = ConvertTo-ShortFramework $group.GetAttribute('targetFramework')
if ($Frameworks -notcontains $tfm) { continue }

$declared = @{}
foreach ($dep in $group.SelectNodes("*[local-name()='dependency']")) {
# Minimum version of a range such as "[4.6.3, )" or "4.6.3".
$declared[$dep.GetAttribute('id')] = ($dep.GetAttribute('version') -replace '^[\[\(]\s*', '' -split '[,\]\)]')[0].Trim()
}

$dlls = foreach ($file in $files) {
# Compare and join with '/', which works on Windows, Linux and macOS agents.
$target = $file.GetAttribute('target') -replace '\\', '/'
if ($target.TrimEnd('/') -ne "lib/$tfm") { continue }
$src = $file.GetAttribute('src') -replace '\\', '/'
$relative = $src -replace '^.*?/bin/\$configuration\$/', ''
Get-ChildItem -Path (Join-Path $BinRoot $relative) -ErrorAction SilentlyContinue |
Where-Object { $_.Extension -eq '.dll' }
}
if (-not $dlls) {
$failures.Add("[$tfm] No DLLs found under '$BinRoot' for the nuspec lib/$tfm entries. Build before running this check.")
continue
}

foreach ($dll in $dlls) {
foreach ($ref in Get-AssemblyReferences $dll.FullName) {
if ($WatchedAssemblies -notcontains $ref.Name) { continue }
$checked++
if (-not $declared.ContainsKey($ref.Name)) {
$failures.Add("[$tfm] $($dll.Name) references $($ref.Name) $($ref.Version), but the nuspec does not declare a $($ref.Name) dependency for $tfm.")
continue
}
$packageVersion = $declared[$ref.Name]
$provided = Get-PackageAssemblyVersion $ref.Name $packageVersion $ref.Name $tfm
if ($null -eq $provided) {
$failures.Add("[$tfm] The nuspec declares $($ref.Name) $packageVersion, but that version is not restored in '$PackagesRoot'. $($dll.Name) was probably compiled against a different version: check IronSoftware.Drawing/SystemPackages.props against the nuspec.")
continue
}
$status = if ($ref.Version -gt $provided) { 'FAIL' } else { 'ok' }
Write-Host ("[{0}] {1}: {2} {3} -> declared package {4} provides {5} {6}" -f $tfm, $dll.Name, $ref.Name, $ref.Version, $packageVersion, $provided, $status)
if ($status -eq 'FAIL') {
$failures.Add("[$tfm] $($dll.Name) references $($ref.Name) $($ref.Version), but the declared $($ref.Name) $packageVersion only provides $provided. Raise the nuspec dependency (and the compile reference) together.")
}
}
}
}

if ($failures.Count -gt 0) {
$failures | ForEach-Object { Write-Host "##vso[task.logissue type=error]$_"; Write-Host "ERROR: $_" }
exit 1
}
Write-Host "System.* assembly reference check passed ($checked references checked)."
9 changes: 9 additions & 0 deletions CI/stage_templates/run_tests_on_pool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ stages:
framework: 'net48'
architecture: ''
buildConfiguration: $(Configuration)
# Windows .NET 481 x64 Tests: net481 loads the netstandard2.0 build (ImageSharp 2.1.13 + BigTIFF guard),
# while net48 loads the net462 (SkiaSharp) build. See IronSoftware.Drawing.Common.Tests.csproj.
- template: ../job_templates/test_drawing_libraries.yml
parameters:
name: UnitTest${{ parameters.OSPlatform }}net481x64
OSPlatform: ${{ parameters.OSPlatform }}
framework: 'net481'
architecture: ''
buildConfiguration: $(Configuration)
# .NET 8.0 Tests
- template: ../job_templates/test_drawing_libraries.yml
parameters:
Expand Down
17 changes: 17 additions & 0 deletions CI/step_templates/sign_drawing_libraries_dll.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ steps:
--file-digest sha256 `
--verbose `
"IronSoftware.Drawing.Common.dll"
- task: PowerShell@2
displayName: 'Sign IronSoftware.Drawing.Common.dll .NET Framework (net462)'
inputs:
workingDirectory: 'bin\$(Configuration)\net462'
targetType: 'inline'
script: |
azuresigntool sign `
--azure-key-vault-url "$(AZURE_KEY_VAULT_URL)" `
--azure-key-vault-client-id "$(AZURE_KEY_VAULT_APPLICATION_ID)" `
--azure-key-vault-client-secret "$(AZURE_KEY_VAULT_CLIENT_SECRET)" `
--azure-key-vault-tenant-id "$(AZURE_KEY_VAULT_TENANT_ID)" `
--azure-key-vault-certificate "$(AZURE_KEY_VAULT_CERTIFICATE_NAME)" `
--timestamp-rfc3161 "$(TimestampUrl)" `
--timestamp-digest sha256 `
--file-digest sha256 `
--verbose `
"IronSoftware.Drawing.Common.dll"
- task: PowerShell@2
displayName: 'Sign IronSoftware.Drawing.Common.dll .NET 6 (net60)'
inputs:
Expand Down
17 changes: 17 additions & 0 deletions IronSoftware.Drawing/DependencyVersions.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>
<!--
Versions of the packages IronSoftware.Drawing.Common's netstandard2.0 build depends on (plus the ones shared
with other targets). Imported by IronSoftware.Drawing.Common.csproj and by the test project, whose net481
target references the netstandard2.0 build directly and must restore exactly the same dependency set.
Keep the netstandard2.0 group of NuGet/IronSoftware.Drawing.nuspec in sync.
-->
<PropertyGroup>
<DrawingAbstractionsVersion>2026.1.2</DrawingAbstractionsVersion>
<LibTiffVersion>2.4.649</LibTiffVersion>
<NetStandardImageSharpVersion>2.1.13</NetStandardImageSharpVersion>
<NetStandardImageSharpDrawingVersion>1.0.0</NetStandardImageSharpDrawingVersion>
<!-- SkiaSharp line used by the netstandard2.0, net6.0 and net8.0 builds (net462 uses SkiaSharp 4). -->
<LegacySkiaSharpVersion>2.88.7</LegacySkiaSharpVersion>
<SkiaSharpSvgVersion>1.60.0</SkiaSharpSvgVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net80</TargetFrameworks>
<TargetFrameworks>net48;net481;net80</TargetFrameworks>
<LangVersion>latest</LangVersion>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IsPackable>false</IsPackable>
Expand All @@ -14,8 +14,6 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="7.0.92" />
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="6.0.5.128" />
<PackageReference Include="SkiaSharp" Version="2.88.7" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.7" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand All @@ -28,8 +26,52 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<!-- IronSoftware.Drawing.Common net462 runs on SkiaSharp 4.x; ImageSharp is referenced here only by the test helpers (Compare.cs). -->
<PackageReference Include="SkiaSharp" Version="4.153.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.13" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net48'">
<PackageReference Include="SkiaSharp" Version="$(LegacySkiaSharpVersion)" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="$(LegacySkiaSharpVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net481'">
<ProjectReference Include="..\IronSoftware.Drawing.Common\IronSoftware.Drawing.Common.csproj" />
</ItemGroup>

<!--
net481 tests Drawing's netstandard2.0 build (ImageSharp 2.1.13 and the BigTIFF guard), which still ships to
netstandard2.0 consumers; net48 picks the net462 build. A normal ProjectReference would resolve net462 for
net481 too, so reference the netstandard2.0 output directly (built first through the reference below,
which ReferenceOutputAssembly=false keeps out of restore) and declare its dependencies. The versions come
from the same DependencyVersions.props and SystemPackages.props that the Drawing csproj uses.
-->
<ItemGroup Condition="'$(TargetFramework)' == 'net481'">
<ProjectReference Include="..\IronSoftware.Drawing.Common\IronSoftware.Drawing.Common.csproj" SetTargetFramework="TargetFramework=netstandard2.0" ReferenceOutputAssembly="false" SkipGetTargetFrameworkProperties="true" />
<Reference Include="IronSoftware.Drawing.Common">
<HintPath>..\IronSoftware.Drawing.Common\bin\$(Configuration)\netstandard2.0\IronSoftware.Drawing.Common.dll</HintPath>
</Reference>
<PackageReference Include="IronSoftware.Drawing.Abstractions" Version="$(DrawingAbstractionsVersion)" />
<PackageReference Include="BitMiracle.LibTiff.NET" Version="$(LibTiffVersion)" />
<PackageReference Include="SixLabors.ImageSharp" Version="$(NetStandardImageSharpVersion)" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="$(NetStandardImageSharpDrawingVersion)" />
<PackageReference Include="SkiaSharp.Svg" Version="$(SkiaSharpSvgVersion)" />
</ItemGroup>

<!-- Version properties shared with the Drawing csproj (SystemPackages.props' own package items only target the library's TFMs). -->
<Import Project="..\SystemPackages.props" />
<Import Project="..\DependencyVersions.props" />

<ItemGroup Condition="'$(TargetFramework)' == 'net481'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Numerics.Vectors" Version="$(SystemNumericsVectorsVersion)" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="$(SystemRuntimeCompilerServicesUnsafeVersion)" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>

Expand Down
Loading