Skip to content

Obsoletions with a DiagnosticId still get warnings in generated code #101738

Open

Description

@eerhardt

Description

This issue is tracking updating the System.Text.Json and ConfigurationBinder source generators to take advantage of dotnet/roslyn#73292 when it is implemented.

Original Description

When the System.Text.Json and ConfigurationBinder source generators generate code against types that have properties with [Obsolete("message", DiagnosticId = "ID01")] attributes, the generated code is emitting warnings that I can't suppress and can't control. The only thing I can do is suppress the warning globally, which isn't ideal because I still want those warnings for my "hand written" code.

Both of those generators already suppress "normal" obsoletions with #pragma warning disable CS0612, CS0618. But since these obsoletion warnings get their own DiagnosticID, those suppressions don't work.

(Note the case I hit was with the Configuration Binder source generator against a class that had an X509Certificate2 property - which has 3 properties with different DiagnosticIDs - SYSLIB0026;SYSLIB0027;SYSLIB0028.)

Reproduction Steps

dotnet build the following project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
  </ItemGroup>

</Project>
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Text.Json.Serialization;

var builder = Host.CreateApplicationBuilder(args);

var c = new MyClass();
builder.Configuration.Bind(c);

[JsonSerializable(typeof(MyClass))]
public partial class JsonContext : JsonSerializerContext
{
}

public class MyClass
{
    [Obsolete("message", DiagnosticId = "EE01")]
    public string? Name { get; set; }
}

Expected behavior

I shouldn't get warnings that I can't take action against.

Actual behavior

I get the following warnings:

\ConsoleApp119\obj\Debug\net8.0\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(65,17): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(53,36): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(54,45): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]
\ConsoleApp119\obj\Debug\net8.0\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\JsonContext.MyClass.g.cs(80,43): warning EE01: 'MyClass.Name' is obsolete: 'message' [C:\Users\eerhardt\source\repos\ConsoleApp119\ConsoleApp119\ConsoleApp119.csproj]

Regression?

No response

Known Workarounds

globally <NoWarn> these warnings.

Configuration

No response

Other information

cc @ericstj @eiriktsarpalis @tarekgh

Activity

  1. dotnet-policy-service commented on Apr 30, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
    See info in area-owners.md if you want to be subscribed.

  2. added
    source-generator
    on Apr 30, 2024
  3. added this to the 9.0.0 milestone on Apr 30, 2024
  4. dotnet-policy-service commented on Apr 30, 2024

    @dotnet-policy-service
    Contributor

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

  5. dotnet-policy-service commented on Apr 30, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
    See info in area-owners.md if you want to be subscribed.

  6. tarekgh commented on Apr 30, 2024

    @tarekgh
    Member

    @eiriktsarpalis I marked this for .NET 9.0. Let me know if you want to change that. Thanks!

  7. teo-tsirpanis commented on Apr 30, 2024

    @teo-tsirpanis
    Contributor

    Maybe Roslyn would need to add support for #pragma warning disable obsolete?

  8. eiriktsarpalis commented on May 1, 2024

    @eiriktsarpalis
    Member

    One possible approach that was considered in the past is exposing a string[] Suppressions { get; set; } property in the JsonSourceGenerationOptionsAttribute that gives users explicit control over what suppressions should be made.

  9. eerhardt commented on May 1, 2024

    @eerhardt
    MemberAuthor

    exposing a string[] Suppressions { get; set; } property in the JsonSourceGenerationOptionsAttribute

    One concern about that is we would need to do that for every source generator that gets/sets user defined properties. And also there are some source generators (like the ConfigurationBinder) that don't have an "Options" API where these things could be placed.

    Maybe Roslyn would need to add support for #pragma warning disable obsolete?

    This seems like an interesting idea. A blanket "suppress obsoletions" statement.

    cc @RikkiGibson - who implemented Customizable Obsolete diagnostics (dotnet/roslyn#42518)

    With other Roslyn analyzers, you can opt-in/out of running the analyzer on generated code. But since this one is built into Roslyn itself, I'm not sure how to tell it stop.

  10. RikkiGibson commented on May 1, 2024

    @RikkiGibson
    Member

    A pseudo-diagnostic-id similar to #pragma warning disable nullable might be the right thing here. Please file an issue on roslyn.

    One rather ugly workaround here might be to put the usages of the "custom obsolete" members in an obsolete context, which does have the effect of blanket suppressing obsolete diagnostics. (seems like this also shows that compiler knows how to blanket suppress the diagnostics already.) SharpLab

    using System;
    
    public class User
    {
        [Obsolete(DiagnosticId = "A")]
        public string? Prop { get; set; }
    }
    
    [Obsolete]
    public class Gen {
        public void M(User user) {
            user.Prop = "B";
        }
    }
  11. RikkiGibson commented on May 1, 2024

    @RikkiGibson
    Member

    BTW, it's also possible to disable all warnings using #pragma warning disable. But, this might be considered undesirable if it hides bugs with the generated code itself.

  12. 32 remaining items

  13. jaredpar commented on Jul 3, 2024

    @jaredpar
    Member

    I think it's the most promising path at this point.

  14. modified the milestones: 9.0.0, 10.0.0 on Jul 5, 2024
  15. eiriktsarpalis commented on Jul 5, 2024

    @eiriktsarpalis
    Member

    Moving to 10.0.0, seems unlikely we'll get to a solution in 9.

  16. terrajobst commented on Jul 12, 2024

    @terrajobst
    Contributor

    @jaredpar @eiriktsarpalis should this issue be moved to dotnet/roslyn? Looks like we concluded this should be a compiler feature.

  17. RikkiGibson commented on Jul 12, 2024

    @RikkiGibson
    Member

    It feels reasonable to close this out and track with dotnet/roslyn#73292. (feel free to re-triage if you don't agree.)

  18. eerhardt commented on Jul 14, 2024

    @eerhardt
    MemberAuthor

    Note that once dotnet/roslyn#73292 is implemented, we will still need to make the small change in the JSON and Config Binder source generators to take advantage of the new feature - by adding #pragma warning disable obsolete (or whatever is decided) to the generated code.

  19. terrajobst commented on Jul 15, 2024

    @terrajobst
    Contributor

    @eerhardt presumably you're suggesting to use this issue to track the needed changes for that? If so, it would be good to update the title / description to reflect this.

  20. eerhardt commented on Jul 15, 2024

    @eerhardt
    MemberAuthor

    I've updated the top comment of this issue with:

    This issue is tracking updating the System.Text.Json and ConfigurationBinder source generators to take advantage of dotnet/roslyn#73292 when it is implemented.

  21. terrajobst commented on Jul 15, 2024

    @terrajobst
    Contributor

    Thanks man, appreciated!

  22. modified the milestones: 10.0.0, Future on Jul 16, 2025
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

    area-Extensions-Configurationarea-System.Text.Jsonsource-generator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions