Skip to content

[Bug]: Generated files have RequiresUnreferencedCode attributes #2200

Description

@Cheesebaron

Describe the bug 🐞

So after updating to Refit 12 and when having the following properties in the project:

<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>

The App won't build as the generated files from the interfaces have RequiresUnreferencedCodeAttribute on them and there is no good way to mark consuming code with the same attributes.

So IL2026 errors will be generated for each interface like:

Using member 'Refit.IRequestBuilder.BuildRestResultFuncForMethod(String, Type[], Type[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Building request delegates from reflected method metadata requires generic method metadata to be available at runtime. [/Users/tomaszcielecki/git/App/src/Data/Data.csproj]

So if I have interface IMyApi then the generated IMyApi.g.cs will have this error.

I am using Refit.HttpClientFactory, but doesn't seem to be related to this, as for the HttpClientFactory extension methods I can apply attributes.

Step to reproduce

  1. Enable TreatWarningsAsErrors and EnableTrimAnalyzer
  2. Implement Refit interface
  3. Build app

Reproduction repository

Expected behavior

I would love for a way to handle the trimming warning. There is no way to add [RequiresUnreferencedCode] attribute on the interface, as it is only allowed on class, ctor and methods.

The only way I can handle the warning is to disable WarningsAsErrors on IL2026, which isn't ideal. I did try to add a rule to .editorconfig but it doesn't apply to these files as they are only spat out in obj folder.

Screenshots 🖼️

No response

IDE

No response

Operating system

No response

Version

No response

Device

No response

Refit Version

12.1.0+

Additional information ℹ️

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions