如何解决TFS中Run Code Analysis任务的文件无法重复索引错误?
解决SonarQube "File can’t be indexed twice" 错误
你用SonarQube 7.4搭配TFS的Run Code Analysis 4任务时碰到的这个错误,本质是SonarQube发现同一个文件被同时标记成了主代码文件和测试代码文件,或者你的包含/排除规则导致文件被重复纳入扫描范围。结合你只想扫描Code文件夹的需求,咱们可以按下面几步来修复:
1. 精准设置sonar.sources(最直接的办法)
你现在把sonar.sources设成了项目根目录$(Build.SourcesDirectory),再靠sonar.inclusions=Code/**来过滤,这种方式很容易让SonarQube误判其他目录的文件归属。不如直接把sonar.sources指向你要扫描的Code文件夹:
sonar.sources=$(Build.SourcesDirectory)/Code
这样SonarQube只会盯着Code目录下的内容扫,从根源上减少了无关目录带来的干扰,重复索引的概率也会大大降低。
2. 明确区分主代码和测试代码
如果你的Code文件夹里混有测试文件(比如*Tests.cs这类命名的),得明确告诉SonarQube哪些是测试文件,别让它和主代码搞混:
- 要是测试文件都放在
Code下的子文件夹(比如Code/Tests),就添加sonar.tests指定测试目录:sonar.tests=$(Build.SourcesDirectory)/Code/Tests - 要是测试文件和主代码混在一起,直接用
sonar.exclusions把它们排除在主代码扫描之外:sonar.exclusions=**/*Tests.cs,**/Test*.cs
3. 检查C/C++构建输出路径的冲突
看你的配置里sonar.cfamily.build-wrapper-output设成了根目录,这可能会让SonarQube解析根目录下的构建文件,不小心把非Code目录的内容也拉进来。如果你的C/C++代码只在Code目录里,把这个路径也调整一下:
sonar.cfamily.build-wrapper-output=$(Build.SourcesDirectory)/Code
4. 用日志定位具体重复文件
你已经开了sonar.verbose=true,可以去TFS构建的详细日志里搜被重复索引的文件路径。找到这些文件后,针对性调整包含/排除规则,确保它们只属于主代码或者测试代码中的一类。
调整后的完整配置参考
sonar.verbose=true sonar.sources=$(Build.SourcesDirectory)/Code sonar.cs.vstest.reportsPaths=**/*.trx sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml sonar.cfamily.build-wrapper-output=$(Build.SourcesDirectory)/Code sonar.cfamily.threads=6 sonar.cfamily.cppunit.reportsPath=**/result.xml sonar.branch.name=$(Build.SourceBranchName) sonar.dependencyCheck.reportPath=$(Build.ArtifactStagingDirectory)\dependency-check-report.xml sonar.dependencyCheck.htmlReportPath=$(Build.ArtifactStagingDirectory)\dependency-check-report.html # 要是有测试文件需要排除,解开下面这行的注释 # sonar.exclusions=**/*Tests.cs
内容的提问来源于stack exchange,提问作者Naveen Kumar
相关产品推荐
相关产品推荐

