SonarLint Exclusion Setting Doesn't Work
About
This article describes the issue I faced when trying to configure pre-commit and CI pipeline for my tech blog site using SonarLint.
Specifically, I encountered a problem where SonarLint's exclusion settings didn't work as expected.
Situation
The directory structure
Follows the standard Next.js layout:
app/
├── layout.tsx
├── page.tsx
├── global.css
└── …
node_modules/ <– auto-generated by npm
coverage/ <– generated by Jest
public/
Requirement
Building a CI pipeline and pre-commit hook that runs the following:
-
prettier
check eslint
-
sonarlint
(code analysis) -
jest
tests with coverage
Command I wrote
npx prettier --check . &&
npm run lint &&
npx sonarlint --exclude 'node_modules/**,coverage/**' &&
npm test -- --coverage --ci --silent=false
Issue
The exclusion option in SonarLint doesn't work when multiple directories are specified using --exclude
:
- If I specify only one directory (e.g.,
node_modules/**
), it works fine. - However, specifying multiple directories (e.g.,
'node_modules/**,coverage/**'
) does not exclude them. - While I tried several patterns, one of them worked:
-
.sonarlintignore
file - SonarLint config file
- Wildcard variations (
**/node_modules/**
) - Separating with spaces or commas
-
Workaround
This appears to be a known SonarLint issue.
There are several similar unresolved reports on the SonarSource community, indicating a possible bug.
So as a temporary workaround, I added the following command before running the analysis:
rm -rf coverage &&
This simply deletes the folder before SonarLint scans the project.
Conclusion
In my case, there were only two directories to exclude, and one (coverage) could be safely removed beforehand.
However, this approach won't scale if I need to exclude more directories.
If you're facing the same issue, I recommend either:
- Avoiding unnecessary folders before analysis (if safe)
- Watching the SonarLint issue tracker for updates.
Discussion