Roslyn allows to write analyzers on additional files (Technically, also on other none compiling files, but that is actively discouraged). However, Sonar does not support this, due to the fact that it filters on file extensions (at least, I think that is the reason). It would be nice if this could be supported.
For those who are interested, I’ve build a dozen rules on project files (and some on resource/resx files), that run perfectly with the compiler: .NET project file analyzers
We use SonarCloud, but I guess that would not make the big difference here. That being said I’d like to see full support, this feels like a hack. And Sonar obviously already runs the analysis, as they are part of the build (output). It just ignores them.
Assuming, you use Azure DevOps. Did you try to add the additional files to the analysis scope in the extra properties?
- task: SonarQubePrepare@6
displayName: 'Prepare analysis on SonarQube'
condition: ne(variables['Build.Reason'], 'Schedule')
inputs:
SonarQube: Sonarqube
projectKey: XXXX
projectVersion: '$(Build.BuildNumber)'
extraProperties: |
# Additional properties that will be passed to the scanner,
# Put one key=value per line, example:
sonar.sources = src/, **/*.myExtension
# sonar.tests = test/
I’ve added sonar.sources as per your suggestion, just to test it out.
Unfortunately, even though the project I’m building against has issues in it’s csproj:
13>C:\project\project.csproj(1,1,10,11): warning Proj0201: Define the <Version> node explicitly or define the <IsPackable> node with value 'false'. (https://dotnet-project-file-analyzers.github.io/rules/Proj0201.html)
13>C:\project\project.csproj(1,1,10,11): warning Proj0202: Define the <Description> or <PackageDescription> node explicitly or define the <IsPackable> node with value 'false'. (https://dotnet-project-file-analyzers.github.io/rules/Proj0202.html)
SonarCloud shows no issues, and my pull request doesn’t have these warnings as comments either.
@ahofland Can you conferm, that in your case the project file was already added as additional file? Because in that case, I would assume that also including the build file as <Content /> should can become obsolete after some small adjustments by Sonar?
Thank you, @ahofland, for doing this investigation and for confirming that issues in the csproj can be propagated this way.
We do have some special handling for <Content Include="" /> but we lack the special handling for <AdditionalFiles />. We will look into this as it would help with some exciting analyzer enhancements, as suggested by @Corniel.
This may have undesired consequences for some users because this mechanism is most often used by source generators to generate code based on an external file. This file may or may not be desirable for upload to the Sonar server. We need to discuss internally whether we want to enable this mechanism for all users.
Note that inlcuding Directory.Build.props and Directory.Packages.props both as <AdditionalFIles /> or <Content /> can be inconvenient. Specially when not all *.??proj files have the same nesting compared to these files, as in those cases, it can not be defined in these special .props files themselves.
I hope you can come up with a solution that does not require extra configuration in the project files themselves. (Adding them as additional files is recomanded by Microsoft anyway, so that should be fine).
I hope you can reconsider the support of <AdditionalFiles>, as it is way more convenient then reporting on <Content>as it turns out.
The MSBuild team introduced <AdditionalFiles> for the purpose of providing analyzers with extra files to report on, were <Content> commonly has other meaning, like in the linked issue when building a Razor application.
A co-contributor (with some help from Claude) stumbled on the custom MSBuild item: <QAdditionalAnalysisFileItemTypes>, which suggests to solve the original issue raised by this topic.
and you can see that AdditionalFiles fits in here conceptually (AdditionalFiles is just another item group with files like in any of the other such bags listed here):
So in theory, this should upload such files to SonarQube and may make diagnsotics available on it, but this is not really a supported use case by us. This setting has no official documentation because we never went further than what is written in the comment: We forward these to scanner plugins - not more not less. If this enough to fully support AdditionalFiles is untested/unsupported.
Thank you for your understanding @Corniel and please let us know what the outcome of your experiment is - I’m interested in moving this topic forward. The feature of rules for other file types is in our backlog as well.
From a technical point of view, I would argue that you want to define your own MSBuild item, instead of property. This makes adding items both easier and flexible:
This gives way more flexibility: for instance, you could decide to only add a subset of specific items, or later use @Remove to remove all *.css files.
Currently, we have to know all the types you want to add, as you will not set the Property with your preferences once we’ve touched it.
Our tests suggests that adding AdditionalFiles to $(SQAnalysisFileTypes) does work, so that is something.
We’ve just shipped a new version of the NuGet Gallery | DotNetProjectFile.Analyzers 1.19.0 that not longer uses <Content> to integrate with SonarQube. I still think that what I suggested would be a nice improvement on the current implementation. Please, if you change it let us know, as there are several customers of you out there that use these analyzers.