Contract.Assert do not throw compilation error - c#

I am trying to add validation for my type at compilation time using Contract but unfortunately it do not work. For example code below do not give compiler error while compiling project. Should I enable something?
Contract.Assert(false, "Invalid state!");

Yes, you need to enable the static checking. Go into the project properties and visit the "Code Contracts" tab. Of course you'll need to have installed the static checker extension first, which IIRC is only available for Visual Studio Ultimate. See the user documentation for details of the exact options available.
Note that this happens after the initial compilation phase, so sometimes you'll see a delay between the first part of the build and the results of the static checker. (It's not really a "compiler" error - it's a "contract checking" error.)

Related

Error CS7038 (failed to emit module) only in Edit and Continue

I'm debugging a .NET 4.0 application in Visual Studio 2015. My application builds and runs fine, but when I try to edit and continue while running under the debugger, regardless of what changes I make or where I make them in my main project, I get a dialog that says:
Edits were made which cannot be compiled. Execution cannot continue
until the compiler errors are fixed.
As an example of the sort of change I'm talking about, I've tried adding this line in various methods:
Console.WriteLine("foo");
When I look in Visual Studio's Error List pane, I see only one error, CS7038, with the description "Failed to emit module '<my app name>'." No filename, line number, or character is given. There are no squiggly red underlines in my code. If I stop the running application, build with the changes, and run again, everything builds and runs just fine. So there seems to be some discrepancy between what the build-time compiler and the edit-and-continue compiler consider acceptable.
Does anyone know of a way to get more information about why the compile fails in Edit and Continue mode? I read something about attaching to and debugging the VBCSCompiler process, so I tried that, but even with all exception types set to break when thrown, the attached VS never broke.
I'm not sharing any code because this isn't a question about my code but rather about strategies for finding out what the Edit and Continue compiler thinks is wrong, and for all I know the source of the compiler error could be anywhere in my entire project.
Edit:
As mentioned in the comments, I was able to attach a debugger to Visual Studio and break when an exception was thrown upon clicking "Continue" after editing code. The exception was a System.NotSupportedException with the following message: "Changing the version of an assembly reference is not allowed during debugging". It listed the name of the assembly in question, which was a small VB.Net project used by my application, which is mostly in C#. I'm trying to build up an MCVE to submit to Microsoft, but currently I'm unable to reproduce the problem in a smaller solution with just one VB and one C# project.
Edit 2:
I've found a workaround and self-answered the question in case anyone else ever encounters this weird problem, but I'm reserving the "Answered" check mark for anyone who can explain what's going on (why the compiler thinks the version number of the referenced project has changed during the edit).
I found a workaround for the problem, but I don't fully understand what was going on. In the VB.NET project whose assembly version the Edit and Continue compiler said was changing, there was a file called "AssemblyInfo.vb". That file contained the following line:
<Assembly: AssemblyVersion("3.0.*")>
The assembly version can also be set in the Project Properties, via the "Assembly Information" button in the Application tab:
When I removed the AssemblyVersion line from AssemblyInfo.vb, my Edit and Continue problem went away. At first I thought this was because the fields in the Assembly Information window were saved to a different file from AssemblyInfo.vb and there was some conflict between the two, but now I see that the Assembly Information window is just a handy way to edit AssemblyInfo.vb: if I delete the line in AssemblyInfo.vb, it gets cleared in the Assembly Information window.
After some more experimentation, it appears that the asterisk in the version number is the culprit. If I fully specify the assembly version, my Edit and Continue problem goes away. And the referenced project has to be a VB.NET project. I tried the same setup with a C# project, and I could Edit and Continue just fine.
This appears to be very much an edge case, and I'll submit a bug report to Microsoft, but in the meantime I'd love to know what is actually going on with the compiler--why it's getting two different assembly versions of an assembly that really shouldn't need to be recompiled during the debugging.... If you have a good explanation for what's happening, please add it as an answer.
Edit: here's the bug report I filed.
This happened with me in a .net 4.8 app with Visual Studio 2019.
I have a mix vb and cs projects, here the problem appears when a vbproj references a csproj that uses the wildcast operator '*' to specify the version of the assembly.
As commented above by #Wai-Ha-Hee, the wildcast uses the current time, I belive when VS rebuild the application to apply the edits you have made, the version of the assembly changes causing the error.
In assemblyInfo file (of the project present in error) Change:
[assembly: AssemblyVersion("1.0.*")]
To:
[assembly: AssemblyVersion("1.0.0.0")]
It Solved for me.
An important thing to say is the use of wildcast '*' make the assembly non-deterministic, it means each build produces a different assembly. This has been considered bad practice because build the source code in the same conditions generates different assemblies.
In Visual Studio 2019:
New csproj/vbproj with non-sdk style projects file are generated with:
<Deterministic>true</Deterministic>
And new csproj/vbproj with Sdk style projects file omits this line but assumes deterministic as default too.
I recommend considerate other ways to version the assembly.
More about Deterministic:
http://blog.paranoidcoding.com/2016/04/05/deterministic-builds-in-roslyn.html
https://reproducible-builds.org/
One of my C# projects in a mixed solution was .NET Framework 2.0 (while others - both C# and VB.NET - were .NET Framework 4). After I changed it to .NET Framework 4 it began to work.

Throw build error if a specific word is found anywhere within the project code or html views

I have an MVC project that is prone to spelling mistakes with short acronyms. i.e. people writing "ZED" instead of "DEZ".
Is it possible to get Visual Studio to throw an error during a build if a specific word is found anywhere within the code being built? My biggest concern is checking to see if it appears in any of the views more than anything else. i.e. If any view contains "ZED" the compiler would throw an error and prevent anything from being published.
Any ideas?
You can use code analysis automation tools like fxcop or stylecop. Build your custom dictionary of allowed words and enable the code analysis policy during builds.

Compilation succeeded even though there were some errors from Roslyn Diagnostic Analyzer

The DiagnosticAnalyzer is a custom Roslyn based extension whose DiagnosticDescriptor with DiagnosticSeverity.Error is as shown below
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Description, MessageFormat, Category, DiagnosticSeverity.Error);
When the extension is used it does show the red squiggles over the codes that is against the Diagnostic Analyzers custom rule emphasising that it is an error, it is even shown in the Error List window of Visual Studio.
But when compiled, this code gets a compilation succeeded message in the Output Window of Visual Studio. This is working against the whole concept of emphasising an error as severity for a diagnostic analyzer.
If this is the default way of working for Roslyn, then do we have a work around for stopping the compilation.
I'm using:
Visual Studio 2013 Update 3
Roslyn End User Preview.vsix
VSIX extension containing the DiagnosticAnalyzer made using Roslyn SDK Project Templates.vsix
The integration of diagnostics into the actual build pipeline has been happening as part of VS "14". If you try the same scenario there, it will cause a compilation error.
Additionally, diagnostics are now attached to projects and distributed via NuGet so that the errors will happen consistently for the entire team, continuous integration build, etc.
I'm using VS2015 Update 3 and this issue is still there. I wanted to enforce local variable and parameter naming conventions, it turned out Roslyn doesn't support these by default, only by workaround: SO link
Then I wanted to fail the build if there is such an error, but Roslyn is not capable of this.
Roslyn could have been a very robust and powerful tool, but it is still failing in the most basic tasks. What a pity.
(For the moderators: I still don't have 50 rep to comment and I don't think I will ever have)

Visual Studio Skip certain errors while compiling

Is there a way to have Visual Studio 2010/2012 skip over certain compiler errors and just keep compiling the rest of the code.
I keep getting this error for DNN and I don't need it.
'ControlName' is not a valid value for attribute 'classname'
No, a compiler error means that there is something fundamentally wrong with your code, such that the compiler is unable to determine what executable code to generate. Syntax and semantics errors must be fixed and cannot be skipped to achieve successful compilation.
Warnings, however, can be skipped or suppressed because they indicate a potential mistake or issue, rather than a blocking one.

fxcop execution failed with return code '9' while running sonar

I am using sonar code analysis tool for analyzing my c# language projects.. It is working fine for some projects. But for some projects, while uploading in sonar, i am getting Fxcop execution failed with return code '9' or '513' or '12' and saying that execution failure...
Check FxCop documentation about exit codes to know more about those failures: http://msdn.microsoft.com/en-us/library/bb164705%28VS.80%29.aspx
Fxcop error code 9 means that Fxcop analysis was failed for one of the dll or project in the solution.
Sometimes Fxcop Analysis failed for some projects in the solution due to various reasons. One of the reason may be that Fxcop requires some DLLs to complete the analysis. In this case the Fxcop analysis for complete solution gets failed and Fxcop exits with error code 9. This results in failure of complete sonar analysis.
Solution: We can make use of the sonar property sonar.fxcop.assemblies to run Fxcop analysis.Provide the comma separated paths of only those dll as this property value on which we want to run Fxcop analysis.
eg: sonar.fxcop.assemblies=path of dll1,path of dll2
Put this property in the sonar.properties file which resides in the same directory in which solution file lies. Don't provide the path of dll which is causing failure.
Since Fxcop will not do analysis on the problematic dll, the sonar analysis will not exit now with the error message "FxCop execution failed with return code 9 in sonar."
For complete problem reason , analysis and solution click here.
I found a solution to this error by checking the FxCop report file (fxcop-report.xml, in my case it was stored in the .sonar folder, under my projects root folder).
In this file I could to see that the problem was with a rule not being loaded (CA2001) and, then, a object null reference happened.
I changed the FxCop instalation, from my standalone instalation do Visual Studio's instalation. The versions were differente.
Problem solved.

Categories