Skip to content

Commit 13dbfdc

Browse files
eerhardtlukebakken
authored andcommitted
Follow up to trimming changes for 6.x
The allows the 6.x library to be used in trimmed and native AOT'd applications without any warnings. Since the 6.x branch doesn't target net6.0+, it only targets netstandard2.0 and net462, the #if NET6_0_OR_GREATER checks don't do anything. To resolve this issue, and copy the trimming attributes into this library following the recommendation at https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/#approach-2-define-the-attributes-internally. This allows the library to apply the attributes without targeting net6.0+. Also moving DebugUtil to the test project - porting #1009 from the main branch. Contributes to #1410 Add AotCompatibility.TestApp Add PS1 to run test app Add AOT test to windows GHA
1 parent 55010ab commit 13dbfdc

13 files changed

+351
-23
lines changed

.github/workflows/main.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
run: dotnet build --no-restore --verbosity=normal
5050
- name: Test
5151
run: ./.ci/gha-run-tests.ps1
52+
- name: AotTest
53+
run: ./projects/AotCompatibility.TestApp/test-aot-compatibility.ps1
5254

5355
build:
5456
name: build/test on ubuntu-latest

RabbitMQDotNetClient.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestApplications", "TestApp
2020
EndProject
2121
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OAuth2", "projects\TestApplications\OAuth2\OAuth2.csproj", "{07E203AC-9E4B-4BED-9445-E2B45E10E412}"
2222
EndProject
23+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AotCompatibility.TestApp", "projects\AotCompatibility.TestApp\AotCompatibility.TestApp.csproj", "{0B79BD0B-B35D-4626-ABCC-023B6726A531}"
24+
EndProject
2325
Global
2426
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2527
Debug|Any CPU = Debug|Any CPU
@@ -46,6 +48,10 @@ Global
4648
{07E203AC-9E4B-4BED-9445-E2B45E10E412}.Debug|Any CPU.Build.0 = Debug|Any CPU
4749
{07E203AC-9E4B-4BED-9445-E2B45E10E412}.Release|Any CPU.ActiveCfg = Release|Any CPU
4850
{07E203AC-9E4B-4BED-9445-E2B45E10E412}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{0B79BD0B-B35D-4626-ABCC-023B6726A531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{0B79BD0B-B35D-4626-ABCC-023B6726A531}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{0B79BD0B-B35D-4626-ABCC-023B6726A531}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{0B79BD0B-B35D-4626-ABCC-023B6726A531}.Release|Any CPU.Build.0 = Release|Any CPU
4955
EndGlobalSection
5056
GlobalSection(SolutionProperties) = preSolution
5157
HideSolutionNode = FALSE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<!-- When targeting net7.0+, this can be updated to PublishAot. -->
8+
<PublishTrimmed>true</PublishTrimmed>
9+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<TrimmerRootAssembly Include="RabbitMQ.Client" />
14+
<TrimmerRootAssembly Update="@(TrimmerRootAssembly)" Path="..\%(Identity)\%(Identity).csproj" />
15+
<ProjectReference Include="@(TrimmerRootAssembly->'%(Path)')" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// publishing this app ensures all of the code in the referenced
2+
// assemblies are trim/AOT compatible.
3+
4+
Console.WriteLine("Hello, World!");
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
$DebugPreference = "Continue"
2+
$ErrorActionPreference = 'Stop'
3+
# Set-PSDebug -Strict -Trace 1
4+
Set-PSDebug -Off
5+
Set-StrictMode -Version 'Latest' -ErrorAction 'Stop' -Verbose
6+
7+
New-Variable -Name rootDirectory -Option Constant -Value $PSScriptRoot
8+
Write-Host "[INFO] rootDirectory: $rootDirectory"
9+
10+
$runtime = $IsWindows ? "win-x64" : ($IsMacOS ? "macos-x64" : "linux-x64")
11+
$app = $IsWindows ? "./AotCompatibility.TestApp.exe" : "./AotCompatibility.TestApp"
12+
13+
$publishOutput = dotnet publish --runtime=$runtime $rootDirectory/AotCompatibility.TestApp.csproj -nodeReuse:false '/p:UseSharedCompilation=false' '/p:Configuration=Release'
14+
15+
$actualWarningCount = 0
16+
17+
foreach ($line in $($publishOutput -split "`r`n"))
18+
{
19+
if (($line -like "*analysis warning IL*") -or ($line -like "*analysis error IL*"))
20+
{
21+
Write-Host $line
22+
$actualWarningCount += 1
23+
}
24+
}
25+
26+
Write-Host "Actual warning count is:", $actualWarningCount
27+
$expectedWarningCount = 0
28+
29+
if ($LastExitCode -ne 0)
30+
{
31+
Write-Error -ErrorAction Continue -Message "[ERROR] error while publishing AotCompatibility Test App, LastExitCode is $LastExitCode"
32+
Write-Error -ErrorAction Continue -Message $publishOutput
33+
}
34+
35+
Push-Location "$rootDirectory/bin/Release/net6.0/$runtime"
36+
try
37+
{
38+
Write-Host "[INFO] executing: $app"
39+
$app
40+
Write-Host "[INFO] finished executing test app"
41+
42+
if ($LastExitCode -ne 0)
43+
{
44+
Write-Error -ErrorAction Continue -Message "[ERROR] there was an error while executing AotCompatibility Test App. LastExitCode is: $LastExitCode"
45+
}
46+
}
47+
finally
48+
{
49+
Pop-Location
50+
}
51+
52+
$exitCode = 0
53+
if ($actualWarningCount -ne $expectedWarningCount)
54+
{
55+
$exitCode = 1
56+
Write-Error -ErrorAction Continue -Message "Actual warning count: $actualWarningCount is not as expected, which is: $expectedWarningCount"
57+
}
58+
59+
Exit $exitCode

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
66
<NoWarn>$(NoWarn);CS1591</NoWarn>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
8-
<EnableTrimAnalyzer Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</EnableTrimAnalyzer>
9-
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
108
<AssemblyTitle>RabbitMQ Client Library for .NET</AssemblyTitle>
119
<Authors>VMware</Authors>
1210
<Company>VMware, Inc. or its affiliates.</Company>

projects/RabbitMQ.Client/client/api/ICredentialsRefresher.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
using System;
3333
using System.Collections.Concurrent;
34+
using System.Diagnostics.CodeAnalysis;
3435
using System.Diagnostics.Tracing;
3536
using System.Timers;
3637

@@ -54,16 +55,12 @@ public class TimerBasedCredentialRefresherEventSource : EventSource
5455
[Event(2)]
5556
public void Unregistered(string name) => WriteEvent(2, "UnRegistered", name);
5657
[Event(3)]
57-
#if NET6_0_OR_GREATER
5858
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
59-
#endif
6059
public void ScheduledTimer(string name, double interval) => WriteEvent(3, "ScheduledTimer", name, interval);
6160
[Event(4)]
6261
public void TriggeredTimer(string name) => WriteEvent(4, "TriggeredTimer", name);
6362
[Event(5)]
64-
#if NET6_0_OR_GREATER
6563
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Parameters to this method are primitive and are trimmer safe")]
66-
#endif
6764
public void RefreshedCredentials(string name, bool succesfully) => WriteEvent(5, "RefreshedCredentials", name, succesfully);
6865
[Event(6)]
6966
public void AlreadyRegistered(string name) => WriteEvent(6, "AlreadyRegistered", name);

projects/RabbitMQ.Client/client/logging/RabbitMqClientEventSource.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33+
using System.Diagnostics.CodeAnalysis;
3334
using System.Diagnostics.Tracing;
3435

3536
namespace RabbitMQ.Client.Logging
@@ -66,19 +67,14 @@ public void Warn(string message)
6667
public void Error(string message, RabbitMqExceptionDetail ex)
6768
{
6869
if (IsEnabled())
69-
{
70-
#if NET6_0_OR_GREATER
7170
WriteExceptionEvent(message, ex);
71+
}
7272

73-
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The properties are preserved with the DynamicallyAccessedMembers attribute.")]
74-
void WriteExceptionEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string message, T ex)
75-
{
76-
WriteEvent(3, message, ex);
77-
}
78-
#else
79-
WriteEvent(3, message, ex);
80-
#endif
81-
}
73+
[NonEvent]
74+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The properties are preserved with the DynamicallyAccessedMembers attribute.")]
75+
private void WriteExceptionEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string message, T ex)
76+
{
77+
WriteEvent(3, message, ex);
8278
}
8379

8480
[NonEvent]

0 commit comments

Comments
 (0)