SonarQube is a leading static code analysis tool that helps developers identify and fix code quality issues. This guide focuses on using SonarQube as a Cs Scanning Tool for C# projects, specifically addressing a common issue encountered when integrating SonarQube with .NET projects. We’ll walk through a practical example, troubleshoot a specific error, and provide solutions to ensure a smooth code analysis process.
Understanding the Issue: “File can’t be indexed twice”
When integrating SonarQube with .NET projects using the dotnet sonarscanner
command, developers often encounter the error: “File … can’t be indexed twice.” This typically occurs during the “end” phase of the analysis process, indicating a conflict in how SonarQube is processing source and test files. The root cause often lies in overlapping file paths specified during the analysis configuration. Let’s explore a real-world scenario and dissect the problem.
Case Study: Analyzing a Simple C# Project
Consider a basic C# project with a single “hello world” source file (Program.cs
) and a corresponding unit test file (ProgramTest.cs
). Following the official SonarScanner for .NET documentation, the analysis process usually involves three steps:
Step 1: Begin Analysis
dotnet sonarscanner begin /k:"MY-KEY" /d:sonar.moduleKey="MY-MODULE-KEY" /d:sonar.login="TOKEN" /d:sonar.verbose="true" /d:sonar.host.url="https://my.sonarqube.server" /d:sonar.analysis.mode="publish" /d:sonar.scm.disabled="true" /d:sonar.sources="src/PROJECT/SRC-DIR" /d:sonar.tests="tests/PROJECT.TESTS/SRC-DIR" /d:sonar.dotnet.excludeTestProjects="true" /d:sonar.cs.vscoveragexml.reportsPaths="coverage-reports/*.xml"
This command initializes the SonarScanner and sets various parameters, including the project key, login token, SonarQube server URL, source and test directories, and code coverage report paths. Crucially, sonar.sources
and sonar.tests
define the directories containing the source and test files respectively.
Step 2: Build and Test
dotnet build
dotnet-coverage
This step compiles the project and runs unit tests, generating code coverage reports.
Step 3: End Analysis and Publish Results
dotnet sonarscanner end
This command finalizes the analysis and sends the results to the SonarQube server. However, in this scenario, the process fails with the “File can’t be indexed twice” error.
Debugging the Issue: Log Analysis
Examining the SonarScanner logs reveals that the same file (Program.cs
) is being indexed multiple times, belonging to different modules:
INFO: Indexing files of module 'PROJECT'...
INFO: Indexing files of module 'PROJECT.TESTS'...
INFO: Indexing files of module 'PROJECT'...
INFO: Indexing files of module 'SONARQUBE-PROJECT-KEY'...
This redundant indexing triggers the error. The issue stems from how the sonar.sources
and sonar.tests
parameters are defined, potentially leading to overlapping file inclusions.
Solution: Ensuring Disjoint File Sets
To resolve this issue, ensure that the sonar.sources
and sonar.tests
parameters specify distinct and non-overlapping sets of files. Review your project structure and adjust these parameters accordingly. Here’s a possible correction:
- Refine
sonar.sources
: Specify the exact path to the directory containing only source code files, excluding any test files. - Verify
sonar.tests
: Ensure it points to the directory containing only test files.
By explicitly defining disjoint sets for source and test files, you eliminate the possibility of duplicate indexing and resolve the “File can’t be indexed twice” error, ensuring a successful SonarQube analysis. This precise configuration allows SonarQube to accurately differentiate between production code and test code, leading to more accurate analysis results and a clearer understanding of code quality. Double-check your configuration and rerun the analysis to confirm the fix.