SonarQube扫描lcov.info异常:TypeScript UI项目覆盖率问题求助
Alright, let's tackle this problem head-on. The core issue here is that SonarQube's TypeScript plugin automatically maps coverage data from compiled .js files back to their source .ts files—something you don't want, since you want Sonar to strictly follow what's in your lcov.info report. Here's how to fix it:
1. Disable Automatic Coverage Mapping Between TS and JS
First, turn off the plugin's default behavior that links JS coverage to TS source files. Add this to your Sonar configuration:
sonar.typescript.excludeCoverageFromAutoscan=true
This tells Sonar to stop inferring TS coverage from JS files and instead use the exact paths listed in your lcov.info.
2. Fine-Tune Source File and Coverage Scopes
Next, adjust your configuration to avoid duplicate scanning and ensure Sonar respects your coverage report:
# Include only your TypeScript source files as project code sonar.inclusions=**/*.ts # Exclude compiled JS files from being scanned as source code (prevents duplicate errors) sonar.exclusions=**/*.js # Don't exclude any files from coverage analysis (so Sonar can read the JS paths in lcov.info) sonar.coverage.exclusions= # Explicitly point to your coverage report sonar.javascript.lcov.reportPaths=test-coverage/lcov.info
This setup ensures:
- Sonar only analyzes your
.tssource files for code quality - It doesn't treat compiled
.jsfiles as duplicate source code - It uses the exact coverage data from
lcov.infowithout mapping it back to TS files
3. Verify Your Setup
After applying these changes:
- Double-check that the file paths in
lcov.infoare relative to your project root (Sonar needs this to match files correctly) - Run your Sonar scan again, then check the coverage tab in SonarQube—you should see coverage only for the
.jsfiles listed in your report, with no unexpected TS coverage or duplicate scanning errors
Why Your Previous Configurations Failed
- Using
sonar.inclusions=**/*.tsalone: Sonar would still map JS coverage to TS files via the default plugin behavior - Setting
sonar.coverage.exclusions=**/*.ts: This excluded TS files from coverage, but left JS files as source code, leading to duplicate scanning of TS and JS - Using
sonar.language=ts: This restricted Sonar to only analyze TS files, which meant it ignored the JS coverage data inlcov.info
内容的提问来源于stack exchange,提问作者pkaramol

