Skip to content
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

Missing [RequiresUnreferencedCodeAttribute] annotation for trimming #349

Open
zeroskyx opened this issue Aug 20, 2024 · 2 comments · May be fixed by #352
Open

Missing [RequiresUnreferencedCodeAttribute] annotation for trimming #349

zeroskyx opened this issue Aug 20, 2024 · 2 comments · May be fixed by #352

Comments

@zeroskyx
Copy link

Greetings,

due to the call of this.WriteEvent, the library throws trim warnings since RequiresUnreferencedCodeAttribute is not currently propagated to the invoking methods.

Minimal reproduction

  1. Create new console app: dotnet new console
  2. Add RecycleableMemoryStream package: dotnet add package Microsoft.IO.RecyclableMemoryStream
  3. Replace contents of Program.cs with: _ = new Microsoft.IO.RecyclableMemoryStreamManager();
  4. Publish trimmed assembly, and show all trim warnings: dotnet publish -p:PublishTrimmed=true -p:EnableTrimAnalyzer=true -p:TrimmerSingleWarn=false

Expected result

The compilation and trimming concludes without any warnings

Actual result

The compilation emits trim warnings such as: /_/src/Events.cs(83,21): Trim analysis warning IL2026: Microsoft.IO.RecyclableMemoryStreamManager.Events.MemoryStreamCreated(Guid, String, Int64, Int64): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32, Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. /_/src/Events.cs(235,21): Trim analysis warning IL2026: Microsoft.IO.RecyclableMemoryStreamManager.Events.MemoryStreamDiscardBuffer(Guid, String, RecyclableMemoryStreamManager.Events.MemoryStreamBufferType, RecyclableMemoryStreamManager.Events.MemoryStreamDiscardReason, Int64, Int64, Int64, Int64, Int64, Int64): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32, Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed.

Adding [RequiresUnreferencedCodeAttribute] to all methods calling this.WriteEvent would properly forward the trim warnings, not causing the library to produce trim warnings.

@benmwatson
Copy link
Member

Thanks for reporting this. I will try to look at this and a few other issues for RMS in the next couple of weeks, unless you'd like to submit a PR.

@zeroskyx
Copy link
Author

Hi @benmwatson,

thanks for the feedback. I've opened a PR that uses a new primitive available in .NET 8.0 that fixes the problem with the potentially dynamic types.

I've added net8.0 to the project types -- you might have to add that to the nuget build pipeline.

All the best
-Simon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants