Sub topic 12
Ignoring files and paths
This chapter assumes you can run sf code-analyzer run and that you have a code-analyzer.yml file or know how to create one. Here you will keep scans focused on code your team owns.
Why ignore files
A scan reports on every file in the workspace. Some of those files are not yours to fix. Third-party libraries in staticresources, minified bundles, and node_modules produce many findings that nobody will act on. Those findings bury the real ones and can fail a CI job for no good reason.
Ignoring a file tells Code Analyzer to leave it out of the results. This differs from lowering a rule's severity, which changes how one rule is treated everywhere.
The ignores section
Put ignore patterns under ignores.files in code-analyzer.yml. Each entry is a glob pattern.
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
- "**/*.min.js"Run a scan with the file:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector RecommendedFiles that match any pattern are skipped for every engine.
Writing glob patterns
Glob patterns use a few wildcards.
| Pattern | Meaning |
|---|---|
| `*` | Any characters within one folder level |
| `**` | Any number of folder levels |
| `*.min.js` | Any file whose name ends in `.min.js` |
| `**/staticresources/**` | Anything under a folder named `staticresources`, at any depth |
Quote each pattern in YAML. An unquoted value that starts with * is read as a YAML alias and causes a parse error.
Ignoring versus targeting
Ignoring and targeting are different tools.
- Use
--targetfor a one-off scan of specific files, for example the file you are editing. - Use
ignores.filesfor a permanent exclusion that every run should respect, locally and in CI.
Because the exclusion lives in the config file, your team and your pipeline share one definition of what is in scope.
Common errors
- Patterns that match too much.
**/*.jsskips every JavaScript file, including your Lightning Web Components. Name the folder you mean to exclude. - Patterns that match nothing. A path written relative to the wrong folder never matches. Start with
**/unless you are sure of the root. - Forgetting the config file. The
ignoressection only applies when Code Analyzer loads the file. Pass--config-file, or keepcode-analyzer.ymlin the folder where you run the command. - Hiding your own code. Ignoring a whole folder removes its real violations too. For one false positive, use an inline suppression instead.
Checking your patterns
After you add a pattern, run the scan and look at the results. Findings from the excluded folder should be gone. If they remain, check the pattern against the file path shown in the results table. Compare the two character by character, because a typo such as staticresource instead of staticresources is a common cause.
Exercise
Your scan reports hundreds of violations in force-app/main/default/staticresources/jquery.min.js. Write the code-analyzer.yml section that excludes every static resource, then name one risk of this approach.
Show answer
Use ignores: with files: containing "**/staticresources/**". The risk is that the pattern also hides violations in any static resource your team wrote yourselves, so exclude only the folders or files you do not own if that matters.
Exercise
Why must a pattern such as **/*.min.js be wrapped in quotes in code-analyzer.yml?
Show answer
In YAML, an unquoted value that starts with * is parsed as an alias, which causes a parse error. Quotes make it a plain string.