dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.07k stars 4.04k forks source link

AnalyzerRunner unable to correctly run analyzers which are defined completely in the core Features Assembly #29245

Closed mavasani closed 4 weeks ago

mavasani commented 6 years ago

I attempted to follow steps from https://github.com/dotnet/roslyn/issues/26778 for my recent PR https://github.com/dotnet/roslyn/pull/28918, and found that the following command line leads to empty document optionset for the analyzer (and hence 0 diagnostics as analyzer bails out early), possibly because the analyzer is fully implemented in the core Features layer, while the options are in C# and VB specific assemblies:

c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\AnalyzerRunner.exe c:\roslyn\Binaries\Release\Dlls\Microsoft.CodeAnalysis.Features\Microsoft.CodeAnalysis.Features.dll c:\roslyn\Roslyn.sln /a MakeFieldReadonlyDiagnosticAnalyzer /log results.log /concurrent /stats
    [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
    internal sealed class MakeFieldReadonlyDiagnosticAnalyzer
        : AbstractCodeStyleDiagnosticAnalyzer

I also attempted to use a different command line by creating a separate folder Analyzers with all 3 Features assemblies, but still hit the same issue:

mkdir c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\Analyzers
xcopy  c:\roslyn\Binaries\Release\Dlls\Microsoft.CodeAnalysis.Features\Microsoft.CodeAnalysis.Features.dll c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\Analyzers
xcopy c:\roslyn\Binaries\Release\Dlls\Microsoft.CodeAnalysis.CSharp.Features\Microsoft.CodeAnalysis.CSharp.Features.dll c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\Analyzers
xcopy c:\roslyn\Binaries\Release\Dlls\Microsoft.CodeAnalysis.VisualBasic.Features\Microsoft.CodeAnalysis.VisualBasic.Features.dll c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\Analyzers

c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\AnalyzerRunner.exe c:\roslyn\Binaries\Release\Exes\AnalyzerRunner\Analyzers c:\roslyn\Roslyn.sln /a MakeFieldReadonlyDiagnosticAnalyzer /log results.log /concurrent /stats

Finally, I ended up doing the following workaround to define analyzers in each language specific assemblies, and this time it worked and analyzer reported the expected set of diagnostics:

    //[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
    internal abstract class MakeFieldReadonlyDiagnosticAnalyzer
        : AbstractCodeStyleDiagnosticAnalyzer
namespace Microsoft.CodeAnalysis.CSharp.MakeFieldReadonly
{
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    internal sealed class CSharpMakeFieldReadonlyDiagnosticAnalyzer : MakeFieldReadonlyDiagnosticAnalyzer
    {
    }
}
Namespace Microsoft.CodeAnalysis.VisualBasic.MakeFieldReadonly
    <DiagnosticAnalyzer(LanguageNames.VisualBasic)>
    Friend Class VisualBasicMakeFieldReadonlyDiagnosticAnalyzer
        Inherits MakeFieldReadonlyDiagnosticAnalyzer
    End Class
End Namespace
mavasani commented 6 years ago

Tagging @sharwell @ivanbasov

CyrusNajmabadi commented 4 weeks ago

Closing out as speculative.