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
Build/Test Issues
Compilation error that is showing up in NCrunch but not in VS
Started by coffeesmurf on 9,394 views
Remco NCrunch Developer
#846
05 Jan 2012 21:48 UTC
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
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
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.
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 NCrunch Developer
#848
06 Jan 2012 08:33 UTC
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.
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.
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...
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 NCrunch Developer
#851
07 Jan 2012 00:03 UTC
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!
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!
Thanks for confirming this !
And great job with NCrunch :) I'm really liking it !
And great job with NCrunch :) I'm really liking it !
Post a reply
Log in to reply.