-
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
Generated code should include [GeneratedCodeAttribute] and // <auto-generated/> #64541
Comments
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsLogging, Interop, Regex and JSON generators include Logging, Regex and JSON generators include something like After #64534, all 5 of them use @jaredpar is it reasonable that all three of these should be required by convention in generators? I'm not sure what effect each one has. For example, we discovered that coverlet looks for
|
That seems like a bug in coverlet, not something we should be designing our generators around.
This has no impact on code that I am aware of. |
I agree. In the case of #64534 I was fine "fixing" it because we already use ".g.cs" everywhere else and this was the one outlier, with no good reason, so changing it made it more consistent with little downside. But if coverlet actually breaks if someone uses a different suffix (or no suffix) with a source generator, that certainly seems like a coverlet bug.
From my perspective, the |
Does VS respect both Coverlet aside, consistency is good; we should try to be consistent across our own generators, and if these two annotations have value, we should likely recommend them as well. @dotnet/interop-contrib do you want me to open a specific issue for your generator to include GeneratedCodeAttribute, like the others? |
Can you be more specefic in what you expect from VS here? There are many layers of VS and, unfortunately, there are differing heuristics for detecting and different responses to generated code |
I meant "are there components of VS that look for one but not the other, such that it is necessary to apply both for all components to identify generated code"? For example, MSBuild writes XX.AssemblyInfo.cs with //------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ but no Yes, I wasn't intending to suggest we should cater to Coverlet's bugs. The extension would purely have the value of consistency for humans. |
Yes. Across VS there are different heuristics for identifying generated code. The one we are trying to standardize on is the compiler heuristic around generated code. This takes into account several popular existing hueristics, including extensions. That is genenerally not a great approach. The one we'd prefer is that people have the |
We have been discussing this in our team meeting. Adding a tracking issue would be helpful and adding it to the productization issue - #60595. |
@stephentoub are you aware of tools in the ecosystem that only respect |
Anything that only has access to binaries / metadata / reflection and not source, e.g. |
(In particular, I think it's important for source generated code to include |
@jaredpar if you agree, where should guidance go - in https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview? and perhaps samples? |
I only came across the |
Mark C# file contents as auto-generated code. ref. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options#exclude-generated-code > .NET code analyzer warnings aren't useful on generated code files, such as designer-generated files, which users can't edit to fix any violations. > In most cases, code analyzers skip generated code files and don't report violations on these files. ref. dotnet/runtime#64541 ref. dotnet/runtime#64541 (comment) > From my perspective, the `<auto-generated/>` is valuable in source to highlight to someone reading the source that trying to change it will be futile as it will be regenerated, > the `.g.cs` suffix is valuable for someone looking at files on disk to know what's been created by a tool rather than by hand, and `[GeneratedCode]` is useful for tools that inspect metadata. ref. dotnet/runtime#64541 (comment) > The one we are trying to standardize on is the compiler heuristic around generated code. > > https://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,1c5dbec99946c19e,references > > This takes into account several popular existing hueristics, including extensions. That is genenerally not a great approach. > The one we'd prefer is that people have the `<auto-generated>` header. Also explicitly enable nullable annotations, since auto-generated C# code is nullable-oblivious by default. ref. https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts > **Important** > > The global nullable context does not apply for generated code files. Under either strategy,> the nullable context is disabled for any source file marked as generated. > **This means any APIs in generated files are not annotated.** Taken together, this should avoid spurious warnings in consuming apps, and help verify that nullable annotations are accurately generated: ``` src/generated/Generated_CS.cs(77,68): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. src/generated/Generated_CS.cs(153,22): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. etc. ```
Logging, Interop, Regex and JSON generators include
// <auto-generated/>
at the top of their emitted .g.cs.EventSource generator does not.
Logging, Regex and JSON generators include something like
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "42.42.42.42")]
EventSource and Interop generators do not.
After #64534, all 5 of them use
.g.cs
extensions when writing to disk.@jaredpar is it reasonable that all three of these should be required by convention in generators? I'm not sure what effect each one has. For example, we discovered that coverlet looks for
.g.cs
.The text was updated successfully, but these errors were encountered: