dotnet / roslyn-analyzers

MIT License
1.58k stars 463 forks source link

Enable EnableNETAnalyzers for .Net Framework not working #4165

Closed sassy224 closed 4 years ago

sassy224 commented 4 years ago

Analyzer

N/A

Analyzer source

N/A

Describe the bug

Trying to enable EnableNETAnalyzers for .Net Framework projects but they are not working/having effects.

Steps To Reproduce for .Net Framework projects

  1. Create .Net Framework with below sample code

            var arr = new List<int>();
            arr.AddRange(new int[] { 1, 2, 3, 4 });
    
            if (arr.Count() > 0)
            {
                Console.WriteLine("Have items");
            }    
            else
            {
                Console.WriteLine("Empty");
            }
  2. Edit csproj file, add 2 properties
    <PropertyGroup>
        <EnableNETAnalyzers>true</EnableNETAnalyzers>
        <AnalysisMode>Default</AnalysisMode>
    </PropertyGroup>
  3. Build project

Expected behavior

Warning CA1829 should be shown.

Actual behavior

No warning is shown

Additional context

  1. If install Microsoft.CodeAnalysis.FxCopAnalyzers and rebuild project (either remove or keep and , warning CA1829 will be shown.

=================================== Add any other context about the problem here. I know that install package Microsoft.CodeAnalysis.FxCopAnalyzers will enable analyzers for all projects (.Net Framework, .Net Standard, .Net Core, .Net 5) however, it will create additional package dependency so I want to use .editorconfig + EnableNETAnalyzers to enable new Roslyn analyzers. As mentioned here: https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisnetanalyzers

For projects targeting earlier .NET frameworks, you can enable them in your MSBuild project file by setting one of the following properties:

EnableNETAnalyzers
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

But seems they aren't working.

mavasani commented 4 years ago

@sassy224 : CA1829 is an IDE suggestion by default, not a warning. See default severities and enabled by default values for all CA rules shipping in .NET5 release over here: https://github.com/dotnet/roslyn-analyzers/blob/master/src/NetAnalyzers/Core/AnalyzerReleases.Shipped.md. You may want to refer to https://docs.microsoft.com/dotnet/fundamentals/productivity/code-analysis#enable-additional-rules to enable additional CA rules as build warnings.

sassy224 commented 4 years ago

@mavasani my point is I was expecting that, for .Net Framework projects, I only need to set EnableNETAnalyzers = true to be able to use the Roslyn analyzers with its default rules which is good enough for me. Even if I add .editorconfig file, and modify severity, it will have no effect at all. With .Net 5 project, you can edit .editorconfig file, modify severities and it will work out of the box. According to MS's document, in .Net Framework, we only need to set EnableNETAnalyzers=true to have that effect. This issue is to report that it's not working yet.

mavasani commented 4 years ago

@sassy224 just to confirm- you are on the .NET5 Preview8 or later SDK correct? That is the first SDK version with built in analyzers. You can target .NetFramework and get all the analyzers, but you need to be on the .Net5 SDK

mavasani commented 4 years ago

Also can you check if you open your project in VS2019 16.8 Preview3, do you see an IDE suggestion diagnostic for CA1829 with a code fix?

sassy224 commented 4 years ago

just to confirm- you are on the .NET5 Preview8 or later SDK correct? That is the first SDK version with built in analyzers. You can target .NetFramework and get all the analyzers, but you need to be on the .Net5 SDK

I see, I was using the old .Net Framework project, not the new SDK-style one.

To confirm, will there be any differences if I build my library using below approaches?

Currently my projects are using old .Net Framework csproj, I'm very hesitant about converting them to new SDK csproj.

mavasani commented 4 years ago

will there be any differences if I build my library using below approaches?

I would like to say no, but I am probably not the best or most authoritative person to state that. Likely post a question on dotnet/project-system repo?

Currently my projects are using old .Net Framework csproj, I'm very hesitant about converting them to new SDK csproj.

One other alternative for you is to directly reference the NuGet package linked at https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisnetanalyzers. Then you don't need to change to SDK style project file. We do plan to release a 5.0.0 stable NuGet package corresponding to the .NET5 SDK release, so you can switch to that whenever it is published.

sassy224 commented 4 years ago

I would like to say no, but I am probably not the best or most authoritative person to state that. Likely post a question on dotnet/project-system repo

Thanks, I posted issue there

One other alternative for you is to directly reference the NuGet package linked at https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisnetanalyzers. Then you don't need to change to SDK style project file. We do plan to release a 5.0.0 stable NuGet package corresponding to the .NET5 SDK release, so you can switch to that whenever it is published

Thanks for the suggestion, I will consider it as last resort after I get confirmation about the assembly differences.

sassy224 commented 4 years ago

Got the answer I need so close the issue for now.