Skip to content

Enable trim and AOT analyzers for selected library tests#128355

Merged
MichalStrehovsky merged 10 commits into
mainfrom
copilot/fix-trim-aot-warnings
May 27, 2026
Merged

Enable trim and AOT analyzers for selected library tests#128355
MichalStrehovsky merged 10 commits into
mainfrom
copilot/fix-trim-aot-warnings

Conversation

Copilot AI commented May 19, 2026

Copy link
Copy Markdown
Contributor

Description

Several library test projects suppressed trim/AOT analyzer warnings by disabling the analyzers at the project level. This change enables the analyzers for the selected test projects where appropriate and replaces broad suppressions with targeted fixes or narrow warning pragmas at known test-only reflection and dynamic-code sites.

  • Removed the dotnet/runtime#126862 analyzer-disable blocks from the selected test projects, while leaving Microsoft.Extensions.HostFactoryResolver.Tests disabled for separate follow-up because enabling it would require product changes.
  • Replaced avoidable warnings with analyzer-friendly code, such as DiagnosticMethodInfo, Enum.GetValues<TEnum>(), LINQ-based collection conversion, and source-project references where applicable.
  • Added .NET Framework-compatible fallbacks or source-linked attributes where analyzer-friendly APIs and annotations are unavailable in net481 test builds.
  • Updated System.Threading.RateLimiting.Tests to reference the source project with SkipUseReferenceAssembly="true" and avoid local reflection suppressions.
  • Marked reflection-heavy test helpers with trim annotations where appropriate, including source generator structural equality helpers and QUIC interop reset helpers.
  • Propagated required DynamicallyAccessedMembers annotations through the WinHttpHandler test marshal shim.
  • Kept GetStateMachineData.Describe and Watchdog behavior intact, with narrow suppressions documenting that the diagnostic reflection path is dead code after runtime async.
  • Added narrow #pragma warning disable/restore scopes only for remaining emitted IL warnings that require reflection, dynamic code, or unsupported trim/AOT analysis patterns.
  • Added explanatory context to remaining targeted suppressions, including issue links or rationale for why the test-only operation is safe.

Copilot AI and others added 2 commits May 19, 2026 02:37
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5977beb1-58f4-4a75-9a1c-02ff1bb17294

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5977beb1-58f4-4a75-9a1c-02ff1bb17294

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI self-assigned this May 19, 2026
Copilot AI review requested due to automatic review settings May 19, 2026 03:06
Copilot AI removed the request for review from Copilot May 19, 2026 03:06
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/libraries/System.IO.Packaging/tests/PartPieceTests.cs Outdated
Comment thread src/libraries/System.Net.Mail/tests/Functional/MailMessageTest.cs Outdated
Comment thread src/libraries/System.Net.WebProxy/tests/WebProxyTest.cs Outdated
Comment thread src/libraries/System.Threading.RateLimiting/tests/Infrastructure/Utils.cs Outdated
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5a0b0c7b-d13e-4936-8392-ff08d859bc72

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 19, 2026 04:24
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5a0b0c7b-d13e-4936-8392-ff08d859bc72

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Comment thread src/libraries/Common/tests/System/Threading/Tasks/GetStateMachineData.cs Outdated

@MichalStrehovsky MichalStrehovsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/libraries/Common/tests/SourceGenerators/GeneratorTestHelpers.cs Outdated
Comment thread src/libraries/Common/tests/System/Threading/Tasks/GetStateMachineData.cs Outdated
Comment thread src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs Outdated
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/087bc9c5-a2a7-4eea-becd-4a9625ddce15

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 21, 2026 05:27
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/087bc9c5-a2a7-4eea-becd-4a9625ddce15

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 21, 2026 05:30
@MichalStrehovsky

Copy link
Copy Markdown
Member

@copilot build breaks

D:\a\_work\1\s\src\libraries\System.Management\tests\System\Management\ManagementObjectTests.cs(118,17): error IL2026: Using member 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream, Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. BinaryFormatter serialization is not trim compatible because the type of objects being processed cannot be statically discovered. [D:\a\_work\1\s\src\libraries\System.Management\tests\System.Management.Tests.csproj::TargetFramework=net11.0-windows]
D:\a\_work\1\s\src\libraries\System.Management\tests\System\Management\ManagementObjectTests.cs(121,54): error IL2026: Using member 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. BinaryFormatter serialization is not trim compatible because the type of objects being processed cannot be statically discovered. [D:\a\_work\1\s\src\libraries\System.Management\tests\System.Management.Tests.csproj::TargetFramework=net11.0-windows]
D:\a\_work\1\s\src\libraries\System.Management\tests\System\Management\ManagementObjectTests.cs(121,54): error IL3050: Using member 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. BinaryFormatter serialization uses dynamic code generation, the type of objects being processed cannot be statically discovered. [D:\a\_work\1\s\src\libraries\System.Management\tests\System.Management.Tests.csproj::TargetFramework=net11.0-windows]
D:\a\_work\1\s\src\libraries\System.DirectoryServices\tests\System\DirectoryServices\DirectoryServicesTests.cs(21,31): error IL2026: Using member 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. [D:\a\_work\1\s\src\libraries\System.DirectoryServices\tests\System.DirectoryServices.Tests.csproj::TargetFramework=net11.0-windows]

For the ManagementObjectTests.cs occurence, mark the method as [RequiresUnreferencedCode("Binary formatter")]

For the DirectoryServicesTests.cs, suppress the warning using #pragma. Add a comment saying "The purpose of the test is to load all types in the assembly. All types that are left is also fine.".

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5e49e977-7ef9-4b3f-ba25-f076d020cb7e

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>

Copilot AI commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

@copilot build breaks

...

Fixed in fa2c19f by adding targeted annotations/suppressions for the reported System.Management and System.DirectoryServices analyzer breaks. I verified the affected test projects build and run via /t:test with 0 warnings and 0 errors.

@MichalStrehovsky

Copy link
Copy Markdown
Member

Build breaks in the .NET framework build (TargetFramework=net481).

2026-05-21T08:19:22.2874157Z D:\a\_work\1\s\src\libraries\Microsoft.Extensions.DependencyInjection\tests\DI.External.Tests\SkippableDependencyInjectionSpecificationTests.cs(19,43): error CS0103: The name 'DiagnosticMethodInfo' does not exist in the current context [D:\a\_work\1\s\src\libraries\Microsoft.Extensions.DependencyInjection\tests\DI.External.Tests\Microsoft.Extensions.DependencyInjection.ExternalContainers.Tests.csproj::TargetFramework=net481]
2026-05-21T08:19:22.2875350Z D:\a\_work\1\s\src\libraries\Common\tests\SourceGenerators\GeneratorTestHelpers.cs(19,10): error CS0122: 'RequiresUnreferencedCodeAttribute' is inaccessible due to its protection level [D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Configuration.Binder\tests\SourceGenerationTests\Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests.csproj::TargetFramework=net481]
2026-05-21T08:19:22.2890994Z D:\a\_work\1\s\src\libraries\Common\tests\SourceGenerators\GeneratorTestHelpers.cs(19,10): error CS0122: 'RequiresUnreferencedCodeAttribute' is inaccessible due to its protection level [D:\a\_work\1\s\src\libraries\System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn3.11.Unit.Tests.csproj::TargetFramework=net481]
2026-05-21T08:19:22.2892327Z D:\a\_work\1\s\src\libraries\Common\tests\SourceGenerators\GeneratorTestHelpers.cs(19,10): error CS0122: 'RequiresUnreferencedCodeAttribute' is inaccessible due to its protection level [D:\a\_work\1\s\src\libraries\System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests.csproj::TargetFramework=net481]

Add ifdef for the DiagnosticMethodInfo, include the .cs file with the RequiresUnreferencedCodeAttribute attribute in the test project for the other one.

Also a build break on Windows x64:

src\libraries\System.Net.Http.WinHttpHandler\tests\UnitTests\FakeMarshal.cs(57,20): error IL2091: (NETCORE_ENGINEERING_TELEMETRY=Build) 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in 'System.Runtime.InteropServices.Marshal.PtrToStructure<T>(nint)'. The generic parameter 'T' of 'System.Net.Http.Marshal.PtrToStructure<T>(nint)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

For this, propagate the DynamicallyAccessedMembers annotations from the called method to the calling methods T.

@copilot

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/798bad8f-5f09-4037-886d-e162a23c8e96

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 21, 2026 22:05

Copilot AI commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Build breaks in the .NET framework build (TargetFramework=net481).

...

Fixed in ea6ff58. I added the net481 fallback around DiagnosticMethodInfo, linked the shared RequiresUnreferencedCodeAttribute source into the source-generator test projects for .NET Framework, and propagated the PtrToStructure<T> DAM annotations in the WinHttpHandler test fake. Targeted builds now pass; WinHttpHandler /t:test also passed.

Comment thread src/libraries/System.Net.Http/tests/FunctionalTests/Watchdog.cs
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/bf35c830-f126-452f-8a1b-0b1e01c449a2

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 25, 2026 03:48

Copilot AI commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • badhost
    • Triggering command: /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet exec --runtimeconfig System.Net.Http.Functional.Tests.runtimeconfig.json --depsfile System.Net.Http.Functional.Tests.deps.json /home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.26257.113/build/../tools/net/xunit.console.dll System.Net.Http.Functional.Tests.dll -xml testResults.xml -nologo -notrait category=OuterLoop -notrait category=failing 8 d-value d-variable j/native/net11.0[:upper:] 8 (dns block)
  • does.not.exist.sorry
    • Triggering command: /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet exec --runtimeconfig /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.deps.json /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/Microsoft.DotNet.RemoteExecutor.dll System.Net.Http.Functional.Tests, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 System.Net.Http.Functional.Tests.DiagnosticsTest &lt;SendAsync_ConnectionFailure_RecordsActivitiesWithCorrectErrorInfo&gt;g__RunTest|18_0 /tmp/ufcs1dmo.x5h 1.1 False dns -DNDEBUG inja -DHOST_UNIX -DNDgit -MD -MT j/native/net11.0HEAD (dns block)
    • Triggering command: /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet exec --runtimeconfig /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.deps.json /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/Microsoft.DotNet.RemoteExecutor.dll System.Net.Http.Functional.Tests, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 System.Net.Http.Functional.Tests.DiagnosticsTest &lt;SendAsync_ConnectionFailure_RecordsActivitiesWithCorrectErrorInfo&gt;g__RunTest|18_0 /tmp/mwl3z5m1.1kj 1.1 True dns -DNDEBUG 18 IBUTE_ALIGNED -Dbash d-variable in-requires-head--runtime-path GET_LINUX -DTARG/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x/home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/Microsoft.DotNet.RemoteExecutor.dll (dns block)
    • Triggering command: /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet exec --runtimeconfig /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/System.Net.Http.Functional.Tests.deps.json /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.Http.Functional.Tests/Release/net11.0-linux/Microsoft.DotNet.RemoteExecutor.dll System.Net.Http.Functional.Tests, Version=11.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 System.Net.Http.Functional.Tests.DiagnosticsTest &lt;SendAsync_ConnectionFailure_RecordsActivitiesWithCorrectErrorInfo&gt;g__RunTest|18_0 /tmp/mbnhq2lj.bmd 2.0 True dns (dns block)
  • www.some.example
    • Triggering command: /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet /home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net11.0-linux-Release-x64/dotnet exec --runtimeconfig System.Net.Http.Functional.Tests.runtimeconfig.json --depsfile System.Net.Http.Functional.Tests.deps.json /home/REDACTED/.nuget/packages/microsoft.dotnet.xunitconsoleREDACTED/2.9.3-beta.26257.113/build/../tools/net/xunit.console.dll System.Net.Http.Functional.Tests.dll -xml testResults.xml -nologo -notrait category=OuterLoop -notrait category=failing 8 d-value d-variable j/native/net11.0[:upper:] 8 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from MichalStrehovsky May 25, 2026 03:48
@sbomer sbomer self-requested a review May 27, 2026 20:53

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question, otherwise LGTM, thanks!

@MichalStrehovsky MichalStrehovsky merged commit 2a54673 into main May 27, 2026
98 checks passed
@MichalStrehovsky MichalStrehovsky deleted the copilot/fix-trim-aot-warnings branch May 27, 2026 21:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone May 28, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants