Code Contracts doesn't seem to work on VS2012

I'm reading up on Code Contracts, which at first glance seem to be pretty revolutionary, but I can't seem to get them working. I'm running Windows 8 and Visual Studio 2012 Premium (Release versions of both). I then installed Code Contracts from here by clicking on the Download Code Contracts link.

I then wrote the following code in a brand new console app:

class Program
{
   static void Main(string[] args)
   {
      var answer = Add(0, 5);
      Console.Write(answer);

      Console.ReadLine();
   }

   static int Add(int x, int y)
   {
      Contract.Requires(x > 0 && y > 0);

      return x + y;
   }
}

I expect the compilation to fail, since the first parameter of Add is 0, but the program succeeds and prints 5 out to the console.

I've tried with the default Code Contract settings, and also mucked with stuff a bit to no avail. My current settings look like this:

Any ideas what I'm doing wrong?

UPDATE:

Here's the results from the Build window. It appears it's doing something, but just raising warnings instead of errors. In the video I watched, these things got flagged as compile errors and the program wouldn't even run.

1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------
1>  DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe
1>  CodeContracts: Task manager is unavailable.
1>  CodeContracts: DeleteMe: Run static contract analysis.
1>  CodeContracts: Suggested requires: Contract.Requires(false);
1>  CodeContracts: DeleteMe: Validated:  0.0 %
1>  CodeContracts: DeleteMe: Contract density: 0.87
1>  CodeContracts: DeleteMe: Total methods analyzed 4
1>  CodeContracts: DeleteMe: Methods with 0 warnings 3
1>  CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method
1>  CodeContracts: DeleteMe: Methods with necessary preconditions: 1
1>  CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843
1>  CodeContracts: DeleteMe: Retained 1 preconditions after filtering
1>  CodeContracts: DeleteMe: Inferred 0 object invariants
1>  CodeContracts: DeleteMe: Retained 0 object invariants after filtering
1>  CodeContracts: DeleteMe: Detected 0 code fixes
1>  CodeContracts: DeleteMe: Proof obligations with a code fix: 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning
1>  CodeContracts: Checked 1 assertion: 1 false
1>  CodeContracts: DeleteMe: 
1>  CodeContracts: DeleteMe: Static contract analysis done.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Answers


So, the issue seemed to be a combination of several limitations and gotchas with Code Contracts. Hopefully this answer will help people like me just starting out.

First off, Code Contracts does support Visual Studio 2012 (any version other than Express) since build 1.4.50327.0, though you have to run devenv.exe /setup if your build is older than 1.4.50910.0. See the Release Notes for more information.

The first issue I was having was that the "Cache Results" check box was checked in the "Static Checking" section of the Code Contracts properties tab. This option is on by default, and also requires SQL Server CE to store its cached data, which is not installed by Windows 8, VS2012 or Code Contracts. Unfortunately, your program will continue to compile just fine, and you'd have to manually go dig through the Build output to see the error:

CodeContracts: xxx: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

Unchecking the "Cache Results" checkbox will fix this issue, as would installing SQL Server CE.

The second issue is that Code Contract violations are treated as warnings, and not compile errors. Even if you have "Treat Warnings as Errors" enabled, your program will continue to compile and run successfully. If you have a larger project with tons of Warnings you ignore, it could potentially be difficult to notice these new Code Contract warnings. In the demo video I saw, these warnings were also reflected in the Visual Studio IDE (the calling code had a blue underline), however I don't seem to get this behavior in Visual Studio 2012.

This design decision disturbs me. If I define a contract within my code, that a function must take an integer greater than 0, and I blatantly pass in a 0, this is an error. Not a warning. I broke that contract, plain and simple.

Overall, I'd say Code Contracts is extremely powerful, and could potentially change the way we test software. MS Research definitely did great work. However, I don't think it's really ready for mainstream yet. It takes some tweaking to get working, it doesn't seamlessly integrate into the Visual Studio build process, and it's also pretty slow. On smaller projects, it worked as expected, but when I plugged it in to a larger project, it took a good ten minutes to analyze all the code.


Sir, you will have to enable runtime checking. Under your in the window you posted as an image.

"Full" will mean that all conditions will be checked. The rest are self explanatory.

  • Static checking only runs in the background and checks for any thing it can improve within your code. Caution - it slows your builds down somewhat.

Need Your Help

MessageDialog ShowAsync throws accessdenied exception on second dialog

windows windows-8 windows-runtime winrt-xaml messagedialog

I am trying to implement try again/cancel dialog box in windows 8. The dialog box shows fine the first time, but on clicking try again and failing again, I get a access denied exception on calling