SonarQube ignores settings file

In my .Net code, I have several generated classes that I want to exclude from the SonarQube analysis that runs on every pull request. In order to figure out how the exclusion works, I’ve tried two approaches to exclude the analysis of a single file adres.cs.

Option A

I added a file sonar-project.properties in the root of the git repository, its contents is:

sonar.projectKey=MyProjectKey
sonar.projectName=MyProjectName
sonar.exclusions=**/adres.cs

The YAML snippet below shows how the SonarQube prepare step is being invoked (except that in this scenario, sonar.exclusions is commented out in the YAML file).
In the output of the build server I see the following interesting statements:

  1. Generating SonarQube project properties file to J:DevOpsAgent01_work13.sonarqubeoutsonar-project.properties
  2. DEBUG: Blame file (native) src/some_folder/adres.cs
  3. Indexing files of module ‘MyModule’. Source paths: J:DevOpsAgent01_work13some_folderadres.cs

1 suggest that the configuration file was found, and 2 suggests that the file was processed. However, 3 suggests that file adres.cs is still being analyzed, which I have confirmed by looking in SonarQube where the file is still visible.

Option B

I modified my YAML build pipeline to include one an extra line that specifies which file to exclude, that is the last line of the snippet below:

task: SonarQubePrepare@6
  inputs:
    SonarQube: 'SonarQubeOnDocker'
    projectKey: MyProjectKey
    projectName: MyProjectName
    extraProperties: |
      sonar.host.url=myUrl
      sonar.exclusions=**/adres.cs #Only used in Option B
      sonar.verbose=true  

These are the properties that I’ve specified, the rest use the default values, which according to the documenation means that: scannerMode: 'MSBuild'

After running the modified pipeline, the file no longer shows up in SonarQube. Also, the output on numerous places states Excluded sources: **/adres.cs

Any idea why the second approach works but not the first?

Update

I modified the YAML task definition to

task: SonarQubePrepare@6
  inputs:
    SonarQube: 'SonarQubeOnDocker'
    projectKey: MyProjectKey
    projectName: MyProjectName
    configFile: 'sonar-project.properties'
    extraProperties: |
      sonar.host.url=myUrl
      sonar.verbose=true  

This still does not work.

Solution

Based on the comments from Kevin Lu, I updated to the YAML task definition to

task: SonarQubePrepare@6
  inputs:
    SonarQube: 'SonarQubeOnDocker'
    projectKey: MyProjectKey
    projectName: MyProjectName
    scannerMode: 'CLI'
    configFile: 'sonar-project.properties'
    extraProperties: |
      sonar.host.url=myUrl
      sonar.verbose=true  

The important piece here is the scannerMode property. When I set it to the non-default value of CLI, the configuration file is being used.

7

Refer to this ticket: Not able to exclude sonar-project.properties files from VSTS pipeline

sonar-project.properties file already in the source code is not compatible with the Scanner for MSBuild. The reason is that the scanner generates itself a file, with all sources reference, to be able to be analyzed by the underlying base scanner.

The cause of the issue could be that the sonar-project.properties is not compatible with MSBuild mode.

To use the sonar-project.properties in sonarqube task, you can change to use CLI mode.

In this case, you can define the sonar-project.properties file in configFile field.

Here is an example:

steps:
- task: SonarQubePrepare@6
  displayName: 'Prepare analysis on SonarQube'
  inputs:
    SonarQube: xx
    scannerMode: CLI
    configMode: 'file'
    configFile: 'sonar-project.properties'
    extraProperties: |
       xxxx

For more detailed info, you can refer to this doc: SonarQubePrepare@6 – Prepare Analysis Configuration v6 task

configFile: ‘sonar-project.properties’ # string. Optional. Use when scannerMode = CLI && configMode = file. Settings File. Default: sonar-project.properties.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật