Sub topic 10
Configuring with code-analyzer.yml
Command-line flags are fine for one-off scans. They get unwieldy once a team wants the same ignores, severities, and engine settings on every laptop and in CI. The code-analyzer.yml file holds those settings in one place that you can commit to version control.
This chapter assumes you can run sf code-analyzer run with a workspace and rule selector, and that you can read basic YAML. It also assumes you know the severity scale from the earlier chapter: 1 Critical, 2 High, 3 Moderate, 4 Low, 5 Info.
What the file controls
The configuration file is a YAML document with a small set of top-level fields.
| Field | Purpose |
|---|---|
| `config_root` | Folder that relative paths in the file are resolved against |
| `log_folder` | Where Code Analyzer writes its log files |
| `log_level` | How much detail goes into the logs |
| `rules` | Per-rule overrides such as severity and tags, grouped by engine |
| `engines` | Per-engine settings, including turning an engine off |
| `ignores` | Glob patterns for files Code Analyzer should skip |
Every field is optional. A file with only an ignores section is valid, and anything you leave out keeps its default.
How the file is found
Code Analyzer looks for a file named code-analyzer.yml or code-analyzer.yaml in the folder you run the command from. If it finds one, it uses it without being told. To use a file somewhere else, or one with a different name, pass --config-file.
Because discovery depends on the current folder, run scans from the project root. A scan started from force-app/ will not pick up the file in the root. Passing --config-file explicitly avoids this problem.
A first configuration
Here is the file from the plan: it 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 RecommendedThree things changed compared with a run with no file:
- Files matching either glob are never analyzed, so they cannot produce violations.
AvoidDebugStatements, a PMD rule, now reports at severity 5 (Info) instead of its default.- Everything else behaves as before. The rule selector still decides which rules run.
Put quotes around glob patterns. A bare **/node_modules/** starts with *, which YAML reads as an alias marker, so an unquoted pattern is a parse error.
The rules section
Under rules, the first level of keys is the engine name, such as pmd, eslint, regex, or retire-js. The second level is the rule name. Each rule can then override severity or tags.
rules:
pmd:
AvoidDebugStatements:
severity: "Info"
ApexCRUDViolation:
severity: 2
tags: ["Recommended", "Security", "TeamPriority"]
eslint:
no-console:
severity: "Low"Severity accepts either the number or the name. Be consistent within a file so reviewers don't have to translate between the two.
Overrides interact with rule selectors. The selector chooses which rules run, and the override changes how they report. If you add a custom tag to a rule, such as TeamPriority above, you can select it with --rule-selector TeamPriority. That makes tags a lightweight way to define your own rule groups. A later chapter covers overrides in more detail.
The ignores section
ignores.files takes a list of glob patterns. Use it for code you don't own or can't change: vendored libraries, minified static resources, and generated output.
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
- "**/*.min.js"
- "force-app/main/default/classes/generated/**"Ignoring is coarse. It skips whole files for every rule. If only one line in a file needs an exception, use an inline suppression instead (covered in the triage chapter). Excluding a file removes all current and future findings in it, so keep the list short and review it from time to time.
The engines section
The engines section holds settings that apply to an engine as a whole. The most common use is switching an engine off:
engines:
eslint:
disable_engine: trueA disabled engine contributes no rules, even if your selector would have matched them. This is useful when a project has no JavaScript, or when a team already runs ESLint in its own pipeline and does not want duplicate findings. Engines also have their own settings, such as pointing at a custom ESLint config. The available settings differ by engine, so check the real list instead of guessing.
Inspecting the effective configuration
Defaults, your file, and flags all combine, so it helps to see the result. The config command prints the configuration Code Analyzer would use, with defaults filled in and comments describing each field:
sf code-analyzer config --workspace force-app --rule-selector RecommendedUse it to confirm that your file was found and parsed, and to discover field names. It can also write out a fully documented starting file with --output-file code-analyzer.yml. Editing a generated file is safer than typing field names from memory.
Precedence and common mistakes
When a flag and the file both speak to the same thing, the flag wins for that run. Rule selection is the clearest case. The file never picks rules for you. The --rule-selector flag does. If a scan returns nothing, check the selector before blaming the config.
| Symptom | Likely cause | Fix |
|---|---|---|
| Settings seem ignored | The scan ran from a folder with no config file | Run from the project root or pass `--config-file` |
| Parse error on startup | Unquoted glob beginning with `*`, or bad indentation | Quote patterns and use spaces, not tabs |
| Override has no effect | Wrong engine key or misspelled rule name | Compare against `sf code-analyzer rules` output |
| Ignored folder still scanned | Pattern doesn't match the path as resolved | Check `config_root` and use `**/` prefixes |
| Rule you lowered still fails CI | Threshold compares against the overridden severity | Re-check the number after the override |
The last row matters for CI. The severity threshold looks at the severity after overrides. Lowering a rule to Info (5) removes it from a --severity-threshold 2 gate. That is a legitimate way to tune a gate, but it is also a quiet way to hide real problems. Review changes to this file in pull requests the way you review code.
Keeping the file maintainable
- Keep one file at the project root and commit it, so local runs and CI behave the same.
- Comment every override with the reason and, if possible, a ticket reference.
- Prefer a tag-based workflow over many per-rule overrides when a group of rules shares a purpose.
- Re-run
sf code-analyzer configafter upgrading the plugin. New engine fields and rules can change the effective configuration.
Exercise
Your team's scan reports dozens of findings from the staticresources folder, which contains minified vendor libraries. Separately, you want PMD's AvoidDebugStatements to stop appearing in a --severity-threshold 3 CI gate. Write the code-analyzer.yml that does both, and explain why running sf code-analyzer run --workspace force-app --rule-selector Recommended from inside force-app/ might not apply it.
Show answer
The file is: ignores: files: - "/staticresources/" rules: pmd: AvoidDebugStatements: severity: "Info" The ignores entry skips the vendor files entirely. The override sets the rule to severity 5, which is less severe than 3, so it no longer trips the gate. Code Analyzer auto-discovers the file only in the folder you run the command from. Started inside force-app/, it would not find the root-level file and would use defaults. Run from the project root or pass --config-file with the path.