Reputation: 1049
So what I need is some why to write a solution analyzer for Visual Studio, that can:
I have a console application that can do this (using the Roslyn compiler), but I want to integrate the logic inside Visual Studio. From what I have read, the current code fix/refactoring/analyzers support only document level of refactoring, but for me that is not enough information to recommend one of the mentioned refactorings. I need information about the whole solution.
So my question is what is the best way to do this? Can you recommend a starting point? Some articles related to the topic? Any advice would help me a lot.
Upvotes: 3
Views: 699
Reputation: 2089
Short Answer: There is no reasonable way to do this with the Roslyn API
Long Answer:
The api as it is currently implemented only allows analyzers to know about things in the current compilation (a project in Visual Studio). If you call RegisterCompilationAction
from within the Initalize
method in your analyzer, you will be able to look at all the symbols within the compilation.
Why can't analyzers see the scope for an entire solution? The simplest answer is: because the compiler can't, and analyzers run inside the compiler. This is done so that analyzers can be run on continuous integration servers without Visual Studio installed. MSBuild reads the solution file and then invokes the compiler once for each project. The compiler is never aware of project dependencies and the compiler team does not want to be in that business, they are happy to leave it to MSBuild.
People have tried to work around this by loading their solution using MSBuildWorkspace
and attempting to look at documents across projects that way. This will fail occasionally because MSBuildWorkspace
is not thread-safe. It will also cause memory usage to skyrocket. People have tried to cache MSBuildWorkspace
instances to partially resolve this problem but the cache needs to be invalidated every time a new compilation is created (essentially in the event of all but the most trivial changes). Basically, going down this path is rife with pain and is unsupported.
Enough people have asked for this feature that its something we think we need to do eventually. There is no reasonable way to accomplish it today unless you are willing to write a Visual Studio extension that imports the Visual Studio Workspace and attempts to run its own analysis engine. Please file feature request on https://github.com/dotnet/roslyn
Upvotes: 2