пятница, 23 августа 2013 г.

Completely Disable Static Code Contract Checking on your PC

Having started using Code Contracts on our project, build time increased dramatically.

Even with “Check in Background” turned on – the build is still slow and causing major problems:
  1. *.pdb and *.dll files sometimes become locked
  2. Error 1 The command "C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite" "@Application1ccrewrite.rsp"" exited with code -1 (even from unit tests)
  3. Process “ccheck.exe” just hang and consume 100% CPU even after build has cancelled

The solution was to do “Clean Solution”, remove all output folders (sometimes with Unlocker, and that is sic), and do clean rebuild.

It is just unacceptable if I am debugging only one Unit Test from one specific project to clean and rebuild everything.

There are several ways to disable checking.


Normal way

Project Properties. If you have full control over *.csproj file you can just uncheck it.

If your team wants to check code contract, you cannot simply disable it in global project properties.

Another drawback is that you need to do it for every project.


Msbuild way

If you only use build scripts for compilation then it is simple to pass variable to msbuild:
msbuild .exe "AllBuild.proj"  /m:8 /t:build /p:CodeContractsRunCodeAnalysis=false,RunCodeAnalysis=Never,CodeContractsReferenceAssembly=DoNotBuild



Visual Studio and Msbuild

If you do not want to pass parameters to msbuild or you are building from Visual Studio, there is a way to suppress static code contracts check and code analysis.

Using this approach, you will disable it machine-wide.

Notice: each *.csproj file contains <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />.
It is possible to modify Microsoft.CSharp.targets in a way it will emulate passing parameters to msbuild

On my PC and.Net 4 full path to *.target and msbuild is "C:\Windows\Microsoft.NET\Framework\v4.0.30319\"
Open “C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets”
Add new PropertyGroup inside Project like:

<Project>
   ...
  <PropertyGroup>
    <CodeContractsRunCodeAnalysis>false</CodeContractsRunCodeAnalysis>
    <RunCodeAnalysis>Never</RunCodeAnalysis>
    <CodeContractsReferenceAssembly>DoNotBuild</CodeContractsReferenceAssembly>
  </PropertyGroup>
  ...
  <!-- About 5k lines bellow... -->
</Project>

All your builds now on your pc (from either MSBuild or Visual Studio) will skip code and static code contracts analysis, so you do not need to pass arguments, from Command Line, and all builds will skip analysis.


Note: all assemblies will still be rewritten so you cannot use "Edit And Continue" feature. I hope to find solution to it soon.

четверг, 15 августа 2013 г.

How to fix Ncrunch in Visual Studio 2013 Preview for code analyse

If you have a following error in NChrunch 2013:

..\..\..\..\..\..\..\..\..\..\..\..\..\..\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets (298)#0: 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'.
..\..\..\..\..\..\..\..\..\..\..\..\..\..\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets (298)#1: The "CodeAnalysis" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
You can easily fix it!

Backup "Microsoft.CodeAnalysis.Targets" in c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis : i named it  "Microsoft.CodeAnalysis.Targets_2013" 

Copy file from 2012 to 2013 folder
c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis\Microsoft.CodeAnalysis.Targets
=> to
c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.Targets


Open for editing: (best to open in VS)
  c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.Targets


Replace all occurences of  
AssemblyFile=".\FxCopTask.dll" 
to  the old vesion of VS =>
AssemblyFile="c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis\FxCopTask.dll"

So it will look like 
    <UsingTask TaskName="Microsoft.Build.Tasks.NativeCodeAnalysis" AssemblyFile="c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\CodeAnalysis\FxCopTask.dll"/>


That's all. 

Resynchronize NChrunch.