简体   繁体   中英

Roslyn Analyzer not clearing last diagnostic

I have created a Roslyn Analyzer that validates text files against a set of rules and have noticed that when I fix the last issue that diagnostic does not disappear. I have created a simple example to replicate the problem below.

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class Analyzer1Analyzer : DiagnosticAnalyzer
{
    public const string DiagnosticId = "TestCompilationAnalyzer";

    private static readonly LocalizableString Title = "TestAnalyzer";
    private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
    private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }

    public override void Initialize(AnalysisContext context)
    {
        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
        context.EnableConcurrentExecution();
        context.RegisterAdditionalFileAction(AdditionalFileAction);
    }

    private void AdditionalFileAction(AdditionalFileAnalysisContext cxt)
    {
        var text = File.ReadAllText(cxt.AdditionalFile.Path);

        if (text.Contains("help"))
        {
            cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"help"));
        }

        if (text.Contains("test"))
        {
            cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"test"));
        }
    }
}

if I add the words help and test to my additional file both diagnostics appear but if I remove both whatever was showing last won't disappear.

I found a workaround using code generators instead

    [Generator]
    public class SchemaTestAnalyzer : ISourceGenerator
    {

        public const string DiagnosticId = "TestCompilationAnalyzer";

        private static readonly LocalizableString Title = "TestAnalyzer";
        private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
        private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);

        public void Execute(GeneratorExecutionContext context)
        {
            var additionalFile = context.AdditionalFiles.FirstOrDefault();
            if (additionalFile != null)
            {
                var text = File.ReadAllText(additionalFile.Path);
                if (text.Contains("help"))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "help"));
                }

                if (text.Contains("test"))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "test"));
                }
            }
        }

        public void Initialize(GeneratorInitializationContext context) { }
    }

these work as expected and the last issue disappears when addressed and re-analyzed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM