-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add support for reporting analyzer diagnostics in editorconfig files #64213
Comments
ProposalFeature motivationThis would help us address the open question mentioned in #44131 (comment) and finally help us notify users about mistakes in editorconfig files, both in CI and in the IDE:
We have repeatedly received github issues from users for incorrect entries added to editorconfig, so this will alleviate this pain point. We also have a story board for potential future items that can be implemented if this feature is implemented: .editorconfig Validation During Compilation DesignPublic API surface for analyzer config file actions would be analogous to AdditionalFile actions and SyntaxTree actions. Approved API for AdditionalFile actions is in this comment: #44131 (comment).
// Existing API for AdditionalFile analysis context
public readonly struct AdditionalFileAnalysisContext
{
public AdditionalText AdditionalFile { get; }
public AnalyzerOptions Options { get; }
public CancellationToken CancellationToken { get; }
public Compilation Compilation { get; }
public void ReportDiagnostic(Diagnostic diagnostic);
}
// New API for AnalyzerConfigFile analysis context
public readonly struct AnalyzerConfigFileAnalysisContext
{
public AdditionalText AnalyzerConfigFile { get; }
public AnalyzerOptions Options { get; }
public CancellationToken CancellationToken { get; }
public Compilation Compilation { get; }
public void ReportDiagnostic(Diagnostic diagnostic);
}
// Existing API for registering callbacks for additional files
public void RegisterAdditionalFileAction(Action<AdditionalFileAnalysisContext> action);
// New API for registering callbacks for editorconfig files
public void RegisterAnalyzerConfigFileAction(Action<AnalyzerConfigFileAnalysisContext> action);
public class AnalyzerOptions
{
// Existing API
public ImmutableArray<AdditionalText> AdditionalFiles { get; }
// New API
public ImmutableArray<AdditionalText> AnalyzerConfigFiles { get; }
// Existing constructors
public AnalyzerOptions(ImmutableArray<AdditionalText> additionalFiles);
public AnalyzerOptions(ImmutableArray<AdditionalText> additionalFiles, AnalyzerConfigOptionsProvider optionsProvider);
// New constructors
public AnalyzerOptions(ImmutableArray<AdditionalText> additionalFiles, ImmutableArray<AdditionalText> analyzerConfigFiles);
public AnalyzerOptions(ImmutableArray<AdditionalText> additionalFiles, ImmutableArray<AdditionalText> analyzerConfigFiles, AnalyzerConfigOptionsProvider optionsProvider);
// Existing WithXXX methods
public AnalyzerOptions WithAdditionalFiles(ImmutableArray<AdditionalText> additionalFiles);
// New WithXXX method
public AnalyzerOptions WithAnalyzerConfigFiles(ImmutableArray<AdditionalText> analyzerConfigFiles);
}
public class AnalysisResult
{
// Existing API for map of diagnostics reported in each AdditionaFile by analyzers.
public ImmutableDictionary<AdditionalText, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> AdditionalFileDiagnostics { get; }
// New API for map of diagnostics reported in each AnalyzerConfigFile by analyzers.
public ImmutableDictionary<AdditionalText, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> AnalyzerConfigFileDiagnostics { get; }
} |
Closes dotnet#64213 Design proposal and public APIs discussed in dotnet#64213 (comment) With this change, analyzers can report diagnostics in editorconfig and globalconfig files, which will be reported both in CI and in the IDE. Additionally, analyzers can access raw source text and file paths for all editorconfig and globalconfig files that are being applied to the compilation from AnalyzerOptions exposed off all analysis contexts for all analyzer callbacks (similar to how they can currently access raw text and file paths of additional files).
Awesome, thank you @mavasani! This will come in really handy when we start looking at improving the .NET Upgrade Assistant for Maui. |
API Review
Conclusion: Needs more work to address the above questions. |
AFAIK, "not enough keys set" is not going to be a mainline scenario, if at all. The primary analysis scenarios would be for individual analyzer config files:
We can always revisit this and add a separate callback with all analyzer config files provided at once, if we get such a request.
Yes, that is going to be the core issue. If we want to expose parsed, structured data, then we need to expose locations of the parsed data for reporting diagnostics. Additionally, it adds a constraint on the structure of the internal parsed data as soon as we expose it publicly. IMO, exposing raw text that user parses is the most flexible.
I see couple of problems with this approach:
|
This would be really nice to have. I also hit this same limitation in PolySharp the other day, where I wanted to emit some diagnostics for incorrect MSBuild properties being passed to the analyzer. Ended up having to write a source generator that only gets properties from the analyzer config and emits diagnostics (see PR here). Works just fine, but it's not ideal since it now can't run OOP (as it's not an analyzer), and the diagnostics don't show up nicely in solution explorer when expanding the analyzer node. Having a way to just emit diagnostics from |
@mgoertz-msft FYI that your original feature request has now been implemented for 16.8 P1 with #45076. I will keep this issue open to track exposing a similar API for analyzer config files.
See open question in #44131 (comment)
Originally posted by @mavasani in #44131 (comment)
The text was updated successfully, but these errors were encountered: