title: Using Code Contracts in library code built with MSBuild
body: <p>I've started using <a href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx" rel="nofollow noreferrer">Code Contracts</a> in all new code I'm writing, such as in a framework library I'm building to help bootstrap <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow noreferrer">IoC</a>, <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" rel="nofollow noreferrer">O/RM</a>, etc., in an ASP.NET MVC application. I've written a simple build script for this framework library that looks like the following:</p>
  
  <pre><code>@echo off
  echo.
  echo Cleaning build output (removing 'obj' and 'bin' folders)...
  for /f "tokens=*" %%G in ('dir /b /ad /s bin') do rmdir /s /q "%%G"
  for /f "tokens=*" %%G in ('dir /b /ad /s obj') do rmdir /s /q "%%G"
  rmdir /s /q build
  echo.
  echo Starting the build...
  call "%VS100COMNTOOLS%\vsvars32.bat"
  msbuild Integration.build /target:Build
  echo.
  echo Done!
  pause
  </code></pre>
  
  <p>This doesn't work. What I end up with in my <code>build</code> folder if I run this is, for whatever reason, assemblies that aren't fully rewritten by <code>ccrewrite</code> alongside <code>.pdb.original</code>, <code>.rewritten</code> and <code>.csproj.FileListAbsolute.txt</code> files that litter the output directory.</p>
  
  <p>What does work is first building the solution in Visual Studio 2010, commenting out line 3 through 7 in the batch file and running it again. I then end up with properly rewritten assemblies and no <code>.pdb.original</code> nor <code>.rewritten</code> files.</p>
  
  <p>What I've deduced from this is that Visual Studio 2010 somehow triggers the Code Contract rewriter properly so the resulting assemblies from the Visual Studio 2010 build is re-used by the command-line MSBuild call, so what my batch script basically does is just copying files to the <code>build</code> directory. Rather useless, in other words.</p>
  
  <p>I've read <a href="https://stackoverflow.com/questions/1209166/why-is-ccrewrite-exe-not-doing-anything-from-the-command-line">this</a>, but Jon's problem seems different from mine since <code>ccrewrite</code> is obviously doing <em>something</em>, but it's just not completing the rewriting for whatever reason. The <code>Integration.build</code> file builds the correct configuration (that has Code Contracts enabled in the <code>.csproj</code> files) and everything else looks right, it just doesn't work properly.</p>
  
  <p>So, I'm wondering: How do I run MSBuild the way Visual Studio 2010 is where <code>ccrewrite</code> does what it's supposed to and doesn't litter my output directory with <code>.rewritten</code> and <code>.pdb.original</code> files? Does anyone have a perfect example of how an MSBuild file doing proper Code Contracts rewriting looks like?</p>