Skip to content

Unify RequiresUnreferencedCode and RequiresAssemblyFiles attributes #55274

Closed

Description

@vitek-karas

Background and Motivation

Currently the shape of these attributes is like:

class RequiresUnreferencedCodeAttribute : Attribute
{
    public RequiresUnreferencedCodeAttribute(string message);
    public string Message { get; }
    public string Url { get; set; }
}

class RequiresAssemblyFilesAttribute : Attribute
{
    public RequiresAssemblyFilesAttribute();
    public string Message { get; set; }
    public string Url { get; set; }
}

The two attributes are very similar in their usage, they annotate a certain method as problematic when used with trimming or single-file respectively. But their usage is different:

[RequiresUnreferencedCode("This method doesn't play nice with trimming")]
[RequiresAssemblyFiles(Message = "This method doesn't play nice with single-file either")]
void ReallyProblematicMethod() {}

The first attribute makes the Message mandatory since it's a constructor parameter which can't be omitted. The second attribute makes Message optional and doesn't provide a way to specify it in the constructor.

From UX perspective we want people to specify the message if they at all can, since without it the produced warnings are not very actionable. It would also be good for these two attributes to have consistent usage patterns.

Proposed API

We can't really change RequiresUnreferencedCode since that already shipped in .NET 5. But we can adapt RequiresAssemblyFiles to match its behavior:

namespace System.Diagnostics.CodeAnalysis;

class RequiresAssemblyFilesAttribute : Attribute
{
-    public RequiresAssemblyFilesAttribute();
+    public RequiresAssemblyFilesAttribute(string message);
-    public string Message { get; set; }
+    public string Message { get; }
    public string Url { get; set; }
}

If we think that message should be optional, then keep the parameter less constructor as well.

Usage Examples

[RequiresUnreferencedCode("This method doesn't play nice with trimming")]
[RequiresAssemblyFiles("This method doesn't play nice with single-file either")]
void ReallyProblematicMethod() {}

Risks

As proposed it's a breaking change compared to previously released .NET 6 Previews. We don't expect this attribute to be used by many people yet.

There's associated cost in the "breaking" change that we would need to fix all of the occurrences of this attribute in the framework (less than 100) and that the Roslyn analyzer for this attribute will need to react to the change.

Activity

  1. added
    api-suggestion
    on Jul 7, 2021
  2. ghost added
    untriaged
    on Jul 7, 2021
  3. vitek-karas commented on Jul 7, 2021

    @vitek-karas
    MemberAuthor
  4. added this to the 6.0.0 milestone on Jul 7, 2021
  5. added
    blocking
    on Jul 7, 2021
  6. eerhardt commented on Jul 7, 2021

    @eerhardt
    Member

    Looking across our code base, I only see 2 places that don't set a message, and they are both in a class that derives from Assembly.

    [Obsolete]
    [RequiresAssemblyFiles]
    #endif
    public override string? CodeBase

    [Obsolete]
    [RequiresAssemblyFiles]
    #endif
    public override string EscapedCodeBase

    I agree that we should force a Message here. If a dev needs to apply this attribute to an API, they should give an indication about why this API requires "assembly files" and what callers can do to mitigate it if they need to work in single-file apps.

    From the original API review I see:

    We shouldn't take a message because they are hard to change

    Which I don't fully understand. cc @terrajobst

  7. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Jul 7, 2021
  8. tlakollo commented on Jul 7, 2021

    @tlakollo
    Contributor

    Explaining the original api review, the premise was that we prefer having a url over having a message. If we have a good url that explains in detail then we don't need the message therefore it was optional. But since url is not always available that was also optional.

  9. tlakollo commented on Jul 7, 2021

    @tlakollo
    Contributor

    Although something that wasn't discussed in the original RequiresAssemblyFiles API review was the actual implementation, so far we don't embed any URL information in the RequiresAssemblyFiles attribute and instead, we put a help URL in the analyzer diagnostic descriptor
    The way I see it, I would only use the URL field in the attribute to go in-depth on why that specific member RequiresAssemblyFiles vs the description provided in the helpurl

  10. vitek-karas commented on Jul 7, 2021

    @vitek-karas
    MemberAuthor

    I could be persuaded that message should be optional - in which case we should make it optional on RequiresUnreferencedCode as well (can be done without breaking anything). My main concern is that the attribute are very similar in function, but the usage of them is at least on the surface very different.

  11. eerhardt commented on Jul 7, 2021

    @eerhardt
    Member

    I definitely don't think RequiresUnreferencedCode should be applied without a message. Applying that basically means "you can never use this API in a trimmed or NativeAOT app. And you are on your own for figuring out why and what to use instead".

    the premise was that we prefer having a url over having a message

    so far we don't embed any URL information in the RequiresAssemblyFiles attribute and instead, we put a help URL in the analyzer diagnostic descriptor

    If we really believe that RequiresAssemblyFiles doesn't need a message because of this, I think that is enough reasoning to explain why the 2 attributes have different shaped APIs:

    • RequiresAssemblyFiles has a decent help URL by default, thus a custom message isn't necessary
    • RequiresUnreferencedCode does NOT have a decent help URL, because there are so many different reasons it could be applied. Thus it requires a custom message.

    In that case, I would take the Alternative solution, but with a slight modification and remove the Message setter:

    namespace System.Diagnostics.CodeAnalysis;
    
    class RequiresAssemblyFilesAttribute : Attribute
    {
        public RequiresAssemblyFilesAttribute();
    +    public RequiresAssemblyFilesAttribute(string message);
    -    public string Message { get; set; }
    +    public string Message { get; }
        public string Url { get; set; }
    }
  12. vitek-karas commented on Jul 7, 2021

    @vitek-karas
    MemberAuthor

    I wanted to keep the message setter in the alternative solution basically just so that it doesn't break any existing usage... but we could go with your proposal as well.

  13. eerhardt commented on Jul 7, 2021

    @eerhardt
    Member

    It's new in net6.0, right? We might as well make it "right" from the start.

    Having a ctor parameter and a setter is super confusing:

    [RequiresAssemblyFiles("A message", Message = "A Different Message")]
    void Foo() {}
  14. vitek-karas commented on Jul 8, 2021

    @vitek-karas
    MemberAuthor

    OK- makes sense. I updated the proposal.

  15. terrajobst commented on Jul 13, 2021

    @terrajobst
    Contributor
    • We're happy with the default message most of the time, so we shouldn't remove the parameterless constructor.
    namespace System.Diagnostics.CodeAnalysis
    {
        public sealed class RequiresAssemblyFilesAttribute : Attribute
        {
            public RequiresAssemblyFilesAttribute();
            public RequiresAssemblyFilesAttribute(string message);
            public string Message { get; }
            public string Url { get; set; }
        }
    }
  16. added
    api-approved
    and removed
    api-ready-for-review
    blocking
    on Jul 13, 2021
  17. ghost added
    in-pr
    on Jul 14, 2021
  18. ghost removed
    in-pr
    on Jul 14, 2021
  19. ghost locked as resolved and limited conversation to collaborators on Aug 13, 2021
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

    api-approved

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions