I have a C# PCL project that uses code contracts. However on TeamCity the project fails to build, but it looked like the build was successful locally. However the build log does claim that the build did fail, but it gives no reason at all.
'Fail build on warnings' is disabled.
I get a single strange warning:
CodeContracts: Could not find the method/type 'System.Diagnostics.Contracts.PureAttribute' in assembly '[project path]\packages\Microsoft.Net.Http.2.2.29\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.dll'
Searching google gave me one relevant result with someone who has the same problem. However it has no responses besides bumping.
I also find this line peculiar:
C:\Windows\system32******.dll (1,1): message : CodeContracts: Checked 3154 assertions: 2821 correct (333 masked)
Why is the binary located in system32? I didn't think MSBuild even had write access to that folder...
Best How To :
The issue seems to be that it by default looks for contract types in Microsoft.Contracts (per spec) which doesn't work because Microsoft.Contracts is not what I am using. I'm using System.Diagnostics.Contracts, which is located in mscorlib. It doesn't tell me this, rather it does something non-sensical. I started looking through the source code and I found the offending code but in my opinion this code is of poor quality, so I gave up on actually finding the issue and creating a pull request.
However the solution is simple when you know: you need to override the default for the contract library which is Microsoft.Contracts with mscorlib, so simply add the following to the command line of the static analyzer:
This worked for me at least. I still think this is caused by two bugs in Code Contracts:
- It fails the build because of a warning.
- It tries to locate the
PureAttribute in the wrong assembly.