Code Analysis CA1307 & CA1310 not working in new .NET 6 projects - c#

I'm using Visual Studio 2022 (17.4.4), and when I create a new empty .NET 6 project, Code Analysis doesn't work.
According to MSDN, Roslyn Code Analyzers are automatically enabled for every new .NET 6 project:
Starting in Visual Studio 2019 16.8 and .NET 5.0, these analyzers are
included with the .NET SDK. Analysis is enabled, by default, for
projects that target .NET 5.0 or later.
Here's the steps I followed:
Start Visual Studio
Create a new .NET 6 Console Project
Paste the code below
Click Analyze -> Run Code Analysis. I would expect to get CA1307 or CA1310 - however, I get neither.
How can I get Code Analysis to work here?
Console.WriteLine("asdf".IndexOf("/")); // expecting CA1307 or CA1310 here
Console.WriteLine("Hello, World!");

Since 5.0 CA1307 is Disabled and CA13010 is Hidden you can have a complete view of Roslyn rules here

Related

VS 2022 - cannot change Target Platform to .Net 6.0 for a project created in VS 2019

Just installed VS 2022 on a machine, after I removed VS 2019 (VM with limited space).
VS 2022 installed fine. I can create a .Net 6.0 project and compile it - no problem.
I can open the project I created in VS 2019 - and compile it - no problem.
But - when I try to change the Target Platform from .Net 4.6 to 6.0 - I do not see anything above .Net 4.8 in the drop down list.
This is VS 2022 - and I CAN create a new .Net 6.0 project and compile it.
And when I go into that New Project's Properties - I can see .Net 6.0 in the dropdown...
So - I think I am missing a step to get my older 2019 project - fully upgraded to 2022. I loaded the project in 2022 - but when I look at the properties for that project, the screen looks like it did in 2019 --- but for a new project the properties screen looks VERY different (as you can see in the two screen shots attached).
Does anyone have any thoughts as to what steps I might be missing? I loaded .Net 6.0 SDK on this machine (hence why I can create a .net 6.0 project and compile it :)
Are there other steps to making an older project work like it was created in VS 2022 - and then make it use .Net 6.0 ???
Any thoughts would be much appreciated!!
There is no direct upgrade from .NET Framework (any version) (but there is a tool for some types of projects) to anything .NET Core. And .NET 5 & 6 are based on Core.
Additionally, VS2019 does not support .NET 6 (as of .NET 6 RTM). You need VS2022 for that.

Build server of an old ASP.NET 4 application fails building new C# 7, but it works in development

I recently start to work on a legacy ASP.NET codebase based on .NET framework 4.0. We managed to pass everything from Visual Studio 2012 to VS 2017, updated the build server with a new version of Jenkins and installing .NET framework 4.7.x.
Locally we can write C# code of the newest version (7.3) and the build works (VS doesn't use MSBuild if I remember right), but when we deploy on the build server the build fails because there MSBuild cannot recognize constructs newer than C# 4.0. To avoid mistakes I fixed the lang version to 4.0 (advanced build properties on projects), so if I write too new C# VS blocks me in dev, but we would like to start using new C#.
We also tried to fix C# 7.3 directly in the project (<LangVersion>7.3</LangVersion> in PropertyGroup inside csproj) and the but ToolsVersion property of Project element (csproj) to 14.0, but then building we MSBuild fails with the error:
CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3,
4, 5 or Default
Here it's explained that what I want to do it is possible: https://www.dotnetcurry.com/dotnet/1427/build-apps-different-dotnet-framework-versions
No matter which .NET framework version we target in the project, the
C# language version in use will not change. That’s fine because the
vast majority of language features that were introduced in later
versions of the language don’t depend on the CLR or specific APIs.
They are only syntactic sugar and the bytecode generated by the
compiler will still work in .NET framework 2.0.
Anyone have an idea of what mistake are we doing?
The problem was that on the build server MSBuild wasn't properly installed and build scripts got an old one.
Installing Visual Studio 2017 Build tools and fixing the path on the script we solved.
After we had the problem "The “GetReferenceNearestTargetFrameworkTask” task was not found" we solved like explained here: The "GetReferenceNearestTargetFrameworkTask" task was not found
(the right answer depends on what strategy have you used to install VS Buld tools).

