Lando
Lando

Reputation: 103

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.

Upvotes: 6

Views: 4110

Answers (4)

kzu
kzu

Reputation: 1871

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.

Upvotes: 5

Jason Malinowski
Jason Malinowski

Reputation: 19021

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.

Upvotes: 6

Kevin Pilch
Kevin Pilch

Reputation: 11615

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

Upvotes: 3

SLaks
SLaks

Reputation: 887509

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.

Upvotes: 1

Related Questions