-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Mark logging source generator file as generated #53275
Mark logging source generator file as generated #53275
Conversation
Give the output file for the M.E.Logging logging classes a ".g" suffix so that it is treated as a generated file to prevent it being flagged by source analyzers for violations the application developer cannot fix for themselves.
Tagging subscribers to this area: @maryamariyan Issue DetailsGive the output file for the M.E.Logging logging classes a This is essentially the same kind of issue as I found with the Razor generator in dotnet/sdk#16777 with .NET 6 preview 3. The use case for finding this was that I was playing around with using the Logging source generator announced in the .NET 6 preview 4 blog post and initially had the The project I was trying it out with has
In my case it's easily fixed by making the
|
@chsienki - Why isn’t the output of a source generator automatically treated as a generated file? It was obviously generated if it was outputted by a source generator. |
@eerhardt There was a bit of back and forth about whether source generated files should be considered generated to analyzers but there is an issue tracking it here dotnet/roslyn#49326 |
If you want your Log class to be public, and you want all public members to have XML documentation, shouldn't you be adding the XML documentation to your side of the |
Well in this specific case I was working through trying it out and started with For my use case, the correct thing to do was to use The code I was replacing used nested private classes, but nested classes aren't supported so I had to hoist the code out into a new |
Feel free to upvote #52301. 😉 |
Just looping back here, |
@chsienki - What are all the ways we need to use to express that the file is generated? Are these listed somewhere so we can be sure we got them all? |
@eerhardt it's technically a heuristic, so I don't think we have it published anywhere, but the code that does the detection is here https://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,1a8366e77d732c39 Currently we recognize:
|
The logging generator already spits out Lines 26 to 30 in 17481fe
So I'm wondering why we need to also change the file name to end with |
@eerhardt hmm, that is not expected. let me take a look and get back to you. |
@eerhardt Hmm, I checked and However, looking at the description in the PR, I tried to repro and realized the error isn't coming from an analyzer but is actually a compiler warning about the xml doc being missing. Using |
@chsienki - thanks for the great investigation! Good to see the underlying issue getting understood here.
I actually think the proposed changes in this PR are correct - we should be generating the file with a runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs Lines 99 to 106 in b21ac01
I just wanted to fully understand what the rules were for marking a file as "generated", which you addressed above. I believe this PR can be merged now that we fully understand the situation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks @martincostello for the contribution here, and forcing the discussion.
Nobody think about writing/updating documentation for source code generators? :-) It is very tricky to write correct SCG. |
Give the output file for the M.E.Logging logging classes a
.g
suffix so that it is treated as a generated file to prevent it being flagged by source analyzers for violations the application developer cannot fix for themselves.This is essentially the same kind of issue as I found with the Razor generator in dotnet/sdk#16777 with .NET 6 preview 3.
The use case for finding this was that I was playing around with using the Logging source generator announced in the .NET 6 preview 4 blog post and initially had the
Log
class I was using defined aspublic
.The project I was trying it out with has
TreatWarningsAsErrors
set totrue
and as it is a library requires public documentation on its members. I temporarily suppressed this with a#pragma warning disable CS1591
in theLog
class, but the compilation still failed with the following error:In my case it's easily fixed by making the
Log
class beinternal
instead, but it highlights the issue that source analyzers may still cause user applications using the feature to fail to compile if the generated source does not conform to a rule required by the non-generated code.