You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Upgrade all NuGet packages; the template doesn't know context.RegisterAdditionalFileAction yet
Make a new class that inherits from DiagnosticAnalyzer, like so:
using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.Diagnostics;using System;using System.Collections.Immutable;using System.IO;using System.Runtime.CompilerServices;namespaceAnalyzer1{[DiagnosticAnalyzer(LanguageNames.CSharp)]publicclassAdditionalFileActionAnalyzer:DiagnosticAnalyzer{publicconststringDiagnosticId="Analyzer1";// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Localizing%20Analyzers.md for more on localizationprivatestaticreadonlyLocalizableStringTitle=new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager,typeof(Resources));privatestaticreadonlyLocalizableStringMessageFormat=new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormat), Resources.ResourceManager,typeof(Resources));privatestaticreadonlyLocalizableStringDescription=new LocalizableResourceString(nameof(Resources.AnalyzerDescription), Resources.ResourceManager,typeof(Resources));privateconststringCategory="Naming";privatestaticreadonlyDiagnosticDescriptorRule=new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault:true, description: Description);publicoverrideImmutableArray<DiagnosticDescriptor> SupportedDiagnostics {get{return ImmutableArray.Create(Rule);}}publicoverridevoidInitialize(AnalysisContextcontext){
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
Logger.Log("hello");
context.RegisterAdditionalFileAction(AnalyzeAdditionalFile);}privatestaticvoidAnalyzeAdditionalFile(AdditionalFileAnalysisContextcontext){
Logger.Log("AnalyzeAdditionalFile");}}internalclassLogger{publicstaticvoidLog(stringtext,[CallerFilePath]stringfilePath=null,[CallerMemberName]stringmemberName=null){
File.AppendAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),@"analyzer.log"),$"{Path.GetFileNameWithoutExtension(filePath)}|{memberName}|{DateTime.Now:yyyy-MM-dd HH:mm:ss}|{text}\n");}}}
Use the template's VSIX project to launch a debugged VS instance. Open a project where <AdditionalFiles> includes a file, or a file is implicitly in it for another reason.
Diagnostic Id:
n/a
Expected Behavior:
AnalyzeAdditionalFile should be logged on occasion when such a file is frontmost, and being edited. I.e., the analyzer should be usable during design time.
Actual Behavior:
AnalyzeAdditionalFile seems to only ever get logged when explicitly building the project.
I'm unsure if this is by design. (The docs don't seem to imply as much.) It's not the behavior I observe with RegisterSymbolAction, where I can simply make a C# file frontmost, and the analyzer will run. If it is by design that RegisterAdditionalFileAction only takes place at build, what's the guidance on running an analyzer on a non-C# file (in my case, a .cshtml, but .xaml and others would be of use as well) as the users view/edit code?
The text was updated successfully, but these errors were encountered:
AnalyzeAdditionalFile seems to only ever get logged when explicitly building the project.
@sharwell I think this is expected behavior, could you pls confirm?
what's the guidance on running an analyzer on a non-C# file (in my case, a .cshtml, but .xaml and others would be of use as well) as the users view/edit code?
I don't think this will work right now, in the IDE at least. Roslyn doesn't register for diagnostic requests for additional files at the moment, and Razor won't run Roslyn diagnostics. I expect this to change in future with our cohosting effort, but that doesn't help now. Depending on the type of analysis being performed, analyzing and reporting diagnostics on the generated C# file that is compiled for a .cshtml should work fine, and Razor tooling will translate diagnostics reported in those files, to the appropriate position in the .cshtml file itself.
Version Used: C# Tools 4.10.0-3.24211.4+e482b6e
Steps to Reproduce:
context.RegisterAdditionalFileAction
yetDiagnosticAnalyzer
, like so:<AdditionalFiles>
includes a file, or a file is implicitly in it for another reason.Diagnostic Id:
n/a
Expected Behavior:
AnalyzeAdditionalFile
should be logged on occasion when such a file is frontmost, and being edited. I.e., the analyzer should be usable during design time.Actual Behavior:
AnalyzeAdditionalFile
seems to only ever get logged when explicitly building the project.I'm unsure if this is by design. (The docs don't seem to imply as much.) It's not the behavior I observe with
RegisterSymbolAction
, where I can simply make a C# file frontmost, and the analyzer will run. If it is by design thatRegisterAdditionalFileAction
only takes place at build, what's the guidance on running an analyzer on a non-C# file (in my case, a.cshtml
, but.xaml
and others would be of use as well) as the users view/edit code?The text was updated successfully, but these errors were encountered: