Sub topic 16
Moving from sf scanner to sf code-analyzer
This chapter assumes you have run scans with the older sf scanner commands (Code Analyzer v4). It also assumes you have read the earlier chapters on sf code-analyzer run, rule selectors, and code-analyzer.yml. It shows how to translate your existing commands and settings to Code Analyzer v5.
What changes
The v4 plugin is @salesforce/sfdx-scanner, and its commands live under sf scanner. The v5 plugin is code-analyzer, and its commands live under sf code-analyzer. Both can be installed side by side, so you can migrate one script at a time. Once everything is moved, remove the old plugin.
sf plugins install code-analyzer
sf plugins uninstall @salesforce/sfdx-scannerThe main shift is how you choose rules. v4 filtered by --category and --engine. v5 uses one --rule-selector option that can combine engines, tags, severities, and rule names. v5 also adds engines that v4 did not have, such as Regex, Flow, and CPD.
Command and flag mapping
| v4 (`sf scanner`) | v5 (`sf code-analyzer`) |
|---|---|
| `sf scanner rule list` | `sf code-analyzer rules` |
| `sf scanner run` | `sf code-analyzer run` |
| `sf scanner run dfa` | `sf code-analyzer run` with the graph engine selected |
| `--target` (files to scan) | `--workspace` (project scope) and `--target` (files within it) |
| `--category`, `--engine` | `--rule-selector` |
| `--format`, `--outfile` | `--view` and `--output-file` |
| `--severity-threshold` | `--severity-threshold` |
| `Config.json` in the scanner home folder | `code-analyzer.yml` |
Two differences cause most migration problems:
- In v4,
--targetwas the main way to say what to scan. In v5,--workspacedefines the project scope, and--targetnarrows the scan to files inside it. - v4 output formats came from
--format, and the format was chosen explicitly. In v5, the extension of the--output-filevalue (for example.htmlor.sarif) selects the file format.
Translating a real command
A typical v4 CI step looked like this:
sf scanner run --target "force-app" --category "Security" --engine "pmd" --format table --severity-threshold 2The v5 equivalent selects PMD rules tagged Security and prints a table:
sf code-analyzer run --workspace force-app --rule-selector pmd:Security --view table --severity-threshold 2Check the threshold number when you migrate. v5 reports severity as 1 (Critical), 2 (High), 3 (Moderate), 4 (Low), and 5 (Info). A threshold that gated your v4 builds may not match the same findings in v5, because the engines and rule sets differ.
Migrating your settings
Move settings from the v4 Config.json into code-analyzer.yml. Rule severity changes go under rules, and skipped paths go under ignores. Inline suppressions such as // NOPMD keep working, because they belong to the underlying engines.
# code-analyzer.yml
ignores:
files:
- "**/staticresources/**"
rules:
pmd:
AvoidDebugStatements:
severity: "Info"Pass the file explicitly in scripts so the behavior does not depend on the working directory:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector RecommendedCommon errors
- Old flags rejected.
--category,--engine,--format, and--outfiledo not exist in v5. Replace them with--rule-selector,--view, and--output-file. - Different violation counts. The Recommended selector is not the same as running every v4 category. Compare the two outputs before you change CI gates.
- Both plugins in CI. Leaving both installed is harmless, but a pipeline that still calls
sf scannerwill not benefit from v5 rules. Search your pipeline files forsf scanner.
Exercise
Rewrite this v4 command for v5: sf scanner run --target "force-app" --category "Performance" --format csv --outfile out.csv. What must you decide that v4 did not ask for?
Show answer
One option is sf code-analyzer run --workspace force-app --rule-selector Performance --output-file out.csv. Performance is a tag in the selector. You must now choose a workspace for the project scope, and the .csv extension picks the file format. Run sf code-analyzer rules --rule-selector Performance first to confirm which rules the selector matches.