Sub topic 7
Controlling what is tracked with .forceignore
This chapter assumes you can generate a DX project, know where sfdx-project.json lives, and have run sf project deploy start and sf project retrieve start. It covers what .forceignore does, the pattern rules it follows, and the mistakes that cause confusing results.
What .forceignore does
.forceignore is a plain text file in the project root. It tells the Salesforce CLI which files in your package directories to leave alone. The CLI skips matching files in these operations:
- Deploys to an org.
- Retrieves from an org. Matching files are not written to your local folders.
- Source tracking in scratch orgs and sandboxes with tracking enabled. Ignored files never show up as local changes or remote changes.
- Conversion between source format and metadata format.
The file is a filter on top of packageDirectories. The directories listed in sfdx-project.json define where the CLI looks. .forceignore removes files from that set. A file outside every package directory is never considered, so you don't need to ignore it.
Where the file lives
Put .forceignore in the same folder as sfdx-project.json. Patterns are interpreted relative to that folder, so force-app/main/default/classes/ means the same thing no matter where you run the command. sf project generate creates a starter .forceignore for you. Commit it to Git so every developer and your CI job filter the same files.
Pattern syntax
The file uses gitignore-style rules. Each line is one pattern.
| Pattern element | Meaning |
|---|---|
| `# text` | Comment line. |
| `*` | Matches any characters within one path segment. |
| `**` | Matches across directory levels. |
| `?` | Matches a single character. |
| `[abc]` | Matches one character from the set. |
| `!pattern` | Negation. Re-includes files that an earlier pattern excluded. |
| Blank line | Ignored. |
Four rules cause most of the surprises:
- Use forward slashes in every pattern, including on Windows.
- Later lines override earlier lines. Put a negation after the pattern it undoes.
- You cannot re-include a file if a parent directory is already excluded. This is standard gitignore behavior.
- A pattern with no slash, such as
*.dup, matches at any depth. A pattern with a slash is anchored to the path you wrote.
A realistic example
# Local tooling output that is not Salesforce metadata
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
# Do not move profiles between orgs
**/profiles/**
# Ignore one class and its metadata file
**/classes/LegacyImporter.cls*
# Ignore all Experience Cloud sites except one
**/experiences/**
!**/experiences/Partner_Portal1/**Notes on each block:
- The first block removes files that live next to your components but are not metadata. The Lightning Web Components (LWC) folder commonly holds Jest tests in
__tests__and editor config files. If these reach a deploy, the deploy fails or uploads clutter. - The profiles pattern is common in teams that manage access with permission sets. Profiles retrieve differently depending on what else is in the retrieve request, so they create noisy diffs.
- The class pattern ends with
*. An Apex class is two files,LegacyImporter.clsandLegacyImporter.cls-meta.xml. Matching.cls*catches both. If you ignore only one, you can get a deploy error or a half-tracked component. - The last block ignores everything under
experiencesand then re-includes one folder. This works only because the first pattern excludes the contents with/**, not theexperiencesfolder itself. If you wrote**/experiences/instead, the folder would be excluded and the!line would have no effect.
Checking what is ignored
Do not guess whether a pattern matched. Run the preview command, which compares your local source with the org and reports what a deploy would send, including files that .forceignore excluded:
sf project deploy preview --target-org my-scratchLook for the ignored section in the output. If a file you expected to see there is listed as a change, the pattern is wrong. If a file you wanted to deploy shows up as ignored, a pattern is too broad.
You can also check the file with Git. Because the syntax matches gitignore, this command shows which line in a copy of the file matches a path:
git check-ignore -v --no-index --exclude-from=.forceignore force-app/main/default/classes/LegacyImporter.clsGit's matcher and the CLI's matcher agree on the common cases, but treat the CLI output as authoritative.
Interaction with the other project files
.forceignore works together with the files you saw in earlier chapters.
sfdx-project.jsondecides which folders are scanned..forceignorefilters inside them. If a component is missing, check both..gitignoreis separate. It controls Git, not the CLI. A file can be in.gitignoreand still deploy, or in.forceignoreand still be committed. Keep the two files consistent on purpose, for example by ignoring.sf/and.sfdx/in Git but not in.forceignore, since those folders are outside your package directories anyway.- The scratch org definition file is unrelated to ignoring. It sets org features and settings. If a setting such as a feature flag is missing,
.forceignoreis not the cause.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| A retrieve finishes but the file is not on disk | A pattern matches the file | Run `sf project deploy preview` and narrow the pattern. |
| Deploy error that a `-meta.xml` file has no matching source file | You ignored only the source file or only the `-meta.xml` file | End the pattern with `*` so both files match. |
| A `!` line has no effect | A parent directory is excluded | Exclude the contents with `/**` and re-include a subfolder. |
| Pattern works on macOS and not on Windows | Backslashes in the pattern | Use forward slashes. |
| Everything is ignored | A pattern such as `**` or `*` with no limits | Anchor the pattern to a folder or file type. |
| A new file never appears as a local change | Source tracking skips ignored files | Check patterns for the file's path. |
| Pattern with a trailing space does not match | Trailing whitespace is part of the pattern in some editors | Remove trailing spaces. |
Design guidelines
- Start from the generated file and add to it. Do not delete the defaults unless you know what they protect.
- Ignore by purpose, with a comment above each block. A file that says why
profilesis excluded is easier to change later. - Prefer narrow patterns.
**/classes/LegacyImporter.cls*is safer than**/Legacy*. - Treat changes to
.forceignoreas code changes. Review them, because one new line can quietly stop a component from deploying through CI. - Do not use
.forceignoreto hide deploy errors. If a component fails validation, fix it. Ignoring it makes the production deploy differ from the one you tested.
Worked example
Suppose your project has this tree, and a deploy fails because of a Jest test file under lwc:
force-app/main/default/lwc/orderList/
├── __tests__/
│ └── orderList.test.js
├── orderList.html
├── orderList.js
└── orderList.js-meta.xmlAdd this line to .forceignore:
**/__tests__/**Then confirm the result:
sf project deploy preview --target-org my-scratch
sf project deploy start --target-org my-scratchThe preview should list orderList.html, orderList.js, and orderList.js-meta.xml as changes and report the test file as ignored. The deploy then sends only the component files.
Exercise
Your .forceignore contains the lines **/experiences/ followed by !**/experiences/Partner_Portal1/**. The Partner_Portal1 site still does not deploy. Why, and what change fixes it?
Show answer
The first line excludes the experiences directory itself, and gitignore-style rules do not let you re-include a file when its parent directory is excluded, so the negation has no effect. Change the first line to **/experiences/** so only the contents are excluded, and keep the !**/experiences/Partner_Portal1/** line after it. Then run sf project deploy preview to confirm the site is no longer listed as ignored.
Exercise
You want to ignore the Apex class LegacyImporter locally. You add **/classes/LegacyImporter.cls and the next deploy fails with a complaint about LegacyImporter.cls-meta.xml. What is wrong with the pattern?
Show answer
An Apex class has two files, the .cls source and the .cls-meta.xml companion. The pattern matches only the .cls file, so the metadata file is still treated as part of the deploy without its source. Use **/classes/LegacyImporter.cls* so both files are ignored.