Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force MSBuild 14.0 in psake build for C# 6.0 code base

I recently updated to Visual Studio 2015 and am using the new C# 6.0 features. In VS, everything builds correctly.

However, I use PSake as build script language, and the build always fails at places where I use c# 6.0 features. How can I tell psake to use MSBuild 14.0, so that the new c# 6.0 features build correctly?

Tried & failed:

  • Passing in the framework version to psake: Unknown .NET Framework version, 4.6

  • Call the vsvars32.bat of VS2015 prior to invoking psake. PSake still uses the old MSBuild version.

like image 847
theDmi Avatar asked Oct 08 '15 11:10

theDmi


2 Answers

I had the same issue & fixed it by configuring PSake build script to use .Net Framework 4.6, simply by adding Framework "4.6" in the beginning of the file. You may check this unit test from PSake code base dotNet4.6_should_pass.ps1

like image 183
Wahid Shalaly Avatar answered Oct 13 '22 17:10

Wahid Shalaly


The solution described by @WahidShalaly seems to be the correct one, so use that. However, on my machine it still didn't find the correct MSBuild version so here is the workaround that I have in place instead:

Add the following task to your PSake script:

Task SetEnvironment {
  Exec { & $env:VS140COMNTOOLS\vsvars32.bat }
}

This sets the environment for VS2015 compilation from within PSake.

Now add a dependency to SetEnvironment from every task that calls MSBuild and use msBuild.exe instead of just msbuild. Example:

Task Compile -Depends SetEnvironment {
  Exec { & "msbuild.exe" "theSolution.sln" /p:VisualStudioVersion=14.0 }
}
like image 44
theDmi Avatar answered Oct 13 '22 16:10

theDmi