Sub topic 16
Moving from sf scanner to sf code-analyzer
This chapter assumes you have run scans with sf code-analyzer as covered in earlier chapters. It is for teams that still have scripts, CI jobs, or habits built on the older sf scanner plugin (version 4). It covers what changed, how to translate commands, and how to switch a pipeline without losing coverage.
What changed between v4 and v5
The v4 plugin shipped as @salesforce/sfdx-scanner and exposed the sf scanner commands. Version 5 is a rewrite published as the code-analyzer plugin, with the sf code-analyzer command group. The rewrite changes how you choose rules, how you configure a scan, and how results are scaled.
| Area | v4 (`sf scanner`) | v5 (`sf code-analyzer`) |
|---|---|---|
| Run a scan | `sf scanner run` | `sf code-analyzer run` |
| List rules | `sf scanner rule list` | `sf code-analyzer rules` |
| Data-flow scan | `sf scanner run dfa` | `sf code-analyzer run` with the graph engine selected |
| Choose rules | `--engine`, `--category`, `--ruleset` | `--rule-selector` |
| Source location | `--projectdir` and `--target` | `--workspace` and `--target` |
| Output format | `--format` and `--outfile` | `--view` and `--output-file` |
| Configuration | Engine-specific flags and files | One `code-analyzer.yml` |
| Severity scale | Engine-specific, unless `--normalize-severity` is set | Always 1 (Critical) to 5 (Info) |
The main shift is that v5 treats selection and configuration as separate, declarative concerns. A rule selector says which rules run. The configuration file says how they behave.
flowchart TDDo not use the fragment above. The diagram for this chapter follows.
Install v5 and remove v4
Install the new plugin and check which plugins you have. Then remove the old one once your scripts no longer call it.
sf plugins install code-analyzer
sf plugins
sf plugins uninstall @salesforce/sfdx-scannerRun the uninstall only after you have migrated every script. If you keep both for a while, the command groups are different, so they will not collide, but two tools reporting different results on the same code confuses reviewers.
Translating common commands
Selecting rules
In v4 you chose rules with several separate flags. A typical security-focused call looked like this:
sf scanner run --target force-app --engine pmd --category Security --format tableIn v5 the same intent is one selector. A selector combines an engine name and a tag with a colon:
sf code-analyzer run --workspace force-app --rule-selector pmd:Security --view tableSelectors can also name a preset such as Recommended, or a single rule. Before you rely on a selector, list what it matches:
sf code-analyzer rules --rule-selector pmd:SecurityIf the output is empty or much shorter than you expected, the selector is wrong. Fix it before you trust a "clean" scan.
Scope: project directory versus workspace
In v4, --target named the files to scan and --projectdir told the tool where the project's other files were. In v5, --workspace names the whole body of code the engines may look at, and --target narrows the files that get reported on. The graph engine and rules that need cross-file context use the workspace, so keep it at the project level (force-app) even when you scan one file:
sf code-analyzer run \
--workspace force-app \
--target force-app/main/default/classes/AccountUpdater.cls \
--rule-selector RecommendedOutput
v4 used --format with --outfile. In v5, --view controls what prints in the terminal (table or detail), and --output-file writes a file whose format comes from its extension:
sf code-analyzer run --workspace force-app --rule-selector Recommended \
--view table --output-file results.htmlYou can pass --output-file more than once to write, for example, an HTML report for people and a SARIF file for tooling in the same run.
Severity: the change most likely to break a pipeline
v4 reported each engine's native severity unless you added --normalize-severity. Many teams set a threshold such as --severity-threshold 1 against whichever scale they happened to see. In v5 every rule uses one scale:
- Critical
- High
- Moderate
- Low
- Info
The threshold means "fail if any violation is at this level or more severe." A lower number is more severe, so --severity-threshold 2 fails the run on Critical or High findings.
Also expect different counts. v5 changes the default rule sets and may rate a rule differently than v4 did. A new scan reporting more or fewer findings than the old one is normal and does not mean either tool is broken.
Moving configuration into code-analyzer.yml
v4 settings were spread across command flags and engine-specific files. In v5, move the settings you want to keep into code-analyzer.yml. Ignored paths and rule severity overrides are the two you will migrate most often:
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
rules:
pmd:
AvoidDebugStatements:
severity: "Info"Then point the run at it:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector RecommendedSettings for a specific engine, such as custom PMD rulesets or an ESLint configuration, also belong in this file under that engine's section. Check the Code Analyzer documentation for the exact keys for your version, because those sections are the part of the schema most likely to grow between releases.
Suppressions in existing code
Inline suppressions written for PMD, such as // NOPMD, are a PMD feature, so they keep working where the PMD engine is still running the same rule. Add the reason next to the comment, as you did in earlier chapters:
public class Logger {
public static void trace(String msg) {
System.debug(msg); // NOPMD - intentional debug logging in sandbox-only utility
}
}After migrating, search for old suppressions and confirm each one still silences a finding. A suppression for a rule that no longer exists is dead weight. A rule that v5 now reports under a different name will show up as a new violation.
Migrating a CI job
Here is a v4-style step and its v5 replacement:
# Before
- run: sf plugins install @salesforce/sfdx-scanner
- run: sf scanner run --target force-app --format sarif --outfile results.sarif --severity-threshold 2# After
- run: sf plugins install code-analyzer
- run: sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2 --output-file results.sarifKeep the artifact upload step with if: always(), so the report is saved even when the threshold makes the job fail.
A safe migration sequence
- Install v5 beside v4 and list the rules your v4 flags selected.
- Run both tools on the same branch and save both reports.
- Compare findings by file and rule. Note the rules that appear in only one report and decide whether each difference is acceptable.
- Move ignores and severity overrides into
code-analyzer.yml. - Run v5 without a threshold, pick a threshold on the 1 to 5 scale, then add it to CI.
- Change CI to call
sf code-analyzer run, then uninstall@salesforce/sfdx-scanner.
Data-flow scans
v4 had a separate sf scanner run dfa command for data-flow analysis. In v5 the graph engine is a normal engine selected through --rule-selector, and it reads the whole workspace. Because it is slower than the other engines, run it on a schedule or on merges to your main branch, and keep the quick Recommended scan on pull requests.
Common errors
- Unknown command
sf scanner: the old plugin is gone. Usesf code-analyzer run. - Unknown flag
--engineor--category: use--rule-selectorinstead. - Zero violations after migrating: check the selector with
sf code-analyzer rulesand confirm the workspace points at the right folder. - CI fails on a new threshold: the severity scale differs. Re-derive the number from a no-threshold run.
Exercise
A v4 job runs sf scanner run --target force-app --engine pmd --category Security --format sarif --outfile out.sarif. Write the v5 command that scans the same folder with the same rules and writes the same file. Then say what you should check before relying on its exit status.
Show answer
sf code-analyzer run --workspace force-app --rule-selector pmd:Security --output-file out.sarif. Before relying on the exit status, run sf code-analyzer rules --rule-selector pmd:Security to confirm the selector matches the rules you expect. If you add --severity-threshold, choose the number from the v5 scale (1 Critical to 5 Info) by looking at a run without a threshold, because v4 severity values do not carry over directly.