Sub topic 10
Configuring with code-analyzer.yml
This chapter assumes you can run sf code-analyzer run and read its results, as covered in the earlier chapters. You should also be comfortable reading YAML.
Why use a configuration file
Command-line flags are fine for a quick scan. A team usually wants the same settings on every laptop and in CI. A code-analyzer.yml file at the project root holds those settings, and you commit it to version control.
If the file is named code-analyzer.yml or code-analyzer.yaml and sits in the directory where you run the command, Code Analyzer reads it automatically. Use --config-file to point at a file elsewhere or with another name.
A first configuration
The file below skips third-party folders and lowers the severity of one PMD rule.
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
rules:
pmd:
AvoidDebugStatements:
severity: "Info"Run a scan with it:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector RecommendedThe main sections
| Section | Purpose |
|---|---|
| `ignores` | Glob patterns for files that no engine should analyze |
| `rules` | Per-rule overrides, grouped by engine name, then rule name |
| `engines` | Settings for a specific engine, such as turning it off |
| `log_folder` and `log_level` | Where logs go and how detailed they are |
Rule severity accepts the names Critical, High, Moderate, Low, and Info, or the numbers 1 to 5. Severity 1 is the most serious. The same numbers drive --severity-threshold, so a rule you downgrade here also changes whether a CI job fails.
The rules block is nested three levels deep: engine, rule, property. In the example, pmd is the engine, AvoidDebugStatements is the rule, and severity is the property. You can also override tags in the same place, which the next chapter covers.
Common errors
- Wrong indentation. YAML uses spaces, not tabs. A misplaced key can nest a rule under the wrong engine, and the override then has no effect.
- Misspelled engine or rule names. Names are case-sensitive and must match the output of
sf code-analyzer rules. Copy them from that output. - Running from the wrong directory. The automatic lookup uses the current directory. If a script runs from a subfolder, the file is not found, so pass
--config-fileexplicitly. - Over-broad ignore patterns. A pattern like
**/*.clswould silently hide every Apex class. Keep ignores narrow and review them when you add them.
To check that your file is being read, run sf code-analyzer config. It prints the configuration that Code Analyzer will use, with your values merged over the defaults. If an override does not appear there, the problem is in the YAML, not in the scan.
Exercise
Your team's code-analyzer.yml lowers AvoidDebugStatements to "Info", but a CI scan still reports it at its original severity. Name two likely causes.
Show answer
First, the file may not be loaded. It could be in a different directory from the one the job runs in, so --config-file is needed. Second, the override may be nested wrongly or misspelled, for example under the wrong engine or with a different rule name. Running sf code-analyzer config shows the merged configuration and tells you which cause applies.