Sub topic 5
Running your first scan
This chapter walks through one complete scan: a file with a known problem, the command that finds it, and the steps to fix it and confirm the fix.
Assumptions
This chapter assumes the following:
- You installed the Salesforce CLI and the
code-analyzerplugin in the previous chapter.sf code-analyzer rulesshould list rules without errors. - Java 11 or later is available on your machine, because the PMD and CPD engines run on Java.
- You are working in a Salesforce DX project with a
force-appfolder. - You can read basic Apex.
What a scan does
sf code-analyzer run takes three kinds of input and produces one result set.
- A rule selector says which rules to use.
- A workspace says which files the engines may read.
- An optional target narrows the scan to specific files inside that workspace.
The plugin starts the engines that supply the selected rules and gives them your files. It then merges their findings into one list.
Create a file with a known problem
Save this class as force-app/main/default/classes/AccountUpdater.cls. The scanner reads source text, so you don't need the -meta.xml file for a scan. You would need it to deploy.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}The update acc; statement runs inside the loop. Each iteration issues its own DML statement. Apex allows 150 DML statements per transaction, so a list of 200 accounts fails at runtime. A PMD rule catches this pattern before the code runs.
Run the scan
From the project root, run:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableEach part of the command does the following:
--workspace force-applets the engines read every file underforce-app.--rule-selector Recommendeduses the rules taggedRecommended. This is a curated set chosen to give useful findings with limited noise.--view tableprints the findings as a table in the terminal.
The command accepts short flags too: -w, -r, and -t. The long forms are easier to read in scripts and review comments, so this tutorial uses them.
Two defaults are worth knowing. If you omit --rule-selector, the plugin uses Recommended. If you omit --workspace, it uses the current directory. Running the command from your project root with no flags therefore does nearly the same scan. Spelling out both flags makes the intent visible to the next reader.
What happens during a run
The first run is slower than later ones, because the plugin starts a Java process for PMD and loads the other engines. The terminal prints progress messages while the engines work. Engines that no selected rule needs don't start.
The scan covers files an engine understands. PMD reads Apex, Visualforce, and other supported file types. ESLint reads JavaScript, including Lightning Web Component files. Other file types in the workspace are ignored, so a large workspace is not a problem in itself.
Read the first result
The table has one row per violation. These are the columns you will use most:
| Column | Meaning |
|---|---|
| Severity | A number from 1 (Critical) to 5 (Info). Lower numbers are more serious. |
| Rule | The engine and the rule name, such as `pmd:AvoidDmlStatementsInLoops`. |
| Location | The file path with the line and column where the problem starts. |
| Message | A short description of what the rule found. |
For AccountUpdater.cls, expect a row whose rule is pmd:AvoidDmlStatementsInLoops and whose location points at the update acc; line. The severity number in your table comes from the installed rule definitions, so use your output as the source of truth. Other rows may appear if the Recommended set also flags other things in your project, such as missing ApexDoc comments or code style issues.
The engine:rule form in the Rule column is the same form you use in selectors and in code-analyzer.yml later in the tutorial. Note the rule name now, because you will need it to override or suppress a rule.
Use --view detail when the table is too narrow, for example with long paths or messages. It prints each violation as a block with its full message and any resource links the rule provides:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view detailFix the finding and scan again
Move the DML statement out of the loop and update the whole list once:
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
}
update accounts;
}
}Run the same command again. The row for AvoidDmlStatementsInLoops should be gone. The scan is deterministic: the same files and the same rules give the same findings. A disappearing violation after an edit is therefore reliable evidence that your change addressed it.
Scan a single file
Scanning the whole workspace is slow on a large project when you only edited one file. Add --target to limit which files are reported, and write the report to a file at the same time:
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountUpdater.cls --rule-selector pmd:Security --output-file results.htmlHere the selector pmd:Security means "PMD rules tagged Security", so this scan is narrower in two ways. The --output-file flag chooses the report format from the file extension, and .html produces an HTML report. When you pass --output-file, the plugin writes the file and still prints a short summary in the terminal. The next chapters cover workspaces, targets, and output formats in detail.
Common errors
command sf code-analyzer not found. The plugin is not installed, or you have the oldersf scannerplugin. Runsf pluginsto check, thensf plugins install code-analyzer.- The scan reports zero violations on code you know is bad. Check that the file sits inside the
--workspacefolder. Then check that your selector includes the rule. A narrow selector such aspmd:Securitywill not report a performance rule. - A Java error appears when PMD starts. Confirm that
java -versionshows version 11 or later and thatjavais on yourPATH. - The run takes a long time. The workspace probably includes folders you do not own, such as
node_modulesor static resources with bundled libraries. A later chapter shows how to ignore them incode-analyzer.yml. - Paths in the results look wrong. Run the command from the project root, or pass absolute paths. Relative paths are resolved from the directory where you run the command.
Checklist for a first scan
- Confirm the plugin works with
sf code-analyzer rules --rule-selector Recommended. - Run from the project root with an explicit workspace and selector.
- Read the Severity, Rule, and Location columns first.
- Fix one finding, then re-run the same command.
- Keep the command unchanged between runs so you can compare results.
Exercise
You run sf code-analyzer run --workspace force-app --rule-selector pmd:Security and get no violations, but you know AccountUpdater.cls contains DML inside a loop. Why is the result expected, and what change to the command would report the problem?
Show answer
The selector pmd:Security includes only PMD rules tagged Security. The DML-in-loop rule is a performance rule, so it is not part of this scan. Use --rule-selector Recommended (or a selector that includes that rule, such as pmd:Performance) and run the scan again.
Exercise
What do --workspace and --rule-selector default to if you leave them out, and why might you still write them out in a script?
Show answer
The workspace defaults to the current directory and the rule selector defaults to Recommended. Writing them out makes the scan's scope and rules visible in the script, so the result does not depend on the directory the script happens to run from.