Sub topic 8
Table, detail, and file outputs
This chapter assumes you can already run sf code-analyzer run against a workspace, as covered in the earlier chapters. It shows how to control what you see in the terminal and what gets saved to disk.
Two views for the terminal
The --view flag controls how violations print to the terminal. It accepts two values.
tableshows one row per violation, with columns such as severity, rule, engine, and file location. This is the default. It suits quick scans of a small result set.detailshows each violation as its own block, including the full message and a link to the rule's documentation where one exists. It suits reading through findings one at a time.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view table
sf code-analyzer run --workspace force-app --rule-selector Recommended --view detailBoth commands scan the same code and find the same violations. Only the layout differs.
Writing results to files
The --output-file flag saves the results to a file. Code Analyzer picks the format from the file extension.
| Extension | Typical use |
|---|---|
| `.html` | A report you open in a browser and share with teammates |
| `.json` | Scripts and custom tooling |
| `.csv` | Spreadsheets and quick sorting by rule or file |
| `.xml` | Tools that expect XML input |
| `.sarif` | Code scanning dashboards and CI artifacts |
This example limits the scan to one file and to PMD rules tagged Security, then writes an HTML report:
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountUpdater.cls --rule-selector pmd:Security --output-file results.htmlYou can repeat --output-file to write several formats from a single scan. This avoids running the analysis twice.
sf code-analyzer run --workspace force-app --rule-selector Recommended --output-file results.html --output-file results.json --output-file results.sarifChoosing a format
Match the output to the reader.
- For your own debugging, use the
detailview in the terminal. - For a report that a reviewer or manager will open, use HTML.
- For automation that parses results, use JSON or CSV.
- For CI pipelines and code scanning tools, use SARIF, a standard format for static analysis results.
Common mistakes
- Unrecognized extension. If the file name ends in an extension Code Analyzer does not support, the command errors instead of guessing. Use one of the extensions in the table.
- Committing reports. Generated files like
results.htmlare build output. Add them to.gitignoreso they do not end up in your repository. - Forgetting the output in CI. A failed job often deletes its workspace. Upload the report as an artifact, using a step that runs even after a failure, so you can read what went wrong.
Exercise
You want a single scan of force-app with the Recommended rules to print each violation in full in the terminal and also save a CSV file and a SARIF file. Write the command.
Show answer
sf code-analyzer run --workspace force-app --rule-selector Recommended --view detail --output-file results.csv --output-file results.sarif