Is StyleCop 4.7 compatible with C# 6.0 code

I installed StyleCop 4.7 for developing customized rules, to analyze our C# code.
We write the code in Visual Studio 2015. I can analyze my C# 5.0 (Visual Studio 2013) code, and I get correct response, but when I analyze my C# 6.0 (Visual Studio 2015) code, StyleCop raises an exception for my rules.
Now my question is:
Is StyleCop 4.7 compatible with C# 6.0 code? If not, do we have any solution?
TL;DR - Yes. StyleCop 4.7 was updated to be compatible with C# 6 starting with 4.7.51 (Beta) released on 13th March 2016.
There was a handover to new custodians back in January 2016, and a new version 4.7.50 alpha has been released 3rd Feb 2016, but as yet does not include C# 6 support.
4.7.51 (Beta) was released on 13th March 2016 and the latest stable is 4.7.54 released May 13th.
Here are three other options that work with Visual Studio 2015:
1. Visual StyleCop
Visual StyleCop, is an extension to Visual Studio. This gives you the right click functionality into Visual Studio 2015 that installing StyleCop gave you for 2013 and below.
This is available by adding an extension into Visual Studio:
It is an active project and will give you StyleCop on C# 6 features. There is a package - Visual-StyleCop.MSBuild, that will give you MSBuild integration.
It is possible to write custom rules with Visual StyleCop.
2. StyleCop Analyzers
StyleCop.Analyzers are built upon Rosalyn and make use of the Analyzers feature of Visual Studio 2015. They are available on NuGet:
This would add them into the rules in the relevant *.ruleset file (same place as CodeAnalysis rules, accessible from the project page:)
giving:
and you can run them via
Which has the same effect as right click, Run StyleCop, used on previous versions and with Visual StyleCop.
Giving:
The source code is available on GitHub, so adding custom rules should be possible.
3. Resharper, with StyleCop by Jetbrains
Install:
Resharper 10.0.1
StyleCop by JetBrains 4.8
Which gives this in R# options:
This gives you syntax highlighting, but no right click run, and no build integration. Additionally, R# is not a free product.
You can extend it using plugins, but it doesn't feel as customizable as the other two options presented.
Version 4.7.51 and later should support C# 6:
Added initial (and untested) support for C# 6.0 syntax
http://stylecop.codeplex.com/releases/view/620339
So the current stable build should support it, too: http://stylecop.codeplex.com/releases

How to set the .NET Version for VisualStudio2015 (Code)

Some people in our team are using VisualStudio 2015 while the rest is still using 2013 (both with ReSharper 9.1).
The Target Framework in the project properties is set to .NET Framework 4.5.1.
My Problem: I can still use code like
public int X() => x;
which is a .NET 4.6 feature. When I build the project, it also runs (I guess because it's more or less syntactical sugar, so the compiler makes code that doesn't require .NET 4.6).
My colleagues however are not very amused, when they check out my changes in Visual Studio 2013 ;-)
Is it possible to get warnings / compile errors in Visual Studio 2015 for using .NET 4.6 features?
That is an expression-bodied member and it is a new language feature introduced in C# 6.0.
The language and the framework/runtime libraries are versioned separately.
What you really want to do is change the language version.
In the Solution Explorer, right-click on the project name, then click the menu item entitled Properties
Click on the Build tab, and then the Advanced button. A window should appear.
From the Language Version dropdown, choose C# 5.0.
Hit the OK button, then re-build.

How to enable C# 6.0 feature in Visual Studio 2013?

