Sub topic 3
Configuring Salesforce Code Analyzer with `code-analyzer.yml`
Assume you know basic Apex, Salesforce CLI commands, and the Salesforce DX project layout. A configuration file keeps scan behavior consistent across local development and CI.
Configure rules and paths
Create code-analyzer.yml at the project root:
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
rules:
pmd:
AvoidDebugStatements:
severity: "Info"The ignores.files patterns exclude paths from analysis. The rules section changes settings for a specific engine and rule. Keep exclusions narrow. Ignoring your application source can hide real problems.
Rule selectors choose rules when you run a scan. For example, Recommended selects Salesforce's recommended set, while pmd:Security selects PMD rules tagged Security.
sf code-analyzer run --config-file code-analyzer.yml \
--workspace force-app --rule-selector RecommendedYou can override a rule's severity in the configuration. Use selectors to choose rule tags rather than assuming every rule is enabled. Check available names first:
sf code-analyzer rules --rule-selector RecommendedTriage findings and false positives
Start with the file, line, rule, and engine named in each result. Fix the underlying code when possible. For example, move DML outside a loop instead of suppressing a governor-limit warning.
Suppress a finding only when the code is intentionally safe and the reason will remain understandable:
System.debug(msg); // NOPMD - intentional debug logging in sandbox-only utilityAdd the scan to CI
A pull-request workflow can fail on high-severity findings and preserve the report:
- run: sf code-analyzer run --workspace force-app \
--rule-selector Recommended --severity-threshold 2 \
--output-file results.sarif
- uses: actions/upload-artifact@v4
if: always()
with:
name: code-analyzer-results
path: results.sarifGraph analysis and migration
The graph engine, SFGE, follows data flow between operations. Use it for questions such as whether untrusted input reaches a sensitive operation. Other engines mainly match syntax, patterns, or dependencies.
If you used sf scanner v4, move to the Code Analyzer plugin and replace scanner commands with sf code-analyzer commands. Recheck selectors and configuration before enforcing CI thresholds.
Exercise
Why should a high-severity finding usually be fixed instead of suppressed?
Show answer
Fixing removes the underlying risk, while suppression only hides the report.