2014-11-26

Code Contracts and the Visual Studio 2015 Preview

I have been attempting to get a project to compile in the Visual Studio 2015 Preview. Unfortunately, it isn't working because I use Code Contracts throughout the project, with the Contract.Requires() method. The current version of Code Contracts does not install or work with the Visual Studio 2015 preview.

I found one work-around, but it only got me part of the way there. From this post about the Visual Studio 2013 Preview, I discovered that you can copy a file from a working installation of Visual Studio into the correct directory for Visual Studio 2015 and the build process will attempt to use Code Contracts. The trick is to copy the file C:\Program Files (x86)\MSBuild\12.0\Microsoft.Common.Targets\ImportAfter\CodeContractsAfter.targets to the folder C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.Targets\ImportAfter\

However, that only solves the first hurdle. The Code Contracts rewriter goes through your compiled code and modifies the IL (Intermediate Language) to  support the Code Contracts. However, the C# Compiler used by Visual Studio 2015 is the Roslyn compiler. When I try to compile, the Code Contracts tool ccrefgen.exe produces the error: AsmMeta failed with uncaught exception: Unknown custom metadata item kind: 5.

The reason is that the Roslyn compiler is producing some meta data that the Code Contracts tools don't handle correctly. There is a switch statement that handles cases for the numbers 1 through 4, but not 5. This article describes the problem and a possible convoluted fix by patching the code contracts tools to ignore the value 5.Trying to update the code is not easy.

I will update this article if I get things working. [8/11/2015 It is FINALLY fixed in the latest release!]

The right solution, of course, is for the Code Contracts people to support the preview. Vote up this forum request so we can get that to happen.

1 comment :

  1. Hi, link to the code contracts pathing article is http://dissipatedheat.com/2014/10/28/curious-case-of-code-contracts-and-roslyn/

    ReplyDelete

Note: Only a member of this blog may post a comment.