Sub topic 4
Keeping files out of your project with .forceignore
This chapter assumes you have a Salesforce DX project, an authorized org, and a working knowledge of sf project retrieve start. You should also know what source format looks like on disk.
What .forceignore does
A .forceignore file sits in the root of your project, next to sfdx-project.json. It lists paths that the Salesforce CLI skips when it moves metadata. The CLI checks it on retrieve, deploy, and source tracking. An ignored file is not written during a retrieve and is not sent during a deploy.
The syntax follows .gitignore. Each line is a pattern, # starts a comment, and ! negates a pattern.
Why you need it
Orgs contain metadata that you often don't want in version control or in a deployment:
- Profiles and other files that differ between orgs and create noisy diffs.
- Generated or personal files, such as
.sfdx,.sf, and editor settings. - Components that belong to a managed package or that your team doesn't own.
- Test folders, scratch notes, and other files that are not metadata.
A starting file
# Ignore Salesforce tool state and local settings
**/.sfdx/**
**/.sf/**
.vscode/
# Ignore one profile that differs per org
**/profiles/Admin.profile-meta.xml
# Ignore every Jest test folder
**/jest.config.js
**/__tests__/**
# Keep one file that a broader pattern would otherwise exclude
!force-app/main/default/classes/KeepMe.clsPatterns are matched against paths relative to the project root. ** matches any number of folders, and * matches characters within one folder name.
How a pattern takes effect
The check happens on each file. If you ask for a component by name, for example --metadata ApexClass:AccountService, and its file is ignored, the CLI has nothing to write. You see a warning or an empty result instead of an error.
Test a change
After you edit .forceignore, run a preview to see what the CLI now considers.
sf project deploy preview --target-org my-sandboxIgnored files don't appear in the list. If a file you expect is missing, a pattern is probably too broad.
Common errors
- A file still deploys. Check the path. A pattern such as
Admin.profile-meta.xmlwithout**/only matches at the project root. Prefix it with**/to match it at any depth. - A file disappeared from retrieves. An earlier broad pattern, such as
**/profiles/**, still matches it. Add a!line below that pattern for the file you want. Order matters, and the later line wins. - A
!line has no effect. Git-style rules don't let you re-include a file when its parent folder is excluded. Ignore the files inside the folder instead of the folder itself. - Changes seem ignored. The file must be named exactly
.forceignore, with the leading dot, in the project root. - A component is half-deployed. Some components have several files, such as a class and its
-meta.xmlfile. Ignore both, or neither.
Keep it in version control
Commit .forceignore so that everyone on the team and your CI jobs apply the same rules. Otherwise one developer deploys a file that another developer's machine ignores.
Exercise
A teammate adds **/profiles/** to .forceignore, but you still need Admin.profile-meta.xml to deploy. What do you add, and where?
Show answer
Add the line !force-app/main/default/profiles/Admin.profile-meta.xml after the **/profiles/** line. Order matters, and the later negation re-includes the one file. Run sf project deploy preview to confirm that it appears in the list.