Salesforce Code Analyzer
How to run and configure Code Analyzer, which PMD and ESLint rules to select, what makes a run fail, and what to do with the findings.
A private bank has just installed Salesforce Code Analyzer over an org (Salesforce organization) with years of Apex inside it: how do you run it, where do you configure it, what makes a run fail, and what do you do with the hundreds of findings the first pass returns? The short answer is that these are four separate decisions — which rules, which severity, which threshold and what gets suppressed — and that none of them comes settled out of the box. This page walks them in that order.
What Code Analyzer checks on code written by an AI (artificial intelligence) assistant belongs to another page in this subcategory, not published yet: what the AI writes goes through these same gates. This page is about the operation.
The two engines that do the daily work
Code Analyzer analyses nothing by itself: it brings engines together, each engine runs its rules, and eight ship with it and are enabled. Two do almost all the work on a Salesforce repository, and both are frequently mis-scoped.
PMD is not “the Apex engine”. Its page defines it as “a source code analyzer that performs static analysis of code written in a number of languages” and lists five: Apex, HTML, JavaScript, XML and Visualforce. What is true is the next sentence, “the default languages are apex and visualforce”: the other three are supported and do not run unless somebody configures them.
ESLint is not “the LWC engine”. Its page defines it as “a popular linting tool for JavaScript, TypeScript, and LWC” — Lightning Web Components, Salesforce’s current component model. It ships separate catalogues for JavaScript, TypeScript, LWC, React and Salesforce Lightning Design System (SLDS), Salesforce’s design system, and each one is disabled on its own.
| Engine | The scope its page states | Dependency of its own |
|---|---|---|
| PMD | Apex, HTML, JavaScript, XML and Visualforce; by default only Apex and Visualforce, that is .cls, .trigger, .page and .component |
JDK 11 or later |
| ESLint | JavaScript, TypeScript and LWC, with separate catalogues for React and for SLDS; .js, .cjs, .mjs, .jsx, .ts and .tsx |
None |
JDK is the Java Platform, Standard Edition Java Development Kit, and the source requires it for PMD, for CPD and for Salesforce Graph Engine, not for PMD alone; Flow Scanner requires Python 3.10 or later. Engines whose dependencies will not be installed are disabled in the configuration and stop erroring.
Neither PMD, nor ESLint, nor YAML — the configuration file’s format — is expanded as an initialism in the cited pages, so this page writes them as those pages write them.
Selecting what runs
Two commands share the same flags and are worth using as a pair: sf code-analyzer rules lists the rules a selection returns, and sf code-analyzer run runs that same selection. Tuning with the first costs seconds; tuning with the second costs a full pass.
The flag that decides is --rule-selector. Its default value is Recommended, a tag: it selects the rules of every engine that carry it. Here is the trap that disorients most: the moment you write a selector of your own, that default disappears. --rule-selector eslint is equivalent to eslint:all and runs every ESLint rule, not the recommended ones; to stay inside the recommended set you have to write eslint:Recommended.
Criteria combine: colons act as “and”, commas inside parentheses as “or”, and repeating the flag adds with “and” again. Order and case do not matter, but a value with parentheses is quoted whole.
The workspace is the set of files that enter the analysis, and by default it is the current folder. Declaring it with --workspace has a less obvious effect: the command deduces from the extensions present which rules apply, so a workspace without TypeScript returns no TypeScript rules. --target then narrows which files are analysed inside it.
Severity, and what makes a run fail
Every rule carries a severity from 1 to 5: 1 Critical, 2 High, 3 Moderate, 4 Low and 5 Info. Severity is used to select, and also to decide when a run counts as failed.
That last part is a separate flag, --severity-threshold, which only the run command has: with it, the command exits with a non-zero exit code when a finding meets or exceeds the threshold. The documentation recommends calling sf code-analyzer run from the scripts of the continuous integration and continuous delivery (CI/CD) system, and adds that many of those tools require a threshold.
This page reads that as an asymmetry worth saying out loud, because no source writes it: selecting and failing are independent decisions. A pipeline can run the right rules, find critical findings and exit zero, only because nobody set the threshold.
The configuration file and where it lives
All customisation fits in one YAML file. Salesforce recommends calling it code-analyzer.yml or code-analyzer.yaml and putting it in the root of the workspace: both the editor extension and every command-line command look for either of the two in the current folder and apply what they find. --config-file points at another path and works on every command. To get started, sf code-analyzer config writes the current state, commented.
Inside are five blocks covering different things.
| Block | What it controls |
|---|---|
| Top-level keys | config_root, the folder relative paths resolve against, and logging through log_folder and log_level, of five levels and Debug by default |
rules |
Reassigns a rule’s severity or tags, or disables it entirely: it turns somebody else’s severity into the team’s |
engines |
Tunes each engine and accepts rules of your own: a PMD ruleset file, a regular expression, the project’s own ESLint file |
ignores |
Excludes files by pattern, and takes precedence: what is both in the target and here is not analysed |
suppressions |
The baseline for inherited findings, below |
From the editor, the terminal and the pipeline
The three surfaces are not three ways of doing the same thing.
| Where it runs | What it gives | What it does not do |
|---|---|---|
| In the editor | Analyses on open or on save; marks each finding in the problems tab; offers a fix for some PMD rules | Blocks nothing: by default everything comes out as a warning |
| In the terminal | The full commands, with rule selection, threshold and a results file | Says nothing while somebody is typing |
| In the pipeline | The exit code and a results file kept as an artifact | Does not exist unless somebody writes it into the script |
The severity mapping in the editor is worth a minute: setting severities 1 and 2 to error is what makes a severe finding visible. In the terminal, --output-file writes the results and the extension decides the format: HTML, CSV (comma-separated values), JSON or XML.
What a team does with inherited findings
A first pass over an old org returns more findings than anybody is going to fix this week. The documented answer is not to lower the threshold, it is to declare a baseline.
suppressions suppresses by file or by folder, with a rule selector, and accepts two keys that are the difference between a baseline and a rug: reason, the explanation of why something is suppressed, and max_suppressed_violations, the maximum number of findings that may be suppressed there. The second is the one that matters. Once the cap is exhausted, further findings are reported normally, and the source says what that is for: “This prevents technical debt from growing beyond the specified limit”. The old code is tolerated; the new code is not.
You can also suppress inside the code, with the Code-analyzer-suppress and Code-analyzer-unsuppress markers, and there is a detail that gets missed there: a suppression begins at its line and reaches the end of the file until somebody lifts it. You cannot suppress by tag, only by rule, by engine or by severity.
When to suppress and when to fix
The criterion this page proposes joins two things the sources state separately: the four suppression guidelines and the cap. A suppression is acceptable when it is bounded, documented and capped; it stops being acceptable when any of the three is missing.
| Finding | Suppress it when | Fix it when |
|---|---|---|
| False positive | The pattern is correct and the reason is written down | Nobody can explain why it is correct |
| Old debt | It sits in a closed folder with a declared cap | It sits in code that is going to be touched anyway |
| An annoying rule | The team disputes its severity, not its existence | The dispute is whether the rule applies: that is a configuration change |
A counterpoint from the source itself: for a package’s AppExchange security review, Salesforce says the scans do not have to be 100% passing, but to have been run, to have had what is fixable fixed, and to have been run again. That belongs to that process and is not a general rule, but it describes well the state an honest team reaches.
Example: the first pass at a private bank
A private bank inherits an org with years of Apex and commissions a first pass before any new branch is opened. The order of the steps is this page’s proposal, not a sequence Salesforce prescribes.
- Inventory.
sf code-analyzer rules --workspace ., to see which rules actually apply to the files that exist before running anything. - Snapshot.
sf code-analyzer runwith no threshold and with--output-file, to read the HTML with somebody who knows the code. - Reading.
ApexCRUDViolationshows up, the PMD rule whose message asks you to check permissions before a SOQL (Salesforce Object Query Language) or DML (Data Manipulation Language) operation. At a financial institution that is not style: it is the permission model. - Baseline.
suppressionsper folder, withreasonand withmax_suppressed_violationsequal to what exists today. The old is tolerated and counted; the next finding is reported again. - Gate.
--severity-threshold 2in the pipeline script, and severities 1 and 2 as errors in the editor, so the developer sees it before the pipeline does.
The result is not a clean org, but an org whose new code cannot make it worse without somebody seeing it.
Before production
- Keep
code-analyzer.ymlin the root of the workspace and under version control, so editor, terminal and pipeline apply the same configuration. - Check the dependencies wherever it will run: JDK 11 or later for PMD, CPD and Salesforce Graph Engine; Python 3.10 or later if Flow Scanner is used; and disable the engines that will not be installed.
- Write the selector with an explicit
:Recommendedif the recommended set is what you want, and check the list withsf code-analyzer rules. - Agree the value of
--severity-thresholdand put it in the script, not in somebody’s memory. - Declare
reasonandmax_suppressed_violationson every inherited suppression, and check thatignoresis not covering folders that do get deployed. - Map severities 1 and 2 to error in the editor, because by default all five levels come out as warnings.
- Keep each run’s results file, so two passes can be compared.
- Pin the plugin version the pipeline uses: Code Analyzer does not follow Salesforce’s quarterly release, it publishes a new one at the end of each month, and its current major version is v5 since v4 was retired.
What gets installed and how the environment connects to an org belongs to Development environment; the frameworks PMD and ESLint analyse, to Visualforce, Aura and LWC.
Official sources
- Salesforce Code Analyzer — Salesforce Code Analyzer (opens in a new tab)
- Use CLI Commands to Analyze Your Code — Salesforce Code Analyzer (opens in a new tab)
- Engines — Salesforce Code Analyzer (opens in a new tab)
- PMD — Salesforce Code Analyzer (opens in a new tab)
- ESLint — Salesforce Code Analyzer (opens in a new tab)
- Customize the Configuration — Salesforce Code Analyzer (opens in a new tab)
- Top-Level Customization Reference — Salesforce Code Analyzer (opens in a new tab)
- Suppress Violations — Salesforce Code Analyzer (opens in a new tab)
- CI/CD Integration with Code Analyzer — Salesforce Code Analyzer (opens in a new tab)
- Use the VS Code Extension to Analyze Your Code — Salesforce Code Analyzer (opens in a new tab)
- Output Schemas Reference — Salesforce Code Analyzer (opens in a new tab)
- Produce Code Analyzer Reports for AppExchange Security Review — Salesforce Code Analyzer (opens in a new tab)
- Release Notes — Salesforce Code Analyzer (opens in a new tab)