Sub topic 12
Ignoring files and paths
This chapter assumes you can run sf code-analyzer run against a Salesforce DX project and read its table output. It also assumes you have a code-analyzer.yml file, or know how to create one. The earlier chapters on running scans and on configuration cover both.
Why ignore files at all
A scan reports on every file the engines can reach in your workspace. Some of those files are not yours to fix:
- Third-party libraries in
node_modulesorstaticresources. - Minified or generated JavaScript.
- Test fixtures that contain deliberately bad code.
- Build output and vendored code.
Findings in these files add noise. They bury real problems and can make a severity threshold fail a CI job for reasons nobody on the team can address.
Code Analyzer gives you three ways to cut this noise. They work at different scopes:
| Scope | Mechanism | Use it when |
|---|---|---|
| Whole run | `--workspace` and `--target` flags | You want to scan only part of the project this time |
| Whole project, every run | `ignores.files` in `code-analyzer.yml` | A path should never be scanned |
| One line or block | Inline suppression comment | A single finding is a justified false positive |
This chapter covers the second row in depth and shows how it relates to the other two.
Flags versus configuration
The --workspace and --target flags decide which files a single run looks at. They are temporary. A teammate who runs a different command can still scan the files you left out.
The ignores section of code-analyzer.yml is part of the project. It travels with the repository and applies on every machine and in CI. Use flags for ad hoc scoping. Use ignores for decisions the whole team should share.
The diagram shows two stages. File-level ignoring happens before the engines run, so ignored files cost no scan time. Inline suppression happens inside an engine, after it has already analyzed the file.
The ignores section
Add an ignores key at the top level of code-analyzer.yml. Under it, files takes a list of glob patterns.
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"
- "**/*.min.js"
- "force-app/test-fixtures/**"Run a scan with this file:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector RecommendedA file that matches any pattern in the list is skipped by every engine. This applies to PMD, ESLint, RetireJS, Regex, Flow, and CPD alike. You do not need one entry per engine.
Glob syntax you will use
Patterns are globs, not regular expressions. These forms cover most needs:
| Pattern | Matches |
|---|---|
| `**/node_modules/**` | Any `node_modules` folder at any depth, and everything inside it |
| `**/*.min.js` | Any file ending in `.min.js`, anywhere |
| `force-app/main/default/staticresources/**` | One specific folder and its contents |
| `**/legacy/*.cls` | Apex classes directly inside any `legacy` folder, but not in its subfolders |
A single * matches within one folder level. A double ** crosses folder levels. Quote every pattern in YAML. An unquoted value that starts with * is read as a YAML alias and breaks the file.
Re-including files with negation
Some Code Analyzer versions let a pattern that starts with ! re-include a path that an earlier pattern excluded. For example, you might ignore a whole folder except one file you do own:
ignores:
files:
- "**/staticresources/**"
- "!**/staticresources/AppBootstrap/**"Check that your installed version supports negation by running the scan and confirming the re-included file appears in the results. If it does not, narrow the ignore pattern instead. List the specific subfolders you want to skip rather than excluding the parent.
Relative paths and the config file location
Relative patterns are resolved against a base directory. In practice, run the command from the project root, where code-analyzer.yml and sfdx-project.json live, and keep your patterns relative to that root. If you run the same config from another directory, a pattern like force-app/test-fixtures/** may stop matching.
Code Analyzer reads code-analyzer.yml from the current directory automatically if you do not pass --config-file. In CI, passing --config-file explicitly is safer. It makes it obvious which file drives the scan and fails loudly if the file is missing.
Checking that an ignore works
An ignore that silently matches nothing is a common mistake. A typo in a folder name leaves the noisy files in your results, and you never see an error. Verify each new pattern:
- Run a scan before adding the pattern and note a finding from the target path.
- Add the pattern.
- Run the same scan again. That finding should be gone.
- Confirm that findings from files you still own are present.
Step 4 matters as much as step 3. An overly broad pattern such as **/*.cls hides every Apex finding. The run then exits with success, and the build goes green while no Apex code is being checked.
Ignoring rules rather than files
Sometimes the problem is not a path but a rule that does not suit your project. Ignoring files is the wrong tool then, because it removes all rules for those files. Two better options exist.
- Select fewer rules with
--rule-selector. For instance,pmd:Securityruns only PMD rules tagged Security. - Lower a rule's severity in the
rulessection, as shown in the previous chapter on overrides:
rules:
pmd:
AvoidDebugStatements:
severity: "Info"A rule at severity 5 (Info) still appears in reports. It stays below a --severity-threshold 2 check, so it no longer fails the build.
Inline suppression for single findings
When one line is a justified false positive, suppress that line and leave the rest of the file scanned. For PMD rules in Apex, add a NOPMD comment with the reason:
public class Logger {
public static void trace(String msg) {
System.debug(msg); // NOPMD - intentional debug logging in sandbox-only utility
}
}This differs from ignores.files in two ways. It is visible next to the code, so a reviewer can judge the reason. It is also narrow, so it cannot hide a new problem added elsewhere in the file later. Each engine has its own suppression syntax. ESLint, for example, uses eslint-disable comments. Check the documentation for the engine that reports the finding.
Common errors
- Pattern has no effect. The pattern does not match the path as the tool sees it. Add
**/at the start and check spelling and case. - YAML parse error. An unquoted pattern starting with
*or!. Wrap each pattern in double quotes. - Ignores work locally but not in CI. The CI job runs from a different directory, or never passes
--config-file. Run from the project root and pass the config path explicitly. - Everything is ignored. A pattern is too broad. Compare the file count or findings before and after the change.
- Ignored folder still scanned by another tool. Other tools, such as your editor or a separate lint step, have their own ignore rules.
code-analyzer.ymlonly controls Code Analyzer.
Summary
Use --workspace and --target to scope a single run. Use ignores.files in code-analyzer.yml for paths the project should never scan. Use rule selectors, severity overrides, or inline suppression when the issue is a rule or a single line. Test each new pattern by comparing results before and after.
Exercise
Your scan reports 40 findings inside force-app/main/default/staticresources/jquery, which is a vendored library. You add "staticresources/**" to ignores.files, but the findings still appear. What is the likely cause, and what pattern fixes it?
Show answer
The pattern has no leading **/, so it only matches a staticresources folder at the base path root, not force-app/main/default/staticresources. Use the quoted pattern "/staticresources/", or the exact path "force-app/main/default/staticresources/**". Then rerun the scan to confirm the jquery findings are gone and findings from your own files remain.
Exercise
A single System.debug call in a sandbox-only logging utility triggers the AvoidDebugStatements rule. Should you add the whole file to ignores.files? Explain which option to use instead.
Show answer
No. Ignoring the file would hide every future finding in it. Add an inline // NOPMD comment on that line with a short reason, or, if the rule is too noisy project-wide, lower its severity to Info in the rules section of code-analyzer.yml.