Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

CodeContract: Rewrite aborted due to metadata errors
Andrii Litvinov
#1 Posted : Thursday, July 10, 2014 2:38:57 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/12/2013(UTC)
Posts: 8
Location: Ukraine

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Hi Remco,

I have set up Code contracts for projects in my solution and NCrunch fails to build some of them with error described in this topic: http://forum.ncrunch.net...heck-output-window.aspx

I have tried to build project with MSBuild in NCrunch's workspace and it build fine with no errors.

I use v2.7.

How can I find out what goes wrong with those projects?

Andrii

EDIT: I have tried to unload files from projects one-by-one and comment-out lines of code and I was able to find exact lines caused build to fail with the error, but I have completely no idea why builds fail. I couldn't find any relations or patterns.
Remco
#2 Posted : Thursday, July 10, 2014 9:37:25 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,979

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for posting.

The first thing that jumps into my head is that NCrunch's instrumentation may be creating an IL arrangement that Code Contracts is unable to process.

Most of the testing of NCrunch's instrumentation has naturally been performed directly using the JIT, which uses a different interpreter to Code Contracts. This means it is possible for code to work for one but not the other.

If you're able to share with me a piece of code that can consistently reproduce the problem, I may be able to get it fixed. Otherwise I think the best option may be to try using code coverage suppression comments around the code in question. This will disable instrumentation for the block of code, likely allowing Code Contracts to work. If this doesn't fix things, does disabling the 'Instrument Output Assembly' project-level configuration setting for the project make any difference?
Andrii Litvinov
#3 Posted : Friday, July 11, 2014 5:47:36 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/12/2013(UTC)
Posts: 8
Location: Ukraine

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Hi Remco, thanks for reply.

I have tried to use comments and set Instrument Output Assembly with no effect.

Two of three projects that fail don't use CodeContract yet. And third fails in the part that don't use CodeContract either.

The code in question is below. If pass null instead of email.ValidationUrl assembly build fails. Second class resides in different assembly.

I've tried to rename/add new property to ForgotPasswordEmail class and build always fails when there is a property access. Two other assemblies has other reasons to fail.

Code:

[UsedImplicitly]
public class ForgotPasswordEmailBuilder : MandrillEmailBuilder<ForgotPasswordEmail>
{
    ..
    protected override void InternalBuild(ForgotPasswordEmail email,  EmailMessage emailMessage)
    {
        emailMessage.AddRecipientVariable(email.Recipient.Email, "CHANGEPASSWORDLINK", email.ValidationUrl);
    }
}


Code:

[Serializable]
public class ForgotPasswordEmail : Email
{
    public string ValidationUrl { get; private set; }

    public ForgotPasswordEmail([NotNull] Recipient recipient, [NotNull] UrlHelper url, [NotNull] string key)
        : base(recipient, () => Resources.Account.RestorePassword.EmailSubject, Template.ForgotPassword)
    {
        if (url == null) throw new ArgumentNullException("url");
        if (key == null) throw new ArgumentNullException("key");

        ValidationUrl = url.Action("ChangePassword", "Account", new { id = key }, UrlBuilder.Protocol);
    }
}


Is it possible to see somehow the reason of fail, some kind of log or whatever?
Andrii Litvinov
#4 Posted : Friday, July 11, 2014 7:01:01 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/12/2013(UTC)
Posts: 8
Location: Ukraine

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Please find an example of project that consistently reproduces this error, hope it will help: http://1drv.ms/TWtRKu
Remco
#5 Posted : Friday, July 11, 2014 12:13:40 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,979

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Thanks for sharing the sample project. I don't think there's any way I could have properly analysed this problem without it.

Deeply buried in the MSBuild output was the following:

ccrewrite : error : Rewrite aborted due to metadata errors. Check output window
Assembly reference not resolved: System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
Could not resolve type reference: [System.Web.Mvc]System.Web.Mvc.UrlHelper.
elapsed time: 1765.7232ms
C:\Program Files (x86)\Microsoft\Contracts\MsBuild\v12.0\Microsoft.CodeContracts.targets(254,5): error MSB3073: The command ""C:\Program Files (x86)\Microsoft\Contracts\Bin\ccrewrite.exe" exited with code -1.

It seems that ccrewrite is failing because of an assembly reference error. The System.Web.Mvc DLL isn't used by the project being built, but it is an indirect dependency via another project referenced using a projectreference. ccrewrite is relying on indirect references being co-located with referenced assemblies - something that NCrunch won't do by default because this prevents it from optimising your build.

To make this work, you will need to turn on the 'Copy referenced assemblies to workspace' NCrunch project-level configuration setting for projects that are being referenced by the project ccrewrite is operating on. In your sample solution, turning this setting on for the Emails project solved the build problem.

This probably would have been much easier to solve if Code Contracts were reporting its error information correctly. Unfortunately it seems to have output the critical piece of information as a simple build message, thus excluding it from the visible failure message.

Thanks again for supplying the sample solution. This really made life much easier.


Cheers,

Remco
1 user thanked Remco for this useful post.
Andrii Litvinov on 7/11/2014(UTC)
Andrii Litvinov
#6 Posted : Friday, July 11, 2014 12:30:21 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/12/2013(UTC)
Posts: 8
Location: Ukraine

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Thanks a lot for your investigation! It did help.

It has solved the issue for one of my projects. But two other projects still fail because of other issue of this kind.

Is there any way I can check MSBuild output for NCrunch build myself so I can find cause of other issue?

Otherwise I can try to enable 'Copy referenced assemblies to workspace' for my projects one-by-one to find which will make it to work =).

Once again, thanks a lot!
Remco
#7 Posted : Friday, July 11, 2014 12:49:56 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,979

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
If you set the 'Log Verbosity' global configuration option to 'Detailed', NCrunch will allow you to inspect the detailed MSBuild output inside the processing queue. Simply find the project that has failed to build inside the queue, and you should see a full dump of the build output below it.

I'd definitely recommend setting the 'Copy referenced assemblies to workspace' on for ALL the projects in your solution. You can then selectively disable them one at a time to see which ones actually require it. Unfortunately this setting will have quite an impact on your build times, so it's better to avoid it if at all possible.
Andrii Litvinov
#8 Posted : Friday, July 11, 2014 2:12:35 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/12/2013(UTC)
Posts: 8
Location: Ukraine

Thanks: 2 times
Was thanked: 1 time(s) in 1 post(s)
Great, I have managed to solve all issues in my solution now.

I never used Processing queue before and wan't aware that it's possible to view log there, very nice.

I have enabled setting to copy dependencies to workplace for a few projects and it works like a charm now.

Thank you, Remco!

Cheers,
Andrii
1 user thanked Andrii Litvinov for this useful post.
Remco on 7/11/2014(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.063 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download