Skip to content

Roslyn Analyzer not clearing last diagnostic #53192

@joncoello

Description

@joncoello

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.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions