如何在VS Dev Cmd&中的msbuild期间显示分析器错误/警告使用MSBuildWorkspace
我将用一个例子来说明这种情况。
I'll explain the situation with an example.
假设我创建了一个Roslyn Analyzer,当类名称为 TestClass 时抛出错误。分析器代码如下:
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()));
}
}
因此我在某些 ConsoleApp 项目。控制台项目在使用系统的 Program.cs 文件
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");
}
}
}
现在,如果我在Visual Studio中构建ConsoleApp项目,然后出现 TestClass name not used的错误,这很好。
但是,当我尝试在针对VS 2017的开发人员命令提示符中使用msbuild命令构建同一项目时,我没有看到Analyzer出现任何错误。我希望在VS中的错误列表中显示的所有错误都应该在Dev Cmd中显示。
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.
我的最终目标是创建一个独立的代码分析工具项目,然后使用MSBuildWorkspace编译ConsoleApp项目并获取分析器错误/警告。部分代码如下:
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.
要在VS Dev Cmd中的msbuild过程中显示分析器错误/警告,您只需通过重建开关,例如 msbuild Tempsolution.sln / t:rebuild
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
对于MSBuidlWorkspace来说,代码为我工作。我们必须通过 compilation.WithAnalyzer(ImmutableArray< DiagnosticAnalyzer>);
来手动指定要使用的分析器。
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();