Sonar cloud - Ignoring rules with editorconfig is not working

Hi community,

Recently I’ve added to my project .editorconfig file in order to ignore some of SonarCloud issues. I followed this advice

Excluding files in certain directories for SonarScanner for .NET - SonarCloud - Sonar Community (sonarsource.com)

Here are my Azure DevOps tasks:

steps:
  - task: SonarCloudPrepare@2
    displayName: "Preparing SonarCloud Analysis"
    inputs:
      SonarCloud: "SonarCloud ---redacted---"
      organization: " ---redacted---"
      scannerMode: "MSBuild"
      projectKey: "${{ parameters.sonarCloudProjectKey }}"
      projectName: "${{ parameters.sonarCloudProjectName }}"
      extraProperties: |
        sonar.exclusions=**/obj/**, **/*.dll, **/Migrations/*.cs, ${{ parameters.sonarExclusions }}
        sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.coverage.exclusions=${{ parameters.sonarCoverageExclusions }}

...some other tasks

  - task: DotNetCoreCLI@2
    displayName: "Build solution"
    inputs:
      command: "build"
      arguments: "--no-restore"
      configuration: $(buildConfiguration)

...some other tasks

  - task: SonarCloudAnalyze@2
    continueOnError: true
    displayName: "Run SonarCloud analysis"

  - task: SonarCloudPublish@2
    continueOnError: true
    displayName: "Publish SonarCloud analysis results on build summary"

And here is my .editorconfig file

[*.cs]
dotnet_diagnostic.S6966.severity = none
dotnet_diagnostic.S1144.severity = none

And here are some logs from the Azure DevOps pipeline that I can share. They’re coming from MS build step.

Starting: Build solution
==============================================================================
Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.242.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
/home/devopsagentuser/vsts-agent-src-04/_work/_tool/dotnet/dotnet build -dl:CentralLogger,"/home/devopsagentuser/vsts-agent-src-04/_work/_tasks/DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b/2.242.0/dotnet-build-helpers/Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"*ForwardingLogger,"/home/devopsagentuser/vsts-agent-src-04/_work/_tasks/DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b/2.242.0/dotnet-build-helpers/Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll" --no-restore
  DOCOsoft.ExampleMicroservice.Infrastructure -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Infrastructure/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.Infrastructure.dll
  Sonar: (DOCOsoft.ExampleMicroservice.Infrastructure.csproj) Project processed successfully
  DOCOsoft.Tests.Shared.Conventions -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/src/DOCOsoft.Tests.Shared.Conventions/bin/Debug/net8.0/DOCOsoft.Tests.Shared.Conventions.dll
  Sonar: (DOCOsoft.Tests.Shared.Conventions.csproj) Project processed successfully
  DOCOsoft.ExampleMicroservice.BusinessLogic -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.BusinessLogic/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.BusinessLogic.dll
  Sonar: (DOCOsoft.ExampleMicroservice.BusinessLogic.csproj) Project processed successfully
  DOCOsoft.ExampleMicroservice.WebApp -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.WebApp/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.WebApp.dll
  Sonar: (DOCOsoft.ExampleMicroservice.WebApp.csproj) Project processed successfully
##[warning]tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/BankDetailsEntity.cs(5,26): Warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/BankDetailsEntity.cs(5,26): warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144) [/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/DOCOsoft.ExampleMicroservice.Domain.csproj]
##[warning]tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/AddressEntity.cs(10,26): Warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/AddressEntity.cs(10,26): warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144) [/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/DOCOsoft.ExampleMicroservice.Domain.csproj]
##[warning]tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/UserEntity.cs(10,26): Warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/UserEntity.cs(10,26): warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144) [/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/DOCOsoft.ExampleMicroservice.Domain.csproj]
  DOCOsoft.ExampleMicroservice.Domain -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.Domain.dll
  Sonar: (DOCOsoft.ExampleMicroservice.Domain.csproj) Project processed successfully
  DOCOsoft.Tests.Shared.Fixtures -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/src/DOCOsoft.Tests.Shared.Fixtures/bin/Debug/net8.0/DOCOsoft.Tests.Shared.Fixtures.dll
  Sonar: (DOCOsoft.Tests.Shared.Fixtures.csproj) Project processed successfully
##[warning]tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.DataAccess/DataContext.cs(12,43): Warning S1144: Remove the unused private setter 'set_Users'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.DataAccess/DataContext.cs(12,43): warning S1144: Remove the unused private setter 'set_Users'. (https://rules.sonarsource.com/csharp/RSPEC-1144) [/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.DataAccess/DOCOsoft.ExampleMicroservice.DataAccess.csproj]
  DOCOsoft.ExampleMicroservice.DataAccess -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.DataAccess/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.DataAccess.dll
  Sonar: (DOCOsoft.ExampleMicroservice.DataAccess.csproj) Project processed successfully
  DOCOsoft.Tests.Shared.Fixtures.IntegrationTests -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/DOCOsoft.Tests.Shared.Fixtures.IntegrationTests/bin/Debug/net8.0/DOCOsoft.Tests.Shared.Fixtures.IntegrationTests.dll
  Sonar: (DOCOsoft.Tests.Shared.Fixtures.IntegrationTests.csproj) Project processed successfully
  DOCOsoft.ExampleMicroservice.DataAccess.UnitTests -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/tests/DOCOsoft.ExampleMicroservice.DataAccess.UnitTests/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.DataAccess.UnitTests.dll
  Sonar: (DOCOsoft.ExampleMicroservice.DataAccess.UnitTests.csproj) Project processed successfully
  DOCOsoft.ExampleMicroservice.BusinessLogic.UnitTests -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/tests/DOCOsoft.ExampleMicroservice.BusinessLogic.UnitTests/bin/Debug/net8.0/DOCOsoft.ExampleMicroservice.BusinessLogic.UnitTests.dll
  Sonar: (DOCOsoft.ExampleMicroservice.BusinessLogic.UnitTests.csproj) Project processed successfully
  DOCOsoft.Tests.Shared.UnitTests -> /home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/DOCOsoft.Tests.Shared.UnitTests/bin/Debug/net8.0/DOCOsoft.Tests.Shared.UnitTests.dll
  Sonar: (DOCOsoft.Tests.Shared.UnitTests.csproj) Project processed successfully

Build succeeded.

/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/Entities/BankDetailsEntity.cs(5,26): warning S1144: Remove the unused private setter 'set_Id'. (https://rules.sonarsource.com/csharp/RSPEC-1144) [/home/devopsagentuser/vsts-agent-src-04/_work/319/s/tests/ExampleMicroservice/src/DOCOsoft.ExampleMicroservice.Domain/DOCOsoft.ExampleMicroservice.Domain.csproj]

Could you help me find out why rule no S6966 is ignored properly and S1144 is not?

Best regards,
Senior Software Engineer @ DOCOsoft
Augustyn Binkowski

1 Like

Hi there,

Partially I found out what was the problem. I hope it helps someone else in the future. The problem was with the dotnet build command I was missing the reference other .editorconfig file in the .sln file. The only thing is that the pipelines are working on the PR builds but not on the ones triggered from the develop branch. Can anyone help?

Best regards,
Senior Software Engineer @ DOCOsoft
Augustyn Binkowski

Hello @ABinkowskiATDOCOsoft,

Do you have the same behavior if you run it locally?

Where is your .editorconfig file located relatively from your files that raises S1144?
Based on where your file is located, it might not apply to some files.
For instance, if your .editorconfig is not in a parent folder, it will not be applied to the file.

I see only four occurrences of S1144 in your logs. Did you check if there are any other places in your code that would raise the issue?

This should have no impact; the dotnet build automatically picks up the file(s).
If I am missing something, let me know.

Hi @sebastien.marichal

Sorry for the late response.

I have not run it locally.

Here is the structure of my editor config files

.
β”œβ”€β”€ .editorconfig
β”œβ”€β”€ /src
└── /tests/
    └── /ExampleMicroservice/
        β”œβ”€β”€ /src/
        β”‚   └── /DOCOsoft.ExampleMicroservice.Domain/
        β”‚       β”œβ”€β”€ .editorconfig
        β”‚       └── /Entities/
        β”‚           β”œβ”€β”€ AddressEntity.cs
        β”‚           β”œβ”€β”€ BankDetailsEntity.cs
        β”‚           └── UserEntity.cs
        └── /tests

And the contents:
Domain editor config file

[Entities/**.cs]
dotnet_diagnostic.S1144.severity = none

Root editor config file

# top-most EditorConfig file for this level
root = true

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers = 

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers = 

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers = 

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix = 
dotnet_naming_style.begins_with_i.word_separator = 
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix = 
dotnet_naming_style.pascal_case.required_suffix = 
dotnet_naming_style.pascal_case.word_separator = 
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix = 
dotnet_naming_style.pascal_case.required_suffix = 
dotnet_naming_style.pascal_case.word_separator = 
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
dotnet_style_namespace_match_folder = true:suggestion

[*.cs]
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = file_scoped:warning
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_indent_labels = one_less_than_current
csharp_space_around_binary_operators = before_and_after

Hope it helps you understand the structure a little bit.

Best regards,
Senior Software Engineer @ DOCOsoft
Augustyn Binkowski

Hello @ABinkowskiATDOCOsoft,

I have been doing more investigation on this issue.
It seems that in the particular case of S1144 (and S4487), it does not work. The severity is ignored for some reason.

This seems to be only the case when invoking our analyzer with SonarScanner for .NET.

I have created a ticket in our backlog to fix it:

https://sonarsource.atlassian.net/browse/SCAN4NET-16

Have a great day!