I was going through the latest features introduced in C# 6.0 and just followed an example of auto property initializer,
class NewSample
{
public Guid Id { get; } = Guid.NewGuid();
}
but my IDE did not recognize the syntax.
I am wondering how I could enable C# 6.0 in Visual Studio 2013. The Target framework I am using is 4.5.1.
Under VS2013 you can install the new compilers into the project as a nuget package. That way you don't need VS2015 or an updated build server.
https://www.nuget.org/packages/Microsoft.Net.Compilers/
Install-Package Microsoft.Net.Compilers
The package allows you to use/build C# 6.0 code/syntax. Because VS2013 doesn't natively recognize the new C# 6.0 syntax, it will show errors in the code editor window although it will build fine.
Using Resharper, you'll get squiggly lines on C# 6 features, but the bulb gives you the option to 'Enable C# 6.0 support for this project' (setting saved to .DotSettings).
As mentioned by #stimpy77: for support in MVC Razor views you'll need an extra package (for those that don't read the comments)
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
If you want full C# 6.0 support, you'll need to install VS2015.
Information for obsoleted prerelease software:
According to this it's just a install and go for Visual Studio 2013:
In fact, installing the C# 6.0 compiler from this release involves little more than installing a Visual Studio 2013 extension, which in turn updates the MSBuild target files.
So just get the files from https://github.com/dotnet/roslyn and you are ready to go.
You do have to know it is an outdated version of the specs implemented there, since they no longer update the package for Visual Studio 2013:
You can also try April's End User Preview, which installs on top of Visual Studio 2013.
(note: this VS 2013 preview is quite out of date, and is no longer updated)
So if you do want to use the latest version, you have to download the Visual Studio 2015.
A lot of the answers here were written prior to Roslyn (the open-source .NET C# and VB compilers) moving to .NET 4.6. So they won't help you if your project targets, say, 4.5.2 as mine did (inherited and can't be changed).
But you can grab a previous version of Roslyn from https://www.nuget.org/packages/Microsoft.Net.Compilers and install that instead of the latest version. I used 1.3.2. (I tried 2.0.1 - which appears to be the last version that runs on .NET 4.5 - but I couldn't get it to compile*.) Run this from the Package Manager console in VS 2013:
PM> Install-Package Microsoft.Net.Compilers -Version 1.3.2
Then restart Visual Studio. I had a couple of problems initially; you need to set the C# version back to default (C#6.0 doesn't appear in the version list but seems to have been made the default), then clean, save, restart VS and recompile.
Interestingly, I didn't have any IntelliSense errors due to the C#6.0 features used in the code (which were the reason for wanting C#6.0 in the first place).
* version 2.0.1 threw error The "Microsoft.CodeAnalysis.BuildTasks.Csc task could not be loaded from the assembly Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
UPDATE One thing I've noticed since posting this answer is that if you change any code during debug ("Edit and Continue"), you'll like find that your C#6.0 code will suddenly show as errors in what seems to revert to a pre-C#6.0 environment. This requires a restart of your debug session. VERY annoying especially for web applications.
It worth mentioning that the build time will be increased for VS 2015 users after:
Install-Package Microsoft.Net.Compilers
Those who are using VS 2015 and have to keep this package in their projects can fix increased build time.
Edit file packages\Microsoft.Net.Compilers.1.2.2\build\Microsoft.Net.Compilers.props and clean it up. The file should look like:
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
Doing so forces a project to be built as it was before adding Microsoft.Net.Compilers package
It is possible to use full C# 6.0 features in Visual Studio 2013 if you have Resharper.
You have to enable Resharper Build and voilá!
In Resharper Options -> Build - enable Resharper Build and in "Use MSBuild.exe version" choose "Latest Installed"
This way Resharper is going to build your C# 6.0 Projects and will also not underline C# 6.0 code as invalid.
I am also using this although I have Visual Studio 2015 because:
Unit Tests in Resharper don't work for me with Visual Studio 2015 for some reason
VS 2015 uses a lot more memory than VS 2013.
I am putting this here, as I was looking for a solution for this problem for some time now and maybe it will help someone else.
It seems there's some misunderstanding. So, instead of trying to patch VS2013 here's and answer from a Microsoft guy: https://social.msdn.microsoft.com/Forums/vstudio/en-US/49ba9a67-d26a-4b21-80ef-caeb081b878e/will-c-60-ever-be-supported-by-vs-2013?forum=roslyn
So, please, read it and install VS2015.

Categories