0

I've this strange problem trying to write a piece of code analysis and then running it through an MSBuild Task. When I'm running using MSBuild I'm using a MSBuildWorkspace.Create in order to create a workspace. However, this method only works when it's run through the version of MSBuild in the .net Framework directory whereas both the v12 (2013) and v14 (2015) crash.

See the following

    [TestMethod]
    public void RunThroughMsBuild()
    {
        RunMsBuild(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe");//Succeeds
        RunMsBuild(@"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe");//Fails
        RunMsBuild(@"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe");//Fails
    }

    private static void RunMsBuild(string msbuildLocation)
    {
        var process = Process.Start(new ProcessStartInfo(msbuildLocation, ProjectFileLocation)
        {
            UseShellExecute = false,
            RedirectStandardError = true,
            RedirectStandardOutput = true
        });

        Debug.Write(process.StandardOutput.ReadToEnd());
        process.WaitForExit();

        Assert.IsTrue(process.HasExited);
        Assert.AreEqual(0, process.ExitCode);
    }

The error I'm getting is even weirder:

  • [0] {"Method 'ResolveReference' in type 'Microsoft.CodeAnalysis.AssemblyReferenceResolver' from assembly 'Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.":"Microsoft.CodeAnalysis.AssemblyReferenceResolver"} System.Exception {System.TypeLoadException}
  • [1] {"Method 'get_Locations' in type 'Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationArrayTypeSymbol' from assembly 'Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.":"Microsoft.CodeAnalysis.CodeGeneration.CodeGenerationArrayTypeSymbol"} System.Exception {System.TypeLoadException}

and 16 more...

The code throwing this exception is a simple:

var workspace = MSBuildWorkspace.Create();
thekip
  • 3,660
  • 2
  • 21
  • 41

1 Answers1

0

The problem in this case was the fact that adding references through visual studio adds them from the wrong source. The v12 and v14 MSBuild locations have their own assemblies relating to MSBuild. Consuming those solved this problem, but the next problem is that you cannot use MSBuildWorkspace from an MSBuild task as per this question:

Can I access the MsBuildWorkspace from within a MsBuild Task class?

Community
  • 1
  • 1
thekip
  • 3,660
  • 2
  • 21
  • 41