Latest
Sonar 1 min read

Choose Which Files SonarQube Analyzes

Choose Which Files SonarQube Analyzes
Sonar · August 5, 2026

Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.

What this guide covers

Analysis scope is the set of files SonarQube checks. Clear source, test and exclusion paths prevent generated files or test files from being counted as application code.

Before you start

Run the scanner from the project root. Identify the actual source, test and generated-code directories first. Paths below are examples, not required directory names.

Step 1: Set source and test directories

For separate src and test directories, use:

sonar.sources=src
sonar.tests=test
sonar.exclusions=src/generated/**
sonar.coverage.exclusions=src/generated/**

sonar.exclusions removes files from source analysis. sonar.coverage.exclusions keeps analysis but removes those files from coverage calculations. Only exclude generated code if that matches your project’s policy.

Step 2: Handle tests beside source files

If TypeScript tests live under src, use this alternative instead of the previous configuration:

sonar.sources=src
sonar.tests=src
sonar.test.inclusions=src/**/*.test.ts
sonar.exclusions=src/**/*.test.ts,src/generated/**
sonar.coverage.exclusions=src/generated/**
sonar.cpd.exclusions=src/generated/**

The inclusion and exclusion patterns keep each file from being treated as both source and test. sonar.cpd.exclusions excludes matching files from duplicate-code checks only.

Step 3: Run analysis and review the file list

List the tracked files before scanning:

git ls-files src | head -30
git ls-files 'src/**/*.test.ts' | head -20
git status --short --untracked-files=normal

Run sonar-scanner and inspect its file counts and warnings. In SonarQube, confirm that expected source files appear and generated files follow your chosen policy.

Check the result

Check at least one application file, test file and excluded file. If coverage rises after an exclusion, verify whether files were removed from the calculation rather than improved by tests.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Create and Rotate SonarQube Analysis Tokens

Sonar August 15, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.