Let users opt in to analyzing generated code - #1309
Merged
Merged
Conversation
Most analyzers do not report diagnostics in generated code, which is the expected default, but there was no way for a user to ask for it. ConfigureGeneratedCodeAnalysis must be called from Initialize(AnalysisContext), where the .editorconfig options are not available, so the opt-in uses the MEZIANTOU_ANALYZER_GENERATED_CODE environment variable. Its value is OR-ed with the flags of each analyzer, so it can only add analysis: the rules that always analyze generated code, such as the Blazor ones, are unaffected and cannot be disabled with it. The analyzers now call AnalysisContextExtensions.ConfigureAnalysisOfGeneratedCode instead of ConfigureGeneratedCodeAnalysis, and BannedSymbols.txt makes sure they cannot call the latter directly. That file was not used by the compilation, so it is now added to the AdditionalFiles items.
meziantou
marked this pull request as draft
August 20, 2026 05:21
meziantou
marked this pull request as ready for review
August 20, 2026 05:52
This was referenced Aug 20, 2026
Bump Meziantou.Analyzer from 3.0.139 to 3.0.173
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#552
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.173
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#538
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
This was referenced Sep 16, 2026
Bump Meziantou.Analyzer from 3.0.139 to 3.0.259
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#574
Open
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.259
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#557
Open
Open
Open
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds an opt-in so users can have the rules analyze and report on generated code:
Unset (the default), nothing changes.
Why
Most analyzers pass
GeneratedCodeAnalysisFlags.None, which is the right default for the vast majority of projects, but some users do want the rules applied to generated code and had no way to ask for it.An
.editorconfigoption cannot express this:ConfigureGeneratedCodeAnalysismust be called fromInitialize(AnalysisContext), andAnalyzerOptionsis only reachable from the analysis callbacks, which run later. An environment variable is the only configuration that can be read early enough.How
The value is OR-ed with the flags of each analyzer, so it can only add analysis, never remove it. The 13 rules that already pass
Analyze | ReportDiagnostics— the Blazor and source-generator ones, where the generated file is the subject — are unaffected and cannot be disabled with it. There is deliberately no opt-out and no per-rule override (ConfigureGeneratedCodeAnalysisis per-analyzer, not per-diagnostic, and several analyzers expose many rule ids, so a per-rule switch would be misleading).The 166 analyzers now call
AnalysisContextExtensions.ConfigureAnalysisOfGeneratedCodeinstead ofConfigureGeneratedCodeAnalysis. The argument at each call site is unchanged — it became the minimum rather than the value.Notes for the reviewer
Two things surfaced while implementing this:
src/Meziantou.Analyzer/BannedSymbols.txtwas inert.Meziantou.NET.Sdkonly injects its ownconfiguration/BannedSymbols.txtand does not glob project-local ones, so the existingGetTypeByMetadataNameban was never enforced. It is now added toAdditionalFiles, which is what makes the new ban onConfigureGeneratedCodeAnalysisa compile error rather than a convention. There were zero pre-existing violations of the old ban.NoWarnfor the analyzer project: it wants a direct call toConfigureGeneratedCodeAnalysisinInitializeand cannot see through the extension method. The banned symbol covers the same ground more strictly. RS1035 (environment variables are banned in analyzers) needed a pragma in the new file — verified that it does fire without it.Also closed an unterminated code fence at the end of
README.md, which would otherwise have swallowed the new section.Testing
ProjectBuilder.WithGeneratedCodeAnalysis) that overrides the flags rather than setting the environment variable, because the tests run withParallelMode.Alland mutating the process state would race with the tests that compile real source-generator output..g.csfile only when the variable is set.dotnet run --project src/DocumentationGeneratorreports no change.Known gap: the environment variable to static field path itself has no in-process test, for the parallelism reason above. The parsing is covered by unit tests, the wiring by the banned symbol, and the join by the manual build.
Documentation
A section in
README.mdand a newdocs/generated-code.mdcovering what Roslyn considers generated code, the caching caveats (dotnet build-server shutdown, IDE restart, rebuild), and thegenerated_code = false.editorconfigalternative for users who want it per path and for all analyzers.