SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
797 stars 229 forks source link

Sonar build with [LoggerMessage] not working #5466

Closed glucaci closed 2 years ago

glucaci commented 2 years ago

Hi,

Already opened a community topic here but no answer.

Description

When using [LoggerMessage], sonar doesn’t find the generated code. CS8795: Partial method '...' must have an implementation part because it has accessibility modifiers.

Repro steps

Known workarounds

Patching the SonarQubeAnalysisConfig.xml and add a new AnalyzerPlugin entry with Path to the Microsoft.Extensions.Logging.Generators.dll.

<AnalyzerPlugin Key="custom">
  <AssemblyPaths>
    <Path>/path/to/nugetpackage/Microsoft.Extensions.Logging.Generators.dll</Path>
  </AssemblyPaths>
</AnalyzerPlugin>

Related information

costin-zaharia-sonarsource commented 2 years ago

Hi @glucaci, sorry for taking so long to take a look at this (I refer here to the community thread).

I've tried to reproduce the problem with an empty Asp.Net Core project targeting .net 6 without any luck. What is the version of the analyzer that you're using?

You can find the project I've used here: Repro5466.zip

Could you please upload your reproducer if you have it already? It will save us some time.

glucaci commented 2 years ago

Hi,

I can reproduce it with your code. src/Repro5466/LoggingExtensions.cs(6,36): Error CS8795: Partial method 'LoggingExtensions.TestLog(ILogger, bool)' must have an implementation part because it has accessibility modifiers.

Additional information:

Let me know if you need more information.

Thanks!

costin-zaharia-sonarsource commented 2 years ago

Seems to be related to the environment. I've tested on my machine with SonarCloud, the same version of the scanner and dotnet SDK and it's not reproducible. Key differences: windows instead of linux and local environment instead of Azure pipeline.

Would it be possible to share the full build log in verbose mode?

glucaci commented 2 years ago

sonar-build-error.txt

This is the output from dotnet build --verbosity detailed. Let me know if you need more verbose.

costin-zaharia-sonarsource commented 2 years ago

Seems to be similar to https://github.com/dotnet/docs/issues/27471

glucaci commented 2 years ago

I'm not sure if is the same problem locally and on the build environment is everything ok without sonar. Could you upload the build output from your environment?

On my build environment if I don't call dotnet sonarscanner begin before dotnet build everything is building without error. What I found out is that Sonar is replacing all the dotnet analyzers with the sonar analyzers and this is kicking out the Microsoft.Extensions.Logging.Generators.dll.

If you take a look on the csc command you will see that only sonar analyzers are listed. On a normal build without dotnet sonarscanner begin the csc is including /analyzer:Microsoft.Extensions.Logging.Generators.dll

This following is the build output from the same environment without sonar: build-success.txt

Is this not the same thing as with the Razor? I saw that the Razor analyzer is included in the securitycsharpfrontend plugin which is listed in the SonarQubeAnalysisConfig.xml

<AnalyzerPlugin Key="securitycsharpfrontend" Version="9.4.0-M1.15235" StaticResourceName="SonarAnalyzer.Security-9.4.0-M1.15235.zip">
  <AssemblyPaths>
    <Path>/tmp/.sonarqube/resources/2/SonarAnalyzer.Security.dll</Path>
    <Path>/tmp/.sonarqube/resources/2/Google.Protobuf.License.txt</Path>
    <Path>/tmp/.sonarqube/resources/2/Microsoft.AspNetCore.Razor.Language.dll</Path>
  </AssemblyPaths>
</AnalyzerPlugin>

That's why I think with the workaround which I posted above everything is working.

costin-zaharia-sonarsource commented 2 years ago

Very good point, this could happen because of this: https://github.com/SonarSource/sonar-scanner-msbuild/blob/master/src/SonarScanner.MSBuild.Tasks/Targets/SonarQube.Integration.targets#L601-L603

glucaci commented 2 years ago

Yes, I think it should not remove any analyzers or additional files because a lot of source generators is relying on them.

costin-zaharia-sonarsource commented 2 years ago

Normally, we should not delete them. There should be a concatenation (in SQAdditionalFiles) but there seems to be a bug. Would it be possible to share with us the binary logs? These can be generated with /bl:name.binlog parameter on the build command.

glucaci commented 2 years ago

Sure, I assume from the failed build logs.zip

costin-zaharia-sonarsource commented 2 years ago

I assume from the failed build

Yes, indeed. I'm sorry, I forgot to mention.

Thanks for the binary logs. I can now confirm that the GetAnalyzerSettings task is removing all the existing analyzers. This is bug since it's not the indented behavior.

I've added an issue to the scanner project: https://github.com/SonarSource/sonar-scanner-msbuild/issues/1211

costin-zaharia-sonarsource commented 2 years ago

@glucaci could you please try to set sonar.cs.roslyn.ignoreIssues to false as a workaround? This will have the side effect to import all the issues to SQ, even the 3rd party ones, but it should fix the build until we manage to address the root problem.

glucaci commented 2 years ago

Thanks for the findings. I can confirm that with /d:sonar.cs.roslyn.ignoreIssues=false the build is working as expected.

The list of analyzers

/analyzer:/tmp/.sonarqube/resources/0/SonarAnalyzer.CSharp.dll
/analyzer:/tmp/.sonarqube/resources/0/SonarAnalyzer.CFG.dll
/analyzer:/tmp/.sonarqube/resources/0/Google.Protobuf.dll
/analyzer:/tmp/.sonarqube/resources/0/SonarAnalyzer.dll
/analyzer:/tmp/.sonarqube/resources/1/SonarAnalyzer.CFG.dll
/analyzer:/tmp/.sonarqube/resources/1/SonarAnalyzer.VisualBasic.dll
/analyzer:/tmp/.sonarqube/resources/1/Google.Protobuf.dll
/analyzer:/tmp/.sonarqube/resources/1/SonarAnalyzer.dll
/analyzer:/tmp/.sonarqube/resources/2/Microsoft.AspNetCore.Razor.Language.dll
/analyzer:/tmp/.sonarqube/resources/2/SonarAnalyzer.Security.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk.Web/analyzers/cs/Microsoft.AspNetCore.Analyzers.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk.Web/analyzers/cs/Microsoft.AspNetCore.Mvc.Analyzers.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk.Web/analyzers/cs/Microsoft.AspNetCore.Components.Analyzers.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll
/analyzer:/usr/share/dotnet/packs/Microsoft.NETCore.App.Ref/6.0.2/analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll
/analyzer:/usr/share/dotnet/packs/Microsoft.AspNetCore.App.Ref/6.0.2/analyzers/dotnet/cs/Microsoft.AspNetCore.App.Analyzers.dll
/analyzer:/usr/share/dotnet/packs/Microsoft.AspNetCore.App.Ref/6.0.2/analyzers/dotnet/cs/Microsoft.AspNetCore.App.CodeFixes.dll
/analyzer:/usr/share/dotnet/packs/Microsoft.AspNetCore.App.Ref/6.0.2/analyzers/dotnet/roslyn4.0/cs/Microsoft.Extensions.Logging.Generators.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk.Razor/targets/../source-generators/Microsoft.AspNetCore.Razor.SourceGenerator.Tooling.Internal.dll
/analyzer:/usr/share/dotnet/sdk/6.0.200/Sdks/Microsoft.NET.Sdk.Razor/targets/../source-generators/Microsoft.NET.Sdk.Razor.SourceGenerators.dll
costin-zaharia-sonarsource commented 2 years ago

I'm closing this issue since the root cause has been identified in the scanner. This issue is tracked now on https://github.com/SonarSource/sonar-scanner-msbuild/issues/1211