Sub topic 6
Scoping a scan with workspace and target
This chapter assumes you can run sf code-analyzer run on a DX project, know what a rule selector is, and have a project with a force-app folder. Here you will learn how the --workspace and --target flags decide which files a scan reads and which files it reports on. You will also see the mistakes that make a scan look empty or slow.
Two flags, two jobs
A scan has to answer two separate questions: which files may the engines look at, and which files do you want results for? Code Analyzer answers them with two flags.
--workspace(short form-w) defines the set of files and folders that make up your project. Engines use it as context. A rule can follow a reference from one file into another only if both files are in the workspace.--target(short form-t) narrows the scan to the files you want violations for. Every target must sit inside the workspace.
If you leave out --workspace, Code Analyzer uses the current directory. If you leave out --target, every file in the workspace is a target. For a first scan, --workspace force-app is usually all you need.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableWhy the split matters
You could simply point the scan at one file. The workspace exists because some engines need surrounding code to judge the target correctly. The graph engine (covered later in this tutorial) follows method calls between classes, so a target class that calls a helper in another file needs that helper in the workspace. PMD works on one file at a time, but it still takes its file list from the workspace.
This gives you a useful pattern. Keep the workspace wide and the target narrow. You get fast, focused results without cutting the engines off from the code they depend on.
Giving multiple values
Both flags accept several values. You can repeat the flag, or list values after it separated by spaces.
sf code-analyzer run \
--workspace force-app \
--workspace sfdx-project.json \
--target force-app/main/default/classes \
--target force-app/main/default/lwc \
--rule-selector RecommendedEach value can be a file or a folder. A folder includes everything beneath it, subject to the ignore rules in your configuration file (covered in a later chapter).
Glob patterns
Values can also be glob patterns, which match many paths at once. Quote globs so that your shell does not expand them before Code Analyzer sees them. Unquoted globs behave differently from shell to shell, which makes scripts hard to reproduce.
sf code-analyzer run \
--workspace force-app \
--target "force-app/**/*.cls" \
--rule-selector pmd:SecurityThis reports only on Apex classes. LWC JavaScript, Flows, and other files remain in the workspace but produce no violations.
Targeting a single file
To check a file you just edited, give its path as the target. The workspace stays the same.
sf code-analyzer run \
--workspace force-app \
--target force-app/main/default/classes/AccountUpdater.cls \
--rule-selector Recommended \
--view detailThe detail view prints each violation with its rule, severity, location, and message. That layout suits a short list better than a wide table.
Scanning only changed files
Targets are a good fit for pull request checks. You can feed the list of changed files from Git into --target, so a CI job reports only on what the developer touched.
CHANGED=$(git diff --name-only origin/main...HEAD -- 'force-app/**/*.cls' 'force-app/**/*.trigger')
if [ -z "$CHANGED" ]; then
echo "No Apex changes to scan."
exit 0
fi
ARGS=()
for f in $CHANGED; do
ARGS+=(--target "$f")
done
sf code-analyzer run --workspace force-app --rule-selector Recommended "${ARGS[@]}"This script assumes file names contain no spaces, which holds for standard Salesforce metadata. It also skips deleted files poorly: git diff lists a file that was removed, and a target that does not exist can make the run fail. Add --diff-filter=d to the git diff command to leave deleted files out.
How the flags interact with the rule selector
The rule selector picks which rules run. The target picks which files they report on. A violation appears only when both agree: the rule is selected and the file is a target. A rule that applies to a file type absent from your targets simply produces nothing. For example, eslint:Recommended against a target list of only .cls files returns no ESLint results.
That is the most common reason for an empty result. Before you assume the code is clean, check three things:
- The target paths exist and are inside the workspace.
- The selected engines have rules for the file types in your targets.
- Your
code-analyzer.ymldoes not ignore those paths.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a target is not within the workspace | The target path is outside every workspace value | Add a parent folder to `--workspace`, or correct the path |
| Zero violations on a project you know has problems | Targets exclude the relevant file types, or the paths are ignored in the config | Widen the target, and check the `ignores` section |
| Scan is very slow | The workspace is the project root, so `node_modules` or build output is included | Point the workspace at `force-app`, or ignore those folders |
| Glob matches nothing | The shell expanded or altered the pattern | Wrap the glob in quotes |
| Different results from the same command on two machines | Relative paths resolved from different working directories | Run from the project root, or use consistent paths |
Choosing a workspace size
A larger workspace gives engines more context but costs time. Use these rules of thumb.
- Use
force-app(or your package directories) for routine scans. This keeps generated and third-party folders out. - Use the whole project root only when you need rules that read
sfdx-project.jsonor other root-level files. - Keep dependencies out of the workspace unless you want RetireJS to report on them. That engine looks for known vulnerable JavaScript libraries, so it needs those files, and the extra time is the price.
Putting it together
A practical set of habits for everyday work:
- Locally, scan the file you are editing with a narrow target and the
detailview. - Before a commit, scan the changed files with the full workspace.
- In scheduled CI jobs, scan the full workspace with no target so the whole project is covered.
- In pull request CI, scan only the changed files to keep feedback fast, and let the scheduled job catch anything the narrow scan misses.
Exercise
You run sf code-analyzer run --workspace force-app --target "force-app/**/*.cls" --rule-selector eslint:Recommended and get no violations, although your LWC JavaScript has known problems. Why, and what is the smallest change that fixes it?
Show answer
The target is limited to .cls files, so ESLint (which analyzes JavaScript) has no target files to report on. The LWC files are in the workspace, but only targets produce violations. Change the target to include the LWC files, for example --target "force-app/**/lwc/**/*.js", or remove --target so the whole workspace is scanned.
Exercise
A teammate sets --workspace force-app/main/default/classes/AccountUpdater.cls and --target force-app/main/default/classes/AccountUpdater.cls for every scan to save time. What do they lose when they scan a class that calls helpers in other files?
Show answer
The workspace holds only that one file, so engines that analyze across files, such as the graph engine, cannot see the helper classes. They can miss problems that depend on code in other files. A wider workspace such as force-app with the single file as target keeps the speed of a focused report and gives the engines full context.