Rulesets are not applies

Hi Nicolas, is there a way that the rulesets can be uploaded to the SonarCloud project. We’ve seen that our Rosyln analysers are being applied with all rules, not those specified in our ruleset.

Kind regards
Ben

Hi @benjones,

Please create a new thread when you encounter an issue. It makes conversations easier to follow and solutions are also easier to find.

Let me check if I understood you correctly, you created a ruleset, configured the build to use this ruleset, analyzed your project and you see rules from the roslyn analyzer appear as external issues in SonarCloud. Is that it?

You can’t upload rulesets to SonarCloud, and there is no need to do so. Roslyn should only execute rules which are in the ruleset, SonarCloud does not filter issues sent by Roslyn. Could you check if the ruleset is enabled during the build of your project?

Kind regards
Nicolas

Thanks Nicolas,

I have now added the ItemGroup Include reference to the ruleset in all csproj files but see within the VSTS build log that it fails to find the files:

/opt/hostedtoolcache/dotnet/sdk/3.0.102/Microsoft.CSharp.CurrentVersion.targets(135,9): warning MSB3884: Could not find rule set file “…/…/…/…/CodeAnalysisRuleSet.ruleset”

I suspect this is why Sonar Cloud is then unable to pick them up. The relative path is the same used within the PropertyGroup so I’m not sure where it is looking.

Do you have any suggestions on getting this to work?

Thanks

I wonder if a similar issue is causing my Pull Requests to register as having 0% code coverage. I can see the following warnings in the Run Code Analysis step:

WARNING: File ‘/home/vsts/work/1/s/CodeAnalysisRuleSet.ruleset’ does not exist.
WARNING: File ‘/home/vsts/.nuget/packages/microsoft.net.test.sdk/16.5.0/build/netcoreapp2.1/Microsoft.NET.Test.Sdk.Program.cs’ is not located under the root directory ‘/home/vsts/work/1/s/Source/DotNet’ and will not be analyzed.
WARNING: File ‘/home/vsts/.nuget/packages/xunit.runner.visualstudio/2.4.1/build/netcoreapp1.0/xunit.runner.visualstudio.dotnetcore.testadapter.dll’ is not located under the root directory ‘/home/vsts/work/1/s/Source/DotNet’ and will not be analyzed.
WARNING: File ‘/home/vsts/.nuget/packages/xunit.runner.visualstudio/2.4.1/build/netcoreapp1.0/xunit.runner.reporters.netcoreapp10.dll’ is not located under the root directory ‘/home/vsts/work/1/s/Source/DotNet’ and will not be analyzed.
WARNING: File ‘/home/vsts/.nuget/packages/xunit.runner.visualstudio/2.4.1/build/netcoreapp1.0/xunit.runner.utility.netcoreapp10.dll’ is not located under the root directory ‘/home/vsts/work/1/s/Source/DotNet’ and will not be analyzed.

Could you confirm if these warnings will result in the code coverage not being picked up?

Thanks
Ben

It seems that this issue is only present when using the Ubuntu build agents. Switching over to use the Windows-latest picked up the ruleset and pushed this through to Sonar. It also picked up some code coverage and pushed this through as well.

@benjones Interesting. The .NET Core version of the Scanner for MSBuild should work the same on Windows and non-Windows agents.

Does the casing of CodeAnalysisRuleSet.ruleset in the project file match the casing of the file name on disc?

Hi Duncan,

They do match. We have a single ruleset at the top level of our solution:
image

We have the ruleset referred to in each csproj within properties (and we’ve tested various relative paths):

image

And we’ve ensured that the ruleset is included within an item group
image

We noticed that the build under the ubuntu agent complained it could not find the ruleset file and promptly displayed a load of warnings in the log for rules we have not applied.

When we changes to ‘windows-latest’, no warnings about the ruleset, only the code warnings we have locally and the Sonar analysis not only replicated these warnings correctly but the code coverage analysis also got picked up.

Maybe we got unlucky with the ubuntu agent but this persisted over 2 days with approx 30 runs.

Hi Ben.

The ItemGroup isn’t required; MSBuild is only interested in the CodeAnalysisRuleSet property. The only effect of the ItemGroup is to make the file appear in the Solution Explorer in VS.

Does a build on the ubuntu agent correctly find the ruleset if you do a build that does is not running a Sonar analysis?

Hi Duncan,

The ItemGroup change we made after the original comment form Nicolas pointed us towards the Rosyln Rulset Help Page so we thought it wasn’t finding them because of that but good to know we can remove them.

To answer you question, yes, we did strip out the Sonar analysis from our yaml and saw that the dotnet build step still failed to find the ruleset file for all projects. From that point, I can understand that it would have had an impact on the Sonar analysis although why it would impact on the code coverage is not clear.

Hi @benjones,

The missing ruleset wouldn’t affect the code coverage. I suspect that is a separate issue. Are you getting code coverage on the Windows agent?

Hi @duncanp
We get both the external code analysis and the code coverage reporting come through fine using the windows-latest agent . If I get chance I will create a separate ci pipeline to test it all again with the ubuntu agent

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.