Start FreeLog in
SonarCloud | Administering your SonarCloud | Setting configurations at organization level | Adjusting analysis scope | Excluding based on path-matching patterns

Was this page helpful?

Excluding files from analysis based on path-matching patterns

On this page

This feature is only available in an Enterprise plan organization.

As an organization admin, you can define, at the organization level, the exclusion of files from the project's analysis baseline based on file paths. It means that this analysis scope adjustment applies to all projects in the organization. However, they can be overridden at the project level in the UI or through analysis parameters set on the CI/CD host.

To exclude the files, you define file exclusion parameters based on directory and file name patterns.

Principles governing the use of file exclusion parameters

A file exclusion parameter:

  • Applies either to source code (non-test code) or to test code files.
    The SonarScanner must identify the non-test code as well as the test code since they are processed differently by SonarCloud. A code file is either a non-test or a test code; it cannot be both (If this is the case, the scanner will fail the analysis with an error message.).
  • Contains:
    • Either exclusion patterns: to define files to be excluded from the analysis scope.
    • Or inclusion patterns: to define files to be included in the scope.
      It means that the rest of the files is excluded from the analysis scope.

For a given code category (source or test), we strongly recommend that you use either exclusion-pattern or inclusion-pattern parameters, depending on what is simpler in your situation (If you do not and there is an overlapping, then exclusion patterns have precedence over inclusion patterns.). 

The following applies:

  • The parameter defined at the project level will override the same parameter defined at the organization level.
    For example, if you define at the organization level the exclusion pattern for source code src/**/test1/**/*, then, if the project administrator sets the exclusion pattern for source code src/**/test2/**/* for their project, the scanner will consider only the pattern src/**/test2/**/* to compute source file exclusion.
  • If test inclusion patterns are used, the scanner will automatically set these patterns as source file exclusion patterns during project analysis. These source file exclusion patterns will apply in addition to the other configured source file exclusion patterns.
    For example, if:
    • the exclusion pattern for source code is src/**/test5/**/* 
    • and the inclusion pattern for test code is src/**/test6/**/*
    • then the scanner will consider both patterns to compute the source file exclusion:
      src/**/test5/**/* and src/**/test6/**/*.

Defining a file exclusion parameter

  1.  Retrieve the organization.
  2. Go to Administration > Analysis scope.
  3. In Files, choose the parameter to configure (source or test code; exclusion or inclusion patterns), and enter and save the first pattern. See Wildcards…. 
  4. Add additional patterns to the parameter if necessary.
  5. Define other parameters if necessary. Make sure you use either Source File Exclusions or Source File Inclusions, and either Test File Exclusions or Test File Inclusions.

© 2008-2024 SonarSource SA. All rights reserved. SONAR, SONARSOURCE, SONARLINT, SONARQUBE, SONARCLOUD, and CLEAN AS YOU CODE are trademarks of SonarSource SA.

Creative Commons License