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
787 stars 227 forks source link

New Rule Idea: Mark pre-release nuget package as non compliant #8351

Open martin-strecker-sonarsource opened 11 months ago

martin-strecker-sonarsource commented 11 months ago

Community request

Reasoning

Adding pre-release package dependency to a project can cause maintainability and security issues. There is a high risk of changing or dropping APIs before a new stable version is released.

Description

Pre-release is a Nuget and not an assembly concept. There are two options for implementing this (further investigation is needed).

Use the AssemblyInformationalVersionAttribute

Some packages like Fody or EF core apply the Nuget version also to the assemblies via the AssemblyInformationalVersionAttribute. We could use Compilation.References to find the AssemblyInformationalVersionAttribute via the metadata reader of the module of the PortableExecutableReference of external DLLs.

Pros:

Cons:

Instrument msbuild in the scanner to add the csproj as an additional file

This would allow us to access the csproj (and reporting there should also just work).

Pros:

Cons:

martin-strecker-sonarsource commented 11 months ago

A PoC was implemented here

https://github.com/martin-strecker-sonarsource/ExampleAnalyzer/blob/master/Analyzer/Analyzer/CsprojAnalyzer.cs

Important: See how Analyzer.targets adds the csproj to AdditionalFiles and how the nuget spec adds the target file to the build directory of the nuget

https://github.com/martin-strecker-sonarsource/ExampleAnalyzer/tree/master/Analyzer/Analyzer.Package

Still open to discuss:

Corniel commented 2 months ago

@martin-strecker-sonarsource Ideally you want to rely on additional files. However, is some cases, files are not available as such. You then still want to be able to do the analysis. This can be done by (also rergestering on the compilation action):

We did it like this: https://github.com/dotnet-project-file-analyzers/dotnet-project-file-analyzers/blob/main/src/DotNetProjectFile.Analyzers/Extensions/Microsoft.CodeAnalysis.Diagnostics.AnalysisContext.cs

martin-strecker-sonarsource commented 2 months ago

Thank you, @Corniel. I looked at the project, and there are some interesting ideas about including some common files for the analysis.

Corniel commented 2 months ago

@martin-strecker-sonarsource I know. I started a year ago with this project, and it has been successfully used within my company (and some others) already. I'm working on some rules for the .editorconfig too, and consider adding rules for appsettings, but there nothing concrete yet has been implemented.

It would be nice if Sonar would support its output too, as mentioned: https://community.sonarsource.com/t/suport-roslyn-analyzers-on-additional-files/123957.