Sub topic 8
Table, detail, and file outputs
This chapter assumes you can already run sf code-analyzer run against a workspace and read a severity level. It covers the three ways to see the same results: the table view, the detail view, and report files. It also covers how to choose between them.
One scan, several outputs
Every scan produces one set of violations. The output options only change how you look at that set. They never change which rules run or what they find.
--viewcontrols what the terminal shows. The options aretableanddetail.--output-file(short form-f) writes the violations to a file. Its extension picks the format.
You can use both in the same command. The terminal view stays readable for you, and the file feeds other tools.
The table view
The table view is the default. It prints a compact summary, then a table with one row per violation. Each row shows the location, the rule, the severity, and the message.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableUse it when you want a quick answer to "is anything wrong?" It works well for small and medium result sets. With hundreds of violations, long messages and file paths wrap and the table gets hard to scan.
The table also tends to be grouped or ordered so that the most severe findings are easy to spot. Even so, do not rely on terminal order for decisions. If you need a guaranteed ordering, sort a CSV or JSON file instead.
The detail view
The detail view prints each violation as its own block of labeled fields. You get the full message, the engine, the rule name, the severity, and the file with line and column. It may also include the rule's tags and a link to its documentation.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view detailUse it when you are working through findings one at a time. For example, suppose the table shows ApexCRUDViolation and you want to know what the rule is asking for. The detail block gives you the whole message and the documentation link without making you wrap a wide table in your head.
| Situation | Better view |
|---|---|
| Quick check before a commit | table |
| Many violations across many files | table, or a file |
| Reading one violation closely | detail |
| Pasting a finding into a ticket | detail |
Writing results to a file
Add --output-file and give it a path. Code Analyzer reads the extension and picks the format.
| Extension | Format | Typical use |
|---|---|---|
| `.html` | Interactive report | Sharing with people who will not use a terminal |
| `.csv` | Spreadsheet rows | Sorting, filtering, and counting in a spreadsheet |
| `.json` | Structured data | Custom scripts and dashboards |
| `.xml` | XML report | Tools that expect XML |
| `.sarif` | SARIF | Code scanning tools such as GitHub code scanning |
Here is a scan that limits itself to one file and PMD security rules, 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.htmlThe HTML report lets you sort and filter findings in a browser. It is the easiest thing to attach to a review or send to a teammate.
Use CSV when you want to answer questions like "which rule fires most often?" Open it in a spreadsheet and pivot on the rule column. Use JSON when you want to answer the same question with a script.
sf code-analyzer run --workspace force-app --rule-selector Recommended --output-file results.jsonA short Node script can then count violations per rule. This sketch assumes the JSON report contains a violations array whose entries have a rule field. Check your own file first, because the schema can change between plugin versions.
const fs = require('fs');
const report = JSON.parse(fs.readFileSync('results.json', 'utf8'));
const counts = {};
for (const violation of report.violations) {
counts[violation.rule] = (counts[violation.rule] || 0) + 1;
}
console.log(counts);Writing more than one file
You can repeat --output-file to write several formats from a single scan. This avoids running the scan twice, which matters because scanning is the slow part.
sf code-analyzer run --workspace force-app --rule-selector Recommended --output-file results.html --output-file results.csv --output-file results.sarifAll three files describe the same violations from the same run. If you scanned twice, a code change between scans could make the files disagree.
SARIF and CI
SARIF (Static Analysis Results Interchange Format) is a JSON-based standard for static analysis results. Code Analyzer writes it when the file ends in .sarif. Tools that understand SARIF can show findings inline on pull requests, so reviewers see the problem next to the changed line.
The CI workflow you will build later in this tutorial uses SARIF for this reason. It writes results.sarif and uploads it as an artifact:
sf code-analyzer run --workspace force-app --rule-selector Recommended --severity-threshold 2 --output-file results.sarifNote that the report and the exit status are independent. The report is written whether or not the threshold is crossed. That is why the workflow's upload step uses if: always(): the file is still available to inspect after the job fails.
Common mistakes
- Expecting the view to change the file.
--view detailaffects only the terminal. A.csvfile has the same columns whichever view you chose. - Using an unrecognized extension. If the extension is not one the plugin supports, the command reports an error instead of guessing a format. Use one of the extensions in the table above.
- Committing reports.
results.htmlandresults.sarifchange on every run and can be large. Add them to.gitignoreunless you have a reason to track them. - Parsing the table view in a script. Terminal output is formatted for humans and can change between releases. Read JSON or CSV files instead.
- Treating an empty report as a pass. A scan that matches no files or no rules also produces few or no violations. If you expected findings, confirm your
--workspace,--target, and--rule-selectorvalues.
Choosing an output
- Start with the default table view while you are exploring.
- Switch to
--view detailwhen you need to understand a particular finding. - Add
--output-filewith.htmlwhen you need to share results with people. - Use
.csvor.jsonwhen you want to analyze results in bulk. - Use
.sarifwhen a code scanning tool or CI system will consume the results.
Exercise
Your team's CI job must show findings on pull requests, and a manager also wants a spreadsheet of the same scan to count violations per rule. How do you produce both without scanning twice, and which extensions do you use?
Show answer
Run one scan and pass --output-file twice, once with a .sarif file for the pull request tooling and once with a .csv file for the spreadsheet, for example sf code-analyzer run --workspace force-app --rule-selector Recommended --output-file results.sarif --output-file results.csv. Both files come from the same run, so they describe the same violations.
Exercise
You ran a scan with --view detail --output-file results.csv. Does the CSV contain the detail-style fields because you chose the detail view?
Show answer
No. --view only changes what the terminal shows. The CSV format is chosen by the .csv extension and has the same columns whichever view you picked.