This stack overflow question:
Microsoft Code Contracts and CI build server
asks how to get code contracts working on a build server without installing Visual Studio 2010. We're trying to do the same. We've followed the steps outlined in the accepted answer, but haven't been able to get it working.
CodeContracts will not install on the build server unless Visual Studio is present. So following the suggestion, we've done the following:
%programfiles%\Microsoft\Contracts\Bin
from a development machine with Visual Studio 2010 Ultimate and Code Contracts Premium installed to the build server.MSBuild\v4.0
folder that contains Microsoft.CodeContracts.targets
and Microsoft.CodeContractAnalysis.targets
.According to the CodeContracts documentation,
Using msbuild on a project or solution that uses contracts enabled via the VS user interface will perform the same actions as the corresponding build under VS.
This is our use case, we're simply calling MSBuild on our solution file as below. The solution file is created via Visual Studio with all the expected Code Contract options configured for rewriting.
<Target Name="Release">
<MSBuild Projects = "Cofamilies\WebApplication\CofamiliesWeb.sln" Properties="Configuration=Release" />
</Target>
But the rewriter is not getting called.
Does anybody have a suggestion for what we're missing and/or suggested troubleshooting steps?
If you go to your project properties, you should see a Code Contracts tab. On the tab, select the mode you are building in (Debug|Release|Both) and then turn on Code Contracts features by checking the appropriate check boxes. I've seen the warning that you detail when Code Contracts are not set to Build .
Code contracts provide a way to specify preconditions, postconditions, and object invariants in . NET Framework code. Preconditions are requirements that must be met when entering a method or property.
Preconditions specify state when a method is invoked. They are generally used to specify valid parameter values. All members that are mentioned in preconditions must be at least as accessible as the method itself; otherwise, the precondition might not be understood by all callers of a method.
I had the same problem with the latest version of Code Contracts. I had installed the Premium Edition on my development PC and the Standard Edition on the build server and was getting the following error due to the Rewriter not running.
Must use the rewriter when using Contract.Requires<TException>
It appears that the Standard edition is missing a key file (CodeContractsAfter.targets) that is needed for MSBuild to invoke the Rewriter.
The solution for me was to copy the CodeContractsAfter.targets from C:\Program Files (x86)\MSBuild\4.0\Microsoft.Common.Targets\ImportAfter on my development PC to the corresponding folder on the build server.
Note: The paths weren't identical as my development PC is running Windows 7 64bit and the build server is running Windows Server 2003 32bit. So you'll need to figure out the exact paths for your environment.
If you aren't using the Premium Edition, the contents of the CodeContractsAfter.targets file is:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Begin CodeTools: CodeContracts: After -->
<PropertyGroup>
<CodeContractsInstallDir Condition="'$(CodeContractsInstallDir)'==''">C:\Program Files (x86)\Microsoft\Contracts\</CodeContractsInstallDir>
</PropertyGroup>
<Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)MsBuild\v4.0\Microsoft.CodeContracts.targets" />
<!-- End CodeTools: CodeContracts: After -->
</Project>
Just paste the above into a file in the folder mentioned ImportAfter folder.
Here's my solution. It's based on issue #368 and stackoverflow:Microsoft Code Contracts without Visual Studio
<PropertyGroup>
<CodeContractsInstallDir Condition="'$(CodeContractsInstallDir)' == ''">$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\packages\DotNet.Contracts.1.10.20606.1\'))</CodeContractsInstallDir>`
</PropertyGroup>
<Import Condition="'$(CodeContractsImported)' != 'true' AND '$(DontImportCodeContracts)' != 'true'" Project="$(CodeContractsInstallDir)\MsBuild\v$(VisualStudioVersion)\Microsoft.CodeContracts.targets"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With