dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

CA2254 Is incompatible/broken with Microsoft.Azure.WebJobs #5987

Open philipborg opened 2 years ago

philipborg commented 2 years ago

Analyzer

Diagnostic ID: CA2254: Template should be a static expression

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 6.0.202

OR

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 6.0.0 (Latest) and 7.0.0-preview1.22168.1 (Preview)

Describe the bug

CA2254 seemingly never gives any warnings even if all other rules seem to work if Microsoft.Azure.WebJobs version 3.0.32 (latest) is installed. I have tried both the built-in CA analyzer, NuGet 6.0.0 and 7.0.0-preview1.22168.1, never works. I also tried lowering Microsoft.Azure.WebJobs to 3.0.0 without success.

Tried properties:

<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest-all</AnalysisLevel>

I have also tried to set AnalysisLevel to 6-all and preview-all, with and without CA nuget package. Same issue persists.

Steps To Reproduce

  1. Enable CA2254 analyzer.
  2. Add the following code.
    
    using Microsoft.Extensions.Logging;

namespace CA2254POC;

public class Sample { public void LogExample(ILogger logger) { logger.LogInformation($"{logger.GetType().Assembly.Location}"); } }

Sample csproj
```xml
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <LangVersion>latest</LangVersion>
        <Nullable>enable</Nullable>
        <EnableNETAnalyzers>true</EnableNETAnalyzers>
        <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
        <AnalysisLevel>latest-all</AnalysisLevel>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
    </ItemGroup>
</Project>
  1. It will flag this sample code correctly.
  2. Add dependency to Microsoft.Azure.WebJobs
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.32" />
    </ItemGroup>
  3. CA2254 analyzer now always returns negative responses.

Expected behavior

CA2254 should trigger even with Microsoft.Azure.WebJobs installed.

Actual behavior

CA2254 doesn't trigger with Microsoft.Azure.WebJobs installed.

Additional context

I have tried just having each of the dependencies of Microsoft.Azure.WebJobs installed and the problem did not occur. I am thus pretty certain it's a problem with Microsoft.Azure.WebJobs specifically rather than any one of its dependencies. I initially discovered this issue when Microsoft.NET.Sdk.Functions 4.1.0 or 4.0.0 (the version I tried) was installed and boiled it down to the Microsoft.Azure.WebJobs dependency.

Bellarmine-Head commented 2 years ago

What this means (just one example, and just to spell it out) is that this warning will never trigger for Azure Function projects.

Bellarmine-Head commented 2 years ago

It would seem that related rule https://docs.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2017 is also broken in Azure Function projects.

remoba commented 1 year ago

Still broken in the latest Microsoft.Azure.WebJobs version (3.0.37), any insights to share? At least an acknowledgement that this is a bug?

dmeytin commented 1 year ago

+1 on fixing this bug