How to show Analyzer errors/warnings during msbuild in VS Dev Cmd & using MSBuildWorkspace

那年仲夏 提交于 2020-07-23 06:40:33

问题


I'll explain the situation with an example.

Suppose I have created a Roslyn Analyzer which throws Error when Class name is TestClass. Analyzer code is as below:

public override void Initialize(AnalysisContext context)
{
    context.RegisterSyntaxNodeAction(Method, SyntaxKind.ClassDeclaration);
}

private static void Method(SyntaxNodeAnalysisContext context)
{
    var node = (ClassDeclarationSyntax)context.Node;
    var name = node.TryGetInferredMemberName();
    if(name == "TestClass")
    {
         context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation()));
    }           
}

So i install the Analyzer nupkg in some ConsoleApp project. Console project has following code in Program.cs file

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }

    class TestClass
    {
        static void test()
        {
            Console.WriteLine("TestClass");
        }
    }
}

Now if i build the ConsoleApp project in Visual Studio then i get Error as "TestClass name not to be used" which is fine. But when i try to build the same project using msbuild command in Developer Command Prompt for VS 2017 i don't see any error from Analyzer. I want that all the errors shown in Error list in VS should be shown in Dev Cmd.

My end goal is to create a stand-alone code analysis tool project and then use MSBuildWorkspace to compile ConsoleApp project and get the analyzer errors/warnings. Part of code is as below:

var filePath = @"C:\Users\user\repos\ConsoleApp\ConsoleApp.sln";
var msbws = MSBuildWorkspace.Create();
var soln = await msbws.OpenSolutionAsync(filePath);
var errors = new List<Diagnostic>();
foreach (var proj in soln.Projects)
{
    var name = proj.Name;
    var compilation = await proj.GetCompilationAsync();
    errors.AddRange(compilation.GetDiagnostics().Where(n => n.Severity == DiagnosticSeverity.Error).ToList());
}
var count = errors.Count();

Above code does not show errors/warnings from analyzer.

How can i achieve this? Thanks in Advance.


回答1:


How to show Analyzer errors/warnings during msbuild in VS Dev Cmd & using MSBuildWorkspace

Actually, these warnings are from Code analysis mechanism rather than MSBuild warnings(like MSBxxx). And I think the TestClass name not to be used is just a warning(yellow mark) not an error.

In VS IDE, its environment integrates the MSBuild tool(Developer Command Prompt for VS) and Code Analyzer. Because of this, you can get the warnings in VS IDE.

However, when you use Developer Command Prompt, which is essentially a separate compilation tool for MSBuild, it doesnot have an integrated code analyzer, so you don't have this type of warning except for MSBuild warnings and errors(MSBxxx). This is also the limitation of the tool. Warning by itself does not affect the entire program.

Test

You can test it by input this in an empty console project: int a=1;(It is a code analyzer warning) and I am sure that the warning can be showed in output window in VS IDE and will not be listed in Developer Command Prompt for VS.

Suggestion

As a suggestion, you can try to treat these warnings as errors and Code Analyzer passes these warnings to the msbuild and specifies them as errors so that you can get the error in DEV.

Add these in your xxx.csproj file:

<PropertyGroup>
 <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

Although this approach breaks the build process, it is reliable and practical. And this method is very commonly used, generally used in the final production stage of the project, to exclude all errors and warnings for large projects, so as to prevent subsequent errors that may occur and be foolproof.

Then, you can use your code to build the project.




回答2:


To show analyzer errors/warnings during msbuild in VS Dev Cmd, you just have to pass rebuild switch for example msbuild Tempsolution.sln /t:rebuild

And for MSBuidlWorkspace, this code worked for me. We have to manually specify the analyzer to use by using compilation.WithAnalyzer(ImmutableArray<DiagnosticAnalyzer>);.

MSBuildLocator.RegisterDefaults();
var filePath = @"C:\Users\user\repos\ConsoleApp\ConsoleApp.sln";
var msbws = MSBuildWorkspace.Create();
var soln = await msbws.OpenSolutionAsync(filePath);
var errors = new List<Diagnostic>();
foreach (var proj in soln.Projects)
{
    var analyzer = proj.AnalyzerReferences.Where(alz => alz.Display.ToLower() == "Your analyzer name").FirstOrDefault();
    var compilation = await proj.GetCompilationAsync();
    var compWithAnalyzer = compilation.WithAnalyzers(analyzer.GetAnalyzersForAllLanguages());
    var res = compWithAnalyzer.GetAllDiagnosticsAsync().Result;
    errors.AddRange(res.Where(r => r.Severity == DiagnosticSeverity.Error).ToList());
}
var count = errors.Count();


来源:https://stackoverflow.com/questions/62278106/how-to-show-analyzer-errors-warnings-during-msbuild-in-vs-dev-cmd-using-msbuil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!