@dvm-2k1 reported this here: https://github.com/dotnet/core/issues/758#issuecomment-335075221
Please let me know Is this CA0055 and CA0052 issues got resolved for .Net Core 2.0 (netcoreapp2.0) projects?
Asking this because I'm still getting these errors when enabled our .Net Core 2.0 projects to use code analysis ruleset which are defined for our projects and trying to build from Visual Studio 2017 (updated to 15.3.5 version) and by using MSBuild command.
As per this conversion thread, I've installed Nuget package of Microsoft.CodeAnalysis.FxCopAnalyzers to our .net core 2.0 projects, but still getting CA0055 and CA0052 errors. Please help me out to resolve these issues which are blocking our builds.
Thanks @Petermarcu for posting this issue to right forums...
This issue can marked as resolved as these issues got fixed in our .Net Core 2.0 projects.
Cause of the errors:
_Note: This was written based on my investigation and understandings of MSBuild execution behavior of .Net Core projects to standard .Net Framework projects._
These issues were coming as "RunCodeAnalysis" flag was set to true which was coming from our Build props file, if csproj file does not have this element in them. If this "RunCodeAnalysis" flag is set to true this is executing FxCop command explicitly by MSBuild and FxCop is not getting any hint about targets details for .Net Core framework it is raising these CA0055 and CA0052 errors.
However, .Net Core implicitly executes code analysis rules with Code Analysis analyzers and this cannot be controlled by "RunCodeAnalysis" flag during build.
Fix for this errors:
Added "RunCodeAnalysis" element in PropertyGroup of csproj file to set to "false" so that this will hint MSBuild not to execute FxCop explicitly for .Net Core 2.0 or .Net Standard 2.0 projects.
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
......
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
I rely on CA2000 and this rule is mandatory on all of my builds. I cannot upgrade to Microsoft.CodeAnalysis.FxCopAnalyzers due to an exiting bug preventing this rule from being implemented. Because of this, I cannot run Code Analysis on any .NET Core application, or .NET Standard libraries 1.5 or greater.
If someone has a solution that allows me to run an equivalent (modern version) of the "Microsoft All Rules" rule set (including CA2000) on a .NET Standard 1.5 or greater library and on .NET Core applications, please let me know.
Closing this issue as per https://github.com/dotnet/roslyn-analyzers/issues/1313#issuecomment-335415808
@daviddunson Please open another issue if you are still having trouble
I don't think this should be closed. The issue is that Code Analysis is failing. Disabling code analysis as per the noted comment appears to be a workaround, not a solution.
I am facing the same exact issue. When I configure the ruleset to my project as the default provided by Sonarqube, named 'The Sonar way', the project builds perfectly. However when I set the project ruleset to be a custom one, I get CA0055 and CA0052 and the build fails midway through the MSBuild 15.0 task.
I tried the workaround suggested by @dvm-2k1 but had no luck there.
The workaround that I am currently using until the FxCopAnalyzers is completed is to set
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)' == 'Debug|net40|AnyCPU'">
<DebugType>full</DebugType>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)' == 'Release|net40|AnyCPU'">
<DebugType>pdbonly</DebugType>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
The disadvantage to this is that it will not catch any code that is not .NET Framework compliant (i.e. #if !NET40).
Do not enable Code Analysis on multiple target frameworks, or you will get multiple warnings in the error list. Do not enable Code Analysis on a .NET Standard framework greater than 1.4.
is there another issue opened to fix the root cause (e.g. code analysis is enabled?) I agree with other posters that disabling code analysis is not a solution, it's a (hacky) workaround...
@samsmithnz, I'm not sure what you're asking for here. RunCodeAnalysis is part of the old FXCop framework, and we won't be fixing the CA0055/CA0052 errors in that analysis framework.
Got it, I didn't understand that sorry.
It seems then that it's not possible to have Code Analysis enabled on the build in a solution with a mix of .Net Framework and .Net core assemblies?
Okay, so the root problem is really:
@BioTurboNick That is the exact story that @333fred is currently investigating. Fred, do you have a tracking work item for the same?
Until the migration story is complete, please follow the below steps:
RunCodeAnalysis MSBuild property to true in CI builds. Analyzers always get executed as part of command line builds during compilation and do not need any additional configuration or explicit invocation.I don't have tracking item for this at the moment, I'm planning on opening one after the holidays are over and we have a more complete plan of what experience we want is.
This is still an issue in Visual Stdio 2017 - 15.5.7 With .NET Standard 2.0
This is frustrating like you wouldn't believe. I try to make sure our team follows the highest coding standards and yet we can't even get basic code rules functionality to work.
None of our .NET Standard 2.0 projects compile properly with code rules turned on. Even when they do compile, the broken code rules are never picked up.
It's about time Microsoft spent some time on the code analyzer. It's ridiculous that release after release of Visual Studio has meant that we can't even implement basic code rules in our projects without using workarounds.
@mavasani , are you saying that we have to build our projects at the command line to get code rules to work?
When is this going to be fixed?
Year after year, the code analyzer has been neglected. There are bugs all over the place even when not using .NET Standard. Are Microsoft actually working to fix the issues?
@MelbourneDeveloper I am sorry to hear about your issues. Please let me know if following helps:
"Run Code Analysis" command is for executing legacy post-build analysis (FxCopCmd.exe), which is not supported for .netcore/.netstandard projects
Does this imply that roslyn analyzers do not work on .Net standard? Surely code quality is still important with .net standard?
@danrien I believe you misread my comment. Roslyn analyzers do work fine on .net standard and .net core platforms. FxCopCmd.exe, which is a legacy binary analysis tool does not support these platforms.
Ah ok, thanks, I clearly misintrepreted!
By the way, for anyone reading this wondering how to use StyleCop, there are StyleCopAnalyzers, which I think has high compatibility with the original StyleCop...
I came across the same problem when having a solution mixed with .NET Full (non-SDK style projects) and .NET Standard projects (SDK style projects).
I fixed it by adding following _custom_ target at the end of the project (I actually created a shared targets file which I imports in each SDK style project):
<Target Name="IgnoreRunCodeAnalysis" Condition=" '$(RunCodeAnalysis)' == 'true' " BeforeTargets="RunCodeAnalysis">
<Message Importance="normal" Text="Set RunCodeAnalysisOnThisProject to false" />
<PropertyGroup>
<RunCodeAnalysisOnThisProject>false</RunCodeAnalysisOnThisProject>
</PropertyGroup>
</Target>
If you want code analysis, you can include the (nuget) package Microsoft.CodeAnalysis.FxCopAnalyzers. I did it like below (in my project files), so I can still leverage the global property RunCodeAnalysis:
<ItemGroup Condition=" '$(RunCodeAnalysis)' == 'true' ">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.1" />
</ItemGroup>
I don't know why, but if I add <GenerateDocumentationFile>true</GenerateDocumentationFile> msbuild runs code analysis and the build fails with IDE0005. It seems a solution for me
P.S.
I configured IDE0005 as an error in editorconfig, installed Microsoft.CodeAnalysis.CSharp.CodeStyle and I want to get errors from msbuild
Most helpful comment
Okay, so the root problem is really: