27

I've got Code Contracts working fine from inside Visual Studio 2010, but I can't get ccrewrite.exe to do anything useful from the command line. Here's a sample app:

using System.Diagnostics.Contracts;

public class Dummy
{
    public static void Main(string[] args)
    {
        Contract.Requires(args.Length > 0);
    }
}

I then compile the code and run ccrewrite.exe on it:

> csc /debug+ /D:CONTRACTS_FULL Dummy.cs

> ccrewrite /o:RewrittenDummy.exe Dummy.exe
elapsed time: 61ms

There's no RewrittenDummy.exe file afterwards.

I've tried loads of options, but nothing's making any difference. A few things I've noticed:

  • It's definitely loading Dummy.exe, because if I specify a non-existent file, it dies
  • Dummy.exe definitely contains references to Contract - if I run it with no arguments, it fails appropriately (but the error message hasn't been filled in as I'd expect if it had been rewritten)
  • Using postconditions and invariants makes no difference

I've tried turning warnings and verbosity up, and that doesn't help at all What am I doing wrong?

(Also asked as a question in the Code Contracts forum. I'll add any relevant answers here myself.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194

1 Answers1

19

Okay, this has been answered in the MSDN forum. For once, it wasn't really me being entirely stupid - it's something that could really do with a warning.

Read the forum post for full details, but the basic problem is that ccrewrite couldn't find the contract classes: it was looking in the .NET 3.5 CLR version of mscorlib instead of the .NET 4.0 one.

This can be fixed by explicitly listing the path to the relevant assembly:

> ccrewrite /o:rewrittendummy.exe dummy.exe 
  /libpaths:%SystemRoot%\Microsoft.Net\Framework\v4.0.20506
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Where I add this line? – Pedro77 Jul 26 '14 at 22:05
  • Ive commented all my Contracts call and returned to the old and good != null... ccrewrite is too buggy. – Pedro77 Jul 26 '14 at 22:10
  • @Pedro77: It's hard to tell exactly what your problem is from these comments. Perhaps open a new question? – Jon Skeet Jul 27 '14 at 06:29
  • 1
    Oh, never mind. My doubt here is where should I write your solution: " > ccrewrite /o:rewrit..." ?? Can you please clarify? – Pedro77 Jul 27 '14 at 18:02
  • @Pedro77: Well that's if you're running it from the command line. If you're just using Visual Studio, you'll need to work out which project options correspond to which command-line options. – Jon Skeet Jul 27 '14 at 19:47
  • @JonSkeet, Isn't there a simple way to set up code contracts to throw a plain old CLR exception when `Contract.Requires(obj != null);`? – Shimmy Weitzhandler Apr 26 '15 at 00:23
  • @Shimmy: It will do that already, won't it? (So long as the rewriter is set to check everything.) I'm not quite sure what you're asking - but it's been a long time since I've used CC anyway. – Jon Skeet Apr 26 '15 at 06:51
  • @JonSkeet looks like it doesn't. I'm working on an ASP.NET MVC project and I get complains about ccwriter. – Shimmy Weitzhandler Apr 26 '15 at 19:52
  • 1
    @Shimmy: Sounds like you should ask a new question with plenty of detail. – Jon Skeet Apr 26 '15 at 20:19