Sub topic 6
Scoping a scan with workspace and target
This chapter assumes you have installed the Code Analyzer plugin and can run sf code-analyzer run with a rule selector. You should also know the layout of a Salesforce DX project, with force-app and sfdx-project.json.
Two flags, two jobs
A scan has two scoping flags that are easy to confuse.
--workspace(short form-w) names the whole body of code the engines can see. Engines use it to resolve references between files, such as a class that calls another class.--target(short form-t) names the files whose violations you want reported. It must be inside the workspace.
If you leave out --target, Code Analyzer reports on everything in the workspace. If you leave out --workspace, it uses the current directory.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableThis scans every file under force-app and reports on all of them.
Narrowing the report with target
To check one file, keep the full workspace and add a target.
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountUpdater.cls --rule-selector pmd:Security --output-file results.htmlThe engines still read all of force-app, so they have the context they need. Only findings in AccountUpdater.cls appear in results.html.
Both flags accept more than a single path. You can repeat a flag, and you can use glob patterns, which are wildcard expressions for matching file names.
sf code-analyzer run \
--workspace force-app \
--target "force-app/**/classes/*.cls" \
--target "force-app/**/lwc/**/*.js" \
--rule-selector RecommendedQuote glob patterns. Without quotes, your shell may expand them before the CLI sees them, and the result differs between shells.
Why the split matters
Narrowing only the workspace changes what the engines can see, and not only what you see in the output. If you point --workspace at one class, an engine that needs a second class to follow a call cannot find it. The scan may then miss violations or skip rules that depend on that wider view. The graph engine, covered later in this tutorial, depends heavily on this. A good default is to set the workspace to the whole package directory and use --target to narrow the report.
A common use is a pull request check. You pass only the files changed in the branch as targets, and the workspace stays the full project.
sf code-analyzer run \
--workspace force-app \
--target force-app/main/default/classes/AccountUpdater.cls \
--rule-selector Recommended \
--view tableCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a target is not in the workspace | The target path lies outside every workspace path | Add its parent folder to `--workspace` |
| No violations for a file you expect to fail | The file extension does not match any selected rule's engine | Check the selector with `sf code-analyzer rules` |
| Different files scanned in different shells | An unquoted glob was expanded by the shell | Put the pattern in quotes |
| Missing cross-file findings | Workspace set to a single file or small folder | Widen the workspace and narrow with `--target` |
Exercise
You want a report on only force-app/main/default/classes/AccountUpdater.cls, but its rules need to read other classes in the project. What do you set for --workspace and --target?
Show answer
Set --workspace force-app so the engines can read the whole project, and set --target force-app/main/default/classes/AccountUpdater.cls so only that file's violations are reported.
Exercise
Why should you quote a pattern such as force-app/**/*.cls when you pass it to --target?
Show answer
Your shell can expand an unquoted glob before the CLI receives it, and different shells expand it differently. Quoting passes the pattern to Code Analyzer unchanged.