6

I've recently upgraded to Visual Studio 2013, which has caused back to back problems when building externally using MSBuild (API or Executable with command line args)

Issue #1 When building with MSBuild it doesn't generate Fake assemblies which are required for our Unit Tests, this leads to build failures. A simple build in visual studio fixes this temporarily, until a new fake assembly needs to be generated.

Issue #2 When running code analysis this complains with the following:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets(284,5): 
error MSB4127: The "CodeAnalysis" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\.\FxCopTask.dll". 

Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.CodeAnalysis' to type 'Microsoft.Build.Framework.ITask'.

I only have Visual Studio 2013 installed on my machine, apparently installing an older version could fix the issue, but it's not something which I can do. (VS 2013 Ships with its own MSBuild 12.0 which is located in a different directory to the previous MSBuild).

I'm unsure why Visual Studio is behaving any differently to MSBuild, i'm simply pointing to the solution file like so...

msbuild.exe "path\solution.sln" /property:Configuration=Debug
Jack
  • 15,614
  • 19
  • 67
  • 92

1 Answers1

6

So, Visual Studio 2013 comes with a new version of MSBuild i.e. MSBuild 12.0. Once installed, it changes the path so that the new version is used by default.

Looks like your solution compiles with visual studio 2012, you can either specify the full path to msbuild.exe such as

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "path\solution.sln" /property:Configuration=Debug

or set the visual studio 2012 environment variables by running the following before executing msbuild

"%VS110COMNTOOLS%"\vsvars32.bat // VS2012 environment variables

EDIT: Using MSbuild 12.0 assemblies "C:\Program Files (x86)\MSBuild\12.0\Bin\" fixes the issue with the code Analysis bug.

Hamid Shahid
  • 4,486
  • 3
  • 32
  • 41
  • I have not got this directory %VS110COMNTOOLS%. Also if i'm using the C# MSBuild API I can't see any means of changing the MSBuild executable which is called. When I build in VS 2013 (which works successfully) I assume it's calling MSBuild 12, unless the tools version is overriding it (set to 4.0). – Jack May 06 '14 at 14:45
  • Ok, the folder will only be there if you have Visual Studio 2012 installed. If you are calling MSBuild programatically, set the ToolsVersion of the toolset class to 4.0 http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.toolset(v=vs.110).aspx – Hamid Shahid May 06 '14 at 15:03
  • I have tried setting the ToolsVersion both using a command line argument, and via the toolset in the C# API. Neither seem to fix the issue. I have also tried playing around with the "VisualStudioVersion" property which fixed another issue that I was having. – Jack May 06 '14 at 16:14
  • Strange. That should have worked. See this "http://msdn.microsoft.com/en-us/library/bb383985.aspx" I have not tried with MSBuild API but with command line and it works fine. Can you tell me what namespace are you using? – Hamid Shahid May 06 '14 at 16:25
  • Hamid, the issue is that the API is currently 4.0 and uses an older MSBuild executable. I'm hoping that a 12.0 API will be released, and hopefully that will allow the correct executable to run. – Jack May 08 '14 at 15:27
  • 1
    i've found the 12.0 assemblies and it's fixed the issue with the code Analysis bug "C:\Program Files (x86)\MSBuild\12.0\Bin\" If you add this information into your answer i'll mark it as accepted. Thanks for the help. – Jack May 14 '14 at 13:15
  • @HamidShahid so I migrate the VS2010 project to VS2013 and try to build but facing some issue. C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets(214,5): error MS175: The task factory "CodeTaskFactory" could not be loaded from the assembly "C:\Program Files (x86)\MSBuild\12.0\bin\Mrosoft.Build.Tasks.v12.0.dll". The environment block used to start a process cannot be longer than 65535 bytes. Your environment block is 67632 bytes long. Remove some environment variables and try again. [X:\base\mti\dmm\winclnt\project\wirap\winwrap.vcxproj] –  Sep 02 '16 at 07:06
  • @YogendraSharma not sure how it's related to this question, the following might be helpful https://gyorgybalassy.wordpress.com/2013/12/31/msb4175-the-task-factory-codetaskfactory-could-not-be-loaded/ – Hamid Shahid Sep 02 '16 at 17:13