简体   繁体   English

使用 Roslyn MSBuildWorkspace 项目 AddAnalyzerReference 不加载分析器

[英]Use Roslyn MSBuildWorkspace Project AddAnalyzerReference doesn't load analyzers

I'm working on a code report project.我正在做一个代码报告项目。 Currently, I'm able to compile the solution projects, get the diagnostics related to the compilation, etc.. The problem appears when I try to load my custom IDiagnosticAnalyzers, I've tried to use the AnalyzerFileReference and the AnalyzerImageReference without any result, Always I access the projects.Analizers are empty.目前,我能够编译解决方案项目,获取与编译相关的诊断等。当我尝试加载自定义 IDiagnosticAnalyzers 时出现问题,我尝试使用 AnalyzerFileReference 和 AnalyzerImageReference 没有任何结果,我总是访问项目。分析器是空的。

var inmutableArray = (new List<IDiagnosticAnalyzer>
    {
        new VariableEndedWithIdNamedCorrectlyDiagnosticAnalyzer()
    }).ToImmutableArray();
var analyzerImageReference = new AnalyzerImageReference(inmutableArray);

foreach (Project project in solution.Projects)
{                  
    project.AddAnalyzerReference(analyzerImageReference );
    //No analizers loaded....
}

UPDATE (thanks for the feedback [Josh Varty])更新(感谢反馈 [Josh Varty])

I've tried this two ways:我试过这两种方式:

var newProjects = new List<Project>();
foreach (Project project in solution.Projects)
{
    var newSolutionn= solution.AddAnalyzerReference(project.Id, analyzerImageReference);
    newProjects.Add(newSolutionn.Projects.FirstOrDefault(p=> p.Id == project.Id));
}

foreach (Project project in solution.Projects)
{
    var newProject = project.AddAnalyzerReference( analyzerImageReference);
}

In both cases have the analyzers loaded but when I get the compilation and I get the diagnostics, I don't get the output related to this analyzers (I think they are not being called at the get compilation function).在这两种情况下都加载了分析器,但是当我获得编译并获得诊断信息时,我没有获得与该分析器相关的输出(我认为它们没有在 get 编译函数中被调用)。

var compilation = newProject.GetCompilationAsync().Result;

var diagnostics =   compilation.GetDiagnostics();

Any suggestions?有什么建议?

As I commented, most Roslyn objects are immutable.正如我所评论的,大多数 Roslyn 对象都是不可变的。 This means methods like AddAnalyzerReference() don't mutate the project, but instead return a new one.这意味着像AddAnalyzerReference()这样的方法不会改变项目,而是返回一个新项目。

I don't have an analyzer to test this, but I believe you can use the following.我没有分析仪来测试这个,但我相信你可以使用以下内容。 Note that I'm using Solution.AddAnalyzerReference() instead of the one you were using.请注意,我使用的是Solution.AddAnalyzerReference()而不是您使用的那个。

var inmutableArray =(new List<IDiagnosticAnalyzer>
        {
            new VariableEndedWithIdNamedCorrectlyDiagnosticAnalyzer()
        }).ToImmutableArray();
var analyzerImageReference = new AnalyzerImageReference(inmutableArray);

Solution newSolution = solution;

//We iterate over the original solution
foreach (Project project in solution.Projects)
{                   
    //But we save our work in the newSolution
    newSolution = newSolution.AddAnalyzerReference(project.Id, analyzerImageReference);
}

//Now newSolution should contain all your changes.
//Maybe you want to save this reference?
solution = newSolution;

I've found the way to do it:我找到了这样做的方法:

 public static Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(this Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, CancellationToken cancellationToken = default(CancellationToken))
    {
        options = options ?? new AnalyzerOptions(ImmutableArray<AdditionalStream>.Empty, ImmutableDictionary<string, string>.Empty);
        Compilation newCompilation = null;
        var analyzerDriver = AnalyzerDriver.Create(compilation, analyzers, options, out newCompilation, cancellationToken);
        newCompilation.GetDiagnostics(cancellationToken);

        return analyzerDriver.GetDiagnosticsAsync();
    }

I've published a version of the open source project that I've been working using Roslyn, you can see the code and other thing related to analyzers and codefix.我已经发布了我一直在使用 Roslyn 工作的开源项目的一个版本,您可以看到与分析器和代码修复相关的代码和其他内容。

https://bitbucket.org/jrierapeiro/codeanalyzer https://bitbucket.org/jrierapeiro/codeanalyzer

I had similar question which i answered over here .我有类似的问题,我在这里回答。 You have to use compilation.WithAnalyzer(analyzer) and then getDiagnostics()你必须使用 compiler.WithAnalyzer(analyzer) 然后 getDiagnostics()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM