Using Roslyn MSBuildWorkspace with Visual Studio 2013

For my master's thesis, I am creating a Visual Studio plugin that should do some code analysis of the current open solution. For this, I decided to try using Roslyn using the appropriate nuget package .

Everything works fine (SyntaxTree for code navigation ...) until I tried using MSBuildWorkspace.Create(). This last call raises 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 these two posts:

from which I understand that I need the MSBuild tools for Visual Studio 14 that are in the corresponding iso.

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

My question really is: if I install MSBuild Tools for Visual Studio 14, what happens to all the other Visual Studio projects I'm working on now? They are currently using MSBuild for Visual Studio 2013. Is it possible to use this?

Update

What I'm really trying to get is to find out if the given method has links within the project. The idea was to continue, as in this post .

+6
source share
4 answers

When you say that you are creating a plug-in for Visual Studio, if everything you care about gets a workspace for the currently open solution (that is, the code that the user is editing), you should not use MSBuildWorkspace, really ever. This is the type for loading things outside of Visual Studio. What can you do if your product contains MEF [Import] Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace. This gives you direct access to an open user and allows you to fully launch MSBuild.

Please note that you still need to be careful in choosing the link assembly you are building with: if you use the latest NuGet packages that will not work, because they will differ in version (as well as APIs), we changed a bunch of things ) than what was in the last preview of Visual Studio 2013.

+6
source

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

However, this is fairly easy to fix, so it works on MSBuild 4.0 (VS2012 +). I gave them the corrected PR ( https://roslyn.codeplex.com/workitem/405 ), but also published a nuget package called DesktopAnalysis, which contains all the assemblies for performing C # code analysis that work on VS2012 + (MSBuild 4.0+): https://www.nuget.org/packages/DesktopAnalysis

Just do install-package DesktopAnalysis -pre and install-package DesktopAnalysis -pre done. The fees are the same, the code is one, etc.

I use this to provide a code port extension that works from VS2013 to VS2015 Preview.

+5
source

You can develop the Roslyn code base and compile with MSBUILD12 installed, which should still work, although we don’t actually experience it.

+3
source

It is completely possible , but not at all easy.

You need to make sure that you only download the version of the Roslyn assemblies that is in the VS version that you are targeting by removing those assemblies from your VSIX and processing AssemblyResolve to make sure you get the ones you need. <sh> You can see my code that does exactly this here , and you can learn more about this technique in my blog post

Please note: if you need [Export] any interfaces defined in Roslyn assemblies, this will not work, as MEF will try to load them before adding a handler. (unless you add the module initializer manually in ildasm)

The more complicated it is, you need to limit yourself to crossing the API in every version of Roslyn that you want to support.

+1
source

Source: https://habr.com/ru/post/976631/


All Articles