Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Roslyn MSBuildWorkspace with Visual Studio 2013

Tags:

c#

roslyn

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:

  • Creating new Microsoft.CodeAnalysis.CustomWorkspace - got ReflectionTypeLoadException
  • MSBuildWorkspace.Create() throws exception

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.

like image 318
Lando Avatar asked Oct 12 '14 08:10

Lando


3 Answers

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 (i.e. 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.

like image 197
Jason Malinowski Avatar answered Oct 20 '22 13:10

Jason Malinowski


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.

like image 5
kzu Avatar answered Oct 20 '22 15:10

kzu


You could fork the Roslyn codebase, and compile with MSBUILD12 defined, which should still work, though we don't really test this much.

like image 3
Kevin Pilch Avatar answered Oct 20 '22 15:10

Kevin Pilch