简体   繁体   中英

Use Roslyn MSBuildWorkspace with Visual Studio 2013

For my master thesis I'm building a Visual Studio plugin that should perform some code-analysis of the current opened solution. In order to do that I've decided to try to use Roslyn, using the corresponding nuget package .

Everything works fine (SyntaxTree for navigating the code,...) until I've tried to use MSBuildWorkspace.Create(). This last call causes the following exception:

Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

I found this two posts:

from which I understand that I need the MSBuild Tools for Visual Studio 14, which is in the related iso.

I do not want to install the full Visual Studio 14, this also because the plugin that I'm writing should run under Visual Studio 2013. From the two post it seems that I can install only this part of VS 14.

My question actually is: if I install MSBuild Tools for Visual Studio 14 what will happen with all other Visual Studio projects that I'm currently working on? At the moment they use the MSBuild tool of Visual Studio 2013. It's possible to still use that?

Update

What I'm acctually trying to get is to find if a given method has references inside the project. The idea was to proceed as in this post .

When you say you're building a plugin for Visual Studio, if all you care about is getting the workspace for the currently open solution (ie the code the user is editing), you shouldn't use MSBuildWorkspace, really ever. That's a type for loading things outside of Visual Studio. What you can do if you're in the product is MEF [Import] Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace. That gives you live access to what the user has open, and avoids running MSBuild entirely.

Note you're still going to have to be careful about your choice of reference assemblies you build against: if you use the latest NuGet packages that won't work since those will differ in version (and also APIs -- we changed a bunch of stuff) than what was in the last Visual Studio 2013 preview.

The issue is that (unfortunately) the assemblies in the public Roslyn nuget packages have been compiled with a newer version of MSBuild than what you want.

However, it's pretty simple to fix so that it works on MSBuild 4.0 (VS2012+). I've provided them with a PR with the fix (at https://roslyn.codeplex.com/workitem/405 ), but also published a nuget package called DesktopAnalysis that contains all the assemblies for doing C# code analysis, that work on VS2012+ (MSBuild 4.0+): https://www.nuget.org/packages/DesktopAnalysis

Just do an install-package DesktopAnalysis -pre instead and you're done. The assemblies are the same, the code is the same, etc.

I'm using this to provide a code migration extension that works from VS2013 all the way to VS2015 Preview.

您可以分叉Roslyn代码库,并使用定义的MSBUILD12进行编译,但仍然可以使用,但我们并没有真正测试这么多。

This is entirely possible , but not at all easy.

You need to make sure that you only ever load the version of the Roslyn assemblies that is in the VS version you're targeting, by removing those assemblies from your VSIX and handling AssemblyResolve to make sure you get the right ones.
You can see my code that does exactly that here , and you can read more about this technique in my blog post

Note that if you need to [Export] any interfaces defined in Roslyn assemblies, this will not work at all, since MEF will try to load them before you add your handler. (unless you add a module initializer by hand in ildasm)

The harder part is that you need to limit yourself to the intersection of the APIs in every Roslyn version you want to support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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