Sub topic 3
Installing the plugin
This chapter installs the Code Analyzer plugin for the Salesforce CLI and checks that it works. It assumes you can use sf from a terminal and have a Salesforce DX project. Later chapters cover running scans.
What you need first
Code Analyzer is a plugin for sf, so the CLI comes first. The plugin also needs a few runtimes, depending on which engines you use.
| Requirement | Needed for | Check with |
|---|---|---|
| Node.js 20 or later | The Salesforce CLI and the plugin | `node --version` |
| Java 11 or later | The PMD and CPD engines | `java -version` |
| Python 3.10 or later | The Flow engine | `python3 --version` |
The ESLint, RetireJS, and Regex engines run on Node.js alone. If Java is missing, scans that use PMD or CPD fail with an error. The other engines can still run.
Install the CLI and the plugin
Install the Salesforce CLI with npm, then add the plugin. Then list the rules in the Recommended set to confirm that the plugin loads.
npm install --global @salesforce/cli
sf plugins install code-analyzer
sf code-analyzer rules --rule-selector RecommendedThe first command can take a minute. The second downloads the plugin and its engine packages. The third prints a table of rules, with the engine, name, severity, and tags of each one.
The diagram shows the order of the steps.
Verify the install
Run sf plugins to see the installed plugins. code-analyzer should appear in the list with a version number. Run sf code-analyzer --help to see the available commands, such as run and rules.
sf plugins
sf code-analyzer --helpTo update later, run sf plugins update. This updates every installed plugin, including Code Analyzer.
Common errors
sf: command not found. The CLI is not installed, or the npm global bin folder is not on yourPATH. Open a new terminal and check again.- Permission errors during
npm install --global. Don't usesudo. Point npm at a user-owned prefix, or use a Node version manager. - A Java error when listing or running rules. Install Java 11 or later and make sure
java -versionworks in the same terminal. - Confusing the two plugins. The older
sf scannercommands come from a different plugin,@salesforce/sfdx-scanner. The new commands start withsf code-analyzer, so check which prefix you are typing.
Exercise
You run sf code-analyzer rules --rule-selector Recommended and it fails with a Java error, but sf plugins lists code-analyzer. What is the likely cause, and how do you fix it?
Show answer
The plugin is installed, but Java 11 or later is missing or not on the PATH. The PMD and CPD engines need it to list rules. Install Java 11 or later, confirm it with java -version in the same terminal, and run the command again.