Sub topic 9
Failing a run with a severity threshold
This chapter assumes you can run sf code-analyzer run against a Salesforce DX project and read its results. It also assumes you know how to read a shell exit status and have used a CI workflow such as GitHub Actions. By the end, you will be able to make a scan fail on serious findings, choose a threshold that fits your team, and avoid the common mistakes.
Why a scan needs an exit status
A scan that only prints violations is advice. Nobody has to read it. A scan that returns a nonzero exit status can stop a script, block a pull request, or halt a deployment. The --severity-threshold flag turns the first kind of scan into the second.
Without the flag, sf code-analyzer run finishes with status 0 as long as the scan itself completes, even when it finds Critical violations. With the flag, the command also compares each violation's severity to your threshold and fails if any violation meets or exceeds it.
Severity levels and the direction of the scale
Code Analyzer uses five severity levels. The numbering runs opposite to what many people expect: 1 is the most severe.
| Number | Name | Meaning |
|---|---|---|
| 1 | Critical | Must be fixed. Likely a defect or serious security problem. |
| 2 | High | Should be fixed before release. |
| 3 | Moderate | Worth fixing. Often a maintainability or performance issue. |
| 4 | Low | Minor style or convention issue. |
| 5 | Info | Informational. |
A threshold of 2 means "fail if any violation has severity 2 or a smaller number". That covers High and Critical. A threshold of 1 fails only on Critical. A threshold of 5 fails on every violation.
Setting the threshold
Pass the flag to run. The flag takes the number, and it also accepts the severity name.
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2The command still prints or writes its results as usual. After that, it checks the threshold. If at least one violation is severity 2 or more severe, it reports an error and exits with a nonzero status. If none are, it exits with status 0.
You can verify this in a terminal by reading the shell's last exit status:
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2
echo "Exit status: $?"A status of 0 means no violation reached the threshold. Any other value means at least one did. Scripts and CI systems treat every nonzero value as failure, so do not depend on the exact number.
How the decision is made
The threshold check runs after the engines finish and after any severity overrides from your configuration file are applied. The diagram shows the order.
Two consequences follow from this order.
- Results are produced even when the run fails. You can upload a report from a failed job, which is what the CI workflow at the end of this tutorial does with
if: always(). - Overrides in
code-analyzer.ymlchange what counts as a failure. If you lower a rule from High to Info, it no longer trips a threshold of 2. If you raise a rule to Critical, it trips even a threshold of 1.
Seeing it fail
Take the AccountUpdater class from earlier in the tutorial. It has a DML statement inside a loop.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}The PMD rule for DML in loops reports this class. Its severity depends on the rule's default, which can change between plugin versions, so check the severity column of your own output instead of assuming it. Run the same scan twice with different thresholds:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view table --severity-threshold 1
echo "Threshold 1 exit: $?"
sf code-analyzer run --workspace force-app --rule-selector Recommended --view table --severity-threshold 3
echo "Threshold 3 exit: $?"The table is identical in both runs. Only the exit status can differ. If the DML-in-loop violation is severity 3 in your version, the first command exits 0 and the second fails. Looser thresholds (larger numbers) fail more often.
Choosing a threshold
A threshold is a policy decision. These criteria help you pick one.
- Start where the code already passes. Run once without the flag and note the most severe violation present. If your project has Critical findings today, a threshold of 1 will fail immediately. Fix those first or you will train the team to ignore a red build.
- Tighten in steps. Begin with 1, fix what appears, and then move to 2. Each step is a small, reviewable cleanup.
- Gate on severity, not on count. The flag has no "allow up to N violations" setting. If you need a ratchet that allows old findings but blocks new ones, scan only the changed files with
--targetand keep a stricter threshold there. - Match the threshold to the rule set. A threshold of 3 on
Recommendedis a very different gate from a threshold of 3 onall, becauseallincludes many style rules. Change the selector and the threshold together, and record both in your pipeline.
Common errors
Reading the scale backwards. Setting --severity-threshold 4 expecting "only the serious ones" fails the build on Low findings too. Remember that the number is a ceiling on how minor a failing violation can be.
Assuming a missing flag means a safe scan. A pipeline without --severity-threshold passes with violations present. If a job is green but you suspect problems, check the command first.
Hiding the failure with the shell. Appending || true to the command, or piping its output through another command in a shell without pipefail, replaces the scan's status with another command's status. In a script that pipes output, set the option explicitly:
set -o pipefail
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2 --view table | tee scan.logHere tee always succeeds, but with pipefail the pipeline reports the scan's failure.
Confusing a failed scan with a failed threshold. A nonzero status can also mean the scan could not run, for example because Java is missing for PMD or the configuration file has an invalid key. Read the error message printed before the status. A threshold failure names the violation count and the severity.
Forgetting overrides when debugging. A teammate lowers a rule's severity in code-analyzer.yml, and the gate quietly weakens. Review changes to that file as carefully as changes to the pipeline.
A reusable gate script
Wrapping the command makes the policy visible in one place:
#!/usr/bin/env bash
set -euo pipefail
THRESHOLD="${1:-2}"
sf code-analyzer run \
--workspace force-app \
--rule-selector Recommended \
--severity-threshold "$THRESHOLD" \
--output-file results.html
echo "No violations at severity $THRESHOLD or worse."Because of set -e, the script stops at the scan's failure and never prints the final message. Run ./gate.sh 1 locally to try the strictest gate, or ./gate.sh 3 to try a looser one.
Exercise
Your team's code-analyzer.yml lowers the severity of a PMD rule from 2 (High) to 5 (Info). Your pipeline runs with --severity-threshold 2. A file contains a violation of that rule and no other violations. Does the job fail? What changes if you remove the override?
Show answer
The job passes. Overrides are applied before the threshold check, so the violation counts as severity 5, which is less severe than 2. If you remove the override, the violation returns to severity 2, which meets the threshold, so the command exits with a nonzero status and the job fails.
Exercise
A scan prints two violations, one Moderate (3) and one Low (4). You run it with --severity-threshold 4, then with --severity-threshold 2. What exit status do you expect from each run?
Show answer
The run with threshold 4 exits nonzero, because both violations are severity 4 or more severe. The run with threshold 2 exits 0, because neither violation is as severe as High (2) or Critical (1).