Roslyn: MSBuildWorkspace can't open a VS2017 project (.Net Framework or .NetStandard)

Created on 28 Sep 2017  路  6Comments  路  Source: dotnet/roslyn

Version Used:
VS2017 15.3.4 and 15.4 preview
Steps to Reproduce:

  1. Compile the following code inside a new project "TestWorkspace.csproj" targeting .net 47
  2. add reference to Microsoft.Build.Tasks and Microsoft.CodeAnalysis.CSharp.Workspaces

You can target a command line project or a unit test one. Can't get this to work in either cases.

Expected Behavior:
The number of documents should be greater than 0 since program.cs exists in the project you created. (return true)

Actual Behavior:
Everything is empty. (return false)

Code to use

    public bool TestLoadingProject()
    {
        var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        var fileInfo = new FileInfo(assemblyLocation);
        var folder = fileInfo.Directory;
        var parent = folder.Parent;

        var projectPath = Path.Combine(parent.FullName, "TestWorkspace.csproj");

        var msWorkspace = MSBuildWorkspace.Create();
        var project = msWorkspace.OpenProjectAsync(projectPath).Result;
        var compilation = project.GetCompilationAsync().Result;
        return project.DocumentIds.Count > 0;
    }

I've been using Roslyn for a while and with latest VS2017 and netstadnard 2, reading MSBuildWorkspace is not working from a command line or unit test. I think it still works inside a VSIX. This is beyound annoying, I don't want to ship a VSIX without doing unit tests... My real test case is to analyse standard2 library, not even sure that will work. It seems such a basic usage of MSBuildWorkspace...

Area-IDE Bug

Most helpful comment

Sorry to bother you with this again, but I'd really (and do mean REALLY) like the ability to unit tests my visual studio extensions. For this, I need the ability to load a workspace inside a unit test. Any traction on this ?

All 6 comments

Tagging @DustinCampbell.

I've hooked up the WorkspaceEvent and when trying to load a .net 4.7 csproj file, it returns

Msbuild failed when processing the file '...' with message: The tools version "15.0" is unrecognized. Available tools versions are "12.0", "2.0", "3.5", "4.0"

When trying to load a .net standard 2 project, I get the following:

Msbuild failed when processing the file '...' with message: The SDK 'Microsoft.NET.Sdk' specified could not be found.

So... it looks like version 15 is not supported. I seriously doubt that I'm the first in the world that tries to load a .net framework project from VS2017. I'm also concerned about the error for loading a .net standard project.

For our project, we are using .net standard everywhere and we want to provide VSIX extensions to help the development effort. By definition, it means a .net framework process needs to be able to read a .net standard project. My original thinking was that since .netstandard is supported by .net framework, it shouldn't be a problem.... is that assumption wrong ?

@phillippe-lavoie: There are couple of reasons why this might happen. My guess is that you're running into a bug where MSBuild cannot locate VS 2017 on your machine. Thus, it doesn't find the "15.0" toolset, which is located beneath the Visual Studio 2017 directory. There are a few issues that this might happen:

  1. You need to pull newer Microsoft.Build and Microsoft.Build.Framework packages into your project. Note that this might also require adding binding redirects. See https://github.com/dotnet/roslyn/issues/21644 for the necessary binding redirects.
  2. The name of your application includes "MSBuild" in its exe name. I've run into this one a couple of times. (https://github.com/Microsoft/msbuild/issues/2194)

I might have made progress, but it's still not working. In the case of a unit test, I'm still getting the above behavior. In the case of a command line tool, I get a different output. The code is at TestingWorkspaceLoading and it isolates the issue for both command line and unit tests import of a VS2017 project (both .net framework and .net standard)

Tests are starting
TestLoadingFrameworkProject fail 3036
System.AggregateException: One or more errors occurred. ---> System.Exception: Msbuild failed when processing the file 'C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\ADotNetFrameworkLibrary\ADotNetFrameworkLibrary.csproj' with message: The imported project "C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\FromCommandLine\bin\Debug\Roslyn\Microsoft.CSharp.Core.targets"
TestLoadingStandardProject fail 440
System.AggregateException: One or more errors occurred. ---> System.Exception: Msbuild failed when processing the file 'C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\ADotNetStandardLibrary\ADotNetStandardLibrary.csproj' with message: The SDK 'Microsoft.NET.Sdk' specified could not be found. C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\ADotNetStandardLibrary\ADotNe

The error was a bit long and the output of the command line truncates it, but it's actually the following

Msbuild failed when processing the file 'C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\ADotNetFrameworkLibrary\ADotNetFrameworkLibrary.csproj' with message: The imported project "C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\FromCommandLine\bin\Debug\Roslyn\Microsoft.CSharp.Core.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. C:\Projects\T\TestingWorkspaceLoading\TestingWorkspaceLoading\FromCommandLine\bin\Debug\Microsoft.CSharp.CurrentVersion.targets

As seen on my test project, I added all binding redirects and every latest version of Microsoft.Build.* needed.

Sorry to bother you with this again, but I'd really (and do mean REALLY) like the ability to unit tests my visual studio extensions. For this, I need the ability to load a workspace inside a unit test. Any traction on this ?

Was this page helpful?
3 / 5 - 1 ratings