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

Notification

Icon
Error

Compilation error that is showing up in NCrunch but not in VS
coffeesmurf
#1 Posted : Thursday, January 5, 2012 8:09:44 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/5/2012(UTC)
Posts: 4
Location: Montreal, Canada

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

I am trying to understand why I am seeing a particular compilation error in NCrunch (1.36.0.10b) and not in Visual Studio 2010. The situation I have is the following:

The code (C#) that I am currently looking at as an unused variable declared. In Visual Studio, I get a Code Analysis warning if I compile in Debug, but I don’t get it in Release (since the unused variable gets optimized away).

I have set the UseBuildConfiguration to "Release" (without the quotes). What I see in the workspace is that the assemblies do get created in the bin\Release folder, which is the build output location of my Release configuration. The debug configuration outputs to bin\Debug.

So it looks like NCrunch is correctly picking up and using the Release build configuration, but still I am getting the compilation error in NCrunch.

I tried running MSBuild from the command line to compare. If I build the Release configuration I am not getting the warning.

Sure I could just remove the unused variable, and I will, but any thoughts on what’s going on? And is there a way for me to see what NCrunch is actually running so that I can try and reproduce this in the command line?

Thanks!
Phil
Remco
#2 Posted : Thursday, January 5, 2012 9:48:10 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Hi Phil,

Thanks for posting! Something that you can try doing is running MSBuild against the project as it exists in NCrunch's workspace. If you right-click on your failing build, you'll see an option to browse to the workspace using explorer. This should allow you to find the location on disk that NCrunch is using to build your project, and the project file shown here will also contain a number of manipulations NCrunch makes to it.

What you've described doesn't sound like normal behaviour, so there must be another variable hidden in here somewhere that is affecting the way the warning is processed. NCrunch doesn't do any manipulation of the 'warnings as errors' flag, so I'm curious as to why this is happening.

Something you'll notice is that if you build the application using a release configuration, you won't get any debugging information and NCrunch likely won't instrument your assemblies (so you won't get code coverage). The best way would be to have NCrunch work with your debug configuration by suppressing the warnings as error flag. There’s an MSBuild property that controls this. You can make the property conditional so that it won’t apply when NCrunch is running.

If you open each of your .proj files in a text editor, look for:

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

Change this to:

<TreatWarningsAsErrors Condition="$(NCrunch) != '1'">true</TreatWarningsAsErrors>

I think this should do the trick for you. Let me know how it goes.


Cheers,

Remco
coffeesmurf
#3 Posted : Thursday, January 5, 2012 10:44:59 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/5/2012(UTC)
Posts: 4
Location: Montreal, Canada

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Thanks for the input! I managed to find the following:

When running with MSBuild, I do get the error even when I specify the Release configuration. So I was wrong about what I said earlier. The behavior between NCrunch and msbuild command line is consistent :)

Looking at the project file, for the Release build, I have: <TreatWarningsAsErrors>true</TreatWarningsAsErrors>

But I also have the following specified outside of any particular build configuration:

<PropertyGroup>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>

So I guess what is strange right now is that MS Build/NCrunch is correctly picking up this Code Analysis warning whereas VS is not identifying this in Release. Makes sense that the "unused variable" is not a problem for the Release build with Optimize set to true, but why would that be any different when using MSBuild/NCrunch is puzzling...

Using the NCrunch != 1 condition does get NCrunch to run, as long as I put it on "CodeAnalysisTreatWarningsAsErrors". But ideally I would like it if NCrunch would fail the build when CA warnings are found, so I am inclined to letting that as is and finding out why MSBuild and VS are giving different results.

Remco
#4 Posted : Friday, January 6, 2012 8:33:06 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
You might find that Visual Studio is taking a different build path to MSBuild. Visual Studio has a tenancy to inject certain properties to modify build behaviour, and while I'm sure there are good reasons for this, it does create all kinds of confusion.

NCrunch itself is designed to follow normal MSBuild logic as closely as possible, as it needs to be able to work out-of-process from the IDE.

You might want to try setting your release build to output trace information with a high verbosity (i.e. MSBuild command line: /v:d) to help you find out why the execution paths are different.
1 user thanked Remco for this useful post.
coffeesmurf on 1/6/2012(UTC)
coffeesmurf
#5 Posted : Friday, January 6, 2012 3:14:40 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/5/2012(UTC)
Posts: 4
Location: Montreal, Canada

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Thanks very much for your help, it's greatly appreciated! :)

I did some more investigation here this morning and from what I can tell it looks like NCrunch is forcing "/P:Optimize=False" even if the project's build configuration is setup with "<Optimize>true</Optimize>". Is this possible?

The unused variable that is optimized out in my case looks something like this:

Class1 c = new Class1
{
MyProperty = "Variable c is not used as far as Code Analysis is concerned when the Optimize switch is specified"
};

I don't get the CA warning when I MSBuild this code in the following cases:
- against the Release configuration
- against the Debug configuration with Optimize set to true

But I do get the CA warning when I MSBuild this way:
- against the Debug configuration
- against the Release configuration with the Optimize switch set to false

That's why I am thinking that NCrunch is explicitly setting Optimize=False...
Remco
#6 Posted : Saturday, January 7, 2012 12:03:19 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 929 times
Was thanked: 1256 time(s) in 1169 post(s)
Yes! Nice deduction, that is exactly what is happening.

NCrunch is setting the optimize switch to ensure it has debugging information to instrument with. I'd actually forgotten this piece of code existed as it's rather old .. my apologies!
coffeesmurf
#7 Posted : Monday, January 9, 2012 7:09:36 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/5/2012(UTC)
Posts: 4
Location: Montreal, Canada

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Thanks for confirming this !

And great job with NCrunch :) I'm really liking it !
1 user thanked coffeesmurf for this useful post.
Remco on 1/9/2012(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.053 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download