roslyn: Diagnostic issues with locations in non-code files do not appear in VS's error list

I have some analyzers which work on non-code files. Everything works as expected except that the reported diagnostics don’t show up in VS’s error list if they have a location in a non-code file.

Here’s a quick sample. Make sure you have at least one ‘additional file’ in any project opened with this analyzer running. Notice that the diagnostic without a location shows up in the error list, but the one with a location in a non-code file does not.

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;

namespace Analyzer1
{
    [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 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.RegisterCompilationAction(cxt =>
            {
                var file = cxt.Options.AdditionalFiles.FirstOrDefault();
                Location loc = (file?.Path != null) ? Location.Create(file.Path, new TextSpan(0, 1), new LinePositionSpan(new LinePosition(0,0), new LinePosition(0,1))) : Location.None;

                // This does NOT show in VS's error list (but I think it should)
                cxt.ReportDiagnostic(Diagnostic.Create(Rule, loc, "A"));

                // This DOES show in VS's error list (as expected)
                cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"B - {loc.ToString()}"));
            });
        }

    }
}

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 26 (26 by maintainers)

Most upvoted comments

@AArnott Ah, then the issue you are talking about is completely different then tracked by this issue, which is specifically meant for diagnostics reported in non-code files by DiagnosticAnalyzers. I suggest you to open a separate issue for diagnostics reported in non-code files by source generators and tag @chsienki @jasonmalinowski on it.

With generators coming down the pipe and being able to access additional files and produce diagnostics, will this issue finally be looked at? 🤔

From the initial spec:

  • Can produce diagnostics. When unable to generate source, the generator can inform the user of the problem.
  • May access additional files, that is, non-C# source texts.

// @chsienki