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

CS0612 in generated code cannot be supressed in editorconfig #69298

Closed
hankovich opened this issue Jul 31, 2023 · 10 comments
Closed

CS0612 in generated code cannot be supressed in editorconfig #69298

hankovich opened this issue Jul 31, 2023 · 10 comments
Labels
Area-Compilers Resolution-Duplicate The described behavior is tracked in another issue untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@hankovich
Copy link

Version Used: .NET 8 preview 6

Steps to Reproduce:

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

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

  <ItemGroup>
    <PackageReference Include="Riok.Mapperly" Version="2.8.0" />
  </ItemGroup>

</Project>
using Riok.Mapperly.Abstractions;

Console.WriteLine(new Source().MapToTarget());

internal sealed class Source
{
    [Obsolete]
    public int Id { get; set; }
}

internal sealed class Target
{
    public int Id { get; set; }
}

[Mapper]
internal static partial class Mapper
{
    public static partial Target MapToTarget(this Source source);
}
root = true

[*.cs]

# CS0612: Member is obsolete
dotnet_diagnostic.CS0612.severity = none

This code id generated by Riok.Mapperly

#nullable enable
internal static partial class Mapper
{
    public static partial global::Target MapToTarget(this global::Source source)
    {
        var target = new global::Target();
        target.Id = source.Id; // <- a diagnostic is reported here
        return target;
    }
}

Diagnostic Id: CS0612

Expected Behavior:

No CS0612 diagnostic is reported.

Actual Behavior:

A diagnostic C:\Users\Hello\source\repos\ConsoleApp2\ConsoleApp2\Riok.Mapperly\Riok.Mapperly.MapperGenerator\Mapper.g.cs(7,21,7,30): warning CS0612: 'Source.Id' is obsolete is reported.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 31, 2023
@sharwell
Copy link
Member

Does it work to put [Obsolete] on the partial definition?

[Mapper]
internal static partial class Mapper
{
    [Obsolete]
    public static partial Target MapToTarget(this Source source);
}

@hankovich
Copy link
Author

Yes, the warning is gone.

@sharwell
Copy link
Member

This issue is technically a duplicate of #47384 (.editorconfig does not apply to source generated files because source generated files are not considered part of the directory tree on disk).

@sharwell
Copy link
Member

sharwell commented Jul 31, 2023

Also related to riok/mapperly#284

I believe the approach I would most recommend is having Riok.Mapperly emit a #pragma warning suppress CS0618 around the property access for the specific case where MapperIgnoreObsoleteMembers has been explicitly provided. In other words, if the code has been explicitly instructed to access an obsolete property during the mapping process, it should suppress warnings resulting from that access.

I couldn't find a pre-existing bug in https://github.com/riok/mapperly for this, so it would be good to file a feature request for it.

@jaredpar
Copy link
Member

jaredpar commented Aug 1, 2023

Closing as the dupe

@jaredpar jaredpar closed this as completed Aug 1, 2023
@latonz
Copy link

latonz commented Aug 1, 2023

@sharwell I‘m looking into the related Mapperly issue. I had in mind that compiler warnings are suppressed for generated files (with a <auto-generated /> comment). I couldn‘t find any documentation on this topic. Are there diagnostics which are suppressed for generated files? Is there any documentation available on this topic?

@sharwell
Copy link
Member

sharwell commented Aug 3, 2023

I had in mind that compiler warnings are suppressed for generated files (with a <auto-generated /> comment).

Compiler warnings are not affected by an <auto-generated/> comment. Analyzer warnings may be affected by such a comment if they opt-in to this behavior (by calling ConfigureGeneratedCodeAnalysis without specifying GeneratedCodeAnalysisFlags.ReportDiagnostics).

My recommendation for this issue is to suppress CS0618 only around specific property accesses, and not for the entire file. In addition, this should only occur if the user has explicitly used MapperIgnoreObsoleteMembers.

@sharwell sharwell added the Resolution-External The behavior lies outside the functionality covered by this repository label Aug 3, 2023
@sharwell
Copy link
Member

sharwell commented Aug 3, 2023

Duplicate of #47384

@sharwell sharwell marked this as a duplicate of #47384 Aug 3, 2023
@sharwell sharwell closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
@sharwell sharwell added Resolution-Duplicate The described behavior is tracked in another issue and removed Resolution-External The behavior lies outside the functionality covered by this repository labels Aug 3, 2023
@latonz
Copy link

latonz commented Aug 4, 2023

@sharwell thank you for these explanations, we'll look into it.

@rabberbock
Copy link

An interesting approach that worked for me was to suppress all warnings in a .globalconfig, which includes generated files. Then you can override it in the .editorconfig and set it back to warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-Duplicate The described behavior is tracked in another issue untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

5 participants