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

[generator] Mark generated .cs files as such so Roslyn analyzers can choose to ignore them. #1052

Merged
merged 1 commit into from
Oct 20, 2022

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Oct 18, 2022

Fixes: #1051
Context: https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.diagnostics.analysiscontext.configuregeneratedcodeanalysis

Roslyn analyzers specify whether they want to scan generated code or not using GeneratedCodeAnalysisFlags. We should mark the .cs files that generator creates in the format that Roslyn understands so that these analyzers can ignore them.

This should allow slightly faster builds with fewer warnings. Additionally, it should help make it clear to users that these files should not be modified by hand. (e.g. dotnet/android#7449)

Note that by default Roslyn disables NRT for code marked as "generated", causing:

Error CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Auto-generated code requires an explicit '#nullable' directive in source.

We have to explicitly restore NRT back to the project settings for our generated files with:

#nullable restore

@jonpryor
Copy link
Member

Related: https://github.com/dotnet/roslyn/blob/3f694a3c39c44b5dccb4aaaa163080bdb3ad412f/src/Compilers/Core/Portable/InternalUtilities/GeneratedCodeUtilities.cs

Apparently all the current analyzer needs to determine if a file is generated is the string <autogenerated or <auto-generated.

@jpobst jpobst marked this pull request as ready for review October 18, 2022 21:59
@jpobst jpobst changed the title [generator] Mark generated .cs files as such so Roslyn analyzers aren't as strict. [generator] Mark generated .cs files as such so Roslyn analyzers can choose to ignore them. Oct 20, 2022
@jonpryor jonpryor merged commit 1720628 into main Oct 20, 2022
@jonpryor jonpryor deleted the autogenerated branch October 20, 2022 15:31
jonpryor pushed a commit to dotnet/android that referenced this pull request Nov 1, 2022
Fixes: dotnet/java-interop#1034
Fixes: dotnet/java-interop#1051

Context: 938b2cb

Changes: dotnet/java-interop@e1ee4b1...5318261

  * dotnet/java-interop@53182615: [build] Update Microsoft.* NuGet package versions (dotnet/java-interop#1055)
  * dotnet/java-interop@8e18c909: [generator] Avoid C#11 delegate cache overhead. (dotnet/java-interop#1053)
  * dotnet/java-interop@2d8b6d24: [generator] More AttributeTargets on SupportedOSPlatformAttribute (dotnet/java-interop#1054)
  * dotnet/java-interop@7dfbab67: [generator] Add [SupportedOSPlatform] to bound constant fields (dotnet/java-interop#1038)
  * dotnet/java-interop@1720628a: [generator] Mark generated .cs files as generated (dotnet/java-interop#1052)
  * dotnet/java-interop@f498fcf5: [Java.Interop] Avoid some method group conversions (dotnet/java-interop#1050)
  * dotnet/java-interop@16e1ecd4: [build] Use $(VSINSTALLDIR), not $(VSINSTALLROOT) (dotnet/java-interop#1048)
  * dotnet/java-interop@8e4c7d20: [Hello-Core] Add "low level" sample. (dotnet/java-interop#1047)

Additionally, remove `$(LangVersion)`=10 from `Mono.Android.csproj`,
which was a hack to work around a size regression due to delegate
caching in C# 11; see also 938b2cb, dotnet/java-interop@8e18c909.

Co-authored-by: Jonathan Pobst <jonathan.pobst@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated files (e.g, Java.Interop.__TypeRegistrations.cs) should mark themselves as auto-generated
2 participants