Sub topic 9
Failing a Run with a Severity Threshold
This chapter assumes you can run sf code-analyzer run against a workspace and read the results table. It also assumes you know the severity levels. If not, review the chapter on reading results first.
Why a threshold matters
By default, a scan that finds violations still finishes normally. The command prints its findings and exits with status 0. A script or CI job sees that as success, so serious problems can pass unnoticed.
The --severity-threshold flag changes this. You pick a severity level. If the scan finds any violation at that level or more severe, the command exits with a nonzero status. Scripts and CI jobs treat a nonzero exit as a failure.
How severity numbers work
Code Analyzer uses numbers from 1 to 5. A lower number is more severe.
| Number | Name |
|---|---|
| 1 | Critical |
| 2 | High |
| 3 | Moderate |
| 4 | Low |
| 5 | Info |
The comparison runs in the direction that surprises many people. A threshold of 2 means "fail on severity 2 or lower numbers", which is High and Critical. It does not mean "2 and above" in the numeric sense.
Using the flag
Here is a scan that fails on High or Critical findings.
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2If no violation has severity 1 or 2, the command exits with status 0. Moderate, Low, and Info findings are still printed, but they do not affect the exit status.
You can check the result in a shell with the $? variable.
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2
echo "Exit status: $?"A nonzero value means the threshold was met or exceeded. The report is still produced either way, so you can read what caused the failure.
Choosing a threshold
The right value depends on how clean your codebase is today.
- Threshold 1 fails only on Critical findings. It is a gentle start for a codebase with many existing violations.
- Threshold 2 fails on High and Critical. It is a common choice for pull request checks.
- Threshold 3 or lower numbers in the other direction (4, 5) is strict. A threshold of 5 fails on any violation at all, including Info.
A practical approach is to start with a loose threshold, fix the findings at that level, and then tighten it one step at a time. If you start too strict on an existing project, every run fails and the team learns to ignore the check.
Combining with severity overrides
The threshold works on the severity each rule has after configuration is applied. If your code-analyzer.yml lowers a rule to Info, that rule no longer trips a threshold of 2. Raising a rule to High makes it start failing runs. This lets you tune what blocks a build without changing the threshold value itself.
Common mistakes
- Reading the number backward and expecting
--severity-threshold 5to be the loosest setting. It is the strictest. - Assuming the threshold removes lower-severity rows from the output.
- Forgetting that the report is written before the command exits, so an output file still exists after a failed run.
Exercise
Your scan reports one High violation (severity 2) and three Low violations (severity 4). What exit status results with --severity-threshold 3, and what about --severity-threshold 4?
Show answer
With threshold 3, the command exits nonzero because the High violation (2) is more severe than 3. With threshold 4, it also exits nonzero, because the High violation and the Low violations (4) all meet the threshold.
Exercise
A team wants their pull request check to fail only on Critical findings while they clean up old code. Which flag value should they use?
Show answer
--severity-threshold 1.