Repository navigation
Description
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
Activity
dotnet-policy-service commentedon Apr 30, 2024
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Apr 30, 2024
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Apr 30, 2024
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.
tarekgh commentedon Apr 30, 2024
@eiriktsarpalis I marked this for .NET 9.0. Let me know if you want to change that. Thanks!
teo-tsirpanis commentedon Apr 30, 2024
Maybe Roslyn would need to add support for
#pragma warning disable obsolete?eiriktsarpalis commentedon May 1, 2024
One possible approach that was considered in the past is exposing a
string[] Suppressions { get; set; }property in theJsonSourceGenerationOptionsAttributethat gives users explicit control over what suppressions should be made.eerhardt commentedon May 1, 2024
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.
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.
RikkiGibson commentedon May 1, 2024
A pseudo-diagnostic-id similar to
#pragma warning disable nullablemight 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
RikkiGibson commentedon May 1, 2024
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.32 remaining items
jaredpar commentedon Jul 3, 2024
I think it's the most promising path at this point.
eiriktsarpalis commentedon Jul 5, 2024
Moving to 10.0.0, seems unlikely we'll get to a solution in 9.
terrajobst commentedon Jul 12, 2024
@jaredpar @eiriktsarpalis should this issue be moved to dotnet/roslyn? Looks like we concluded this should be a compiler feature.
RikkiGibson commentedon Jul 12, 2024
It feels reasonable to close this out and track with dotnet/roslyn#73292. (feel free to re-triage if you don't agree.)
eerhardt commentedon Jul 14, 2024
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.terrajobst commentedon Jul 15, 2024
@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.
eerhardt commentedon Jul 15, 2024
I've updated the top comment of this issue with:
terrajobst commentedon Jul 15, 2024
Thanks man, appreciated!