Sub topic 5
Keeping files out of tracking with .forceignore
This chapter assumes you have a Salesforce DX project, an authorized org, and the sf CLI installed. You should also know the basic layout of force-app. Source tracking is covered in earlier chapters. Here you learn how to keep files out of it.
What .forceignore does
A .forceignore file lists patterns for files that the CLI skips. The CLI skips them when it deploys, when it retrieves, and when it computes tracked changes. A file that matches a pattern is not synchronized with the org. It also never shows up in preview output.
Your project holds files that the org has no use for. Examples are editor settings, lint configuration, and Jest tests for Lightning web components. If the CLI tried to deploy these, it would report errors or list noise as local changes. Ignoring them keeps the change list limited to real metadata.
The file sits at the project root, next to sfdx-project.json. Projects created with sf project generate include a starter version.
Writing patterns
.forceignore uses the same pattern syntax as .gitignore. Each line is one pattern. Here is a typical example:
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**Each line works like this:
**/jsconfig.jsonignores every file with that name, in any directory.**/.eslintrc.jsonignores ESLint configuration files in any directory.**/__tests__/**ignores everything inside any__tests__folder, which is where Jest tests for Lightning web components live.
Other common features:
- Lines that start with
#are comments. - A line that starts with
!re-includes a file that an earlier pattern excluded. - A pattern that ends in
/matches directories only.
# Keep lint and editor files out of the org
**/.eslintrc.json
**/.prettierrc
# Ignore a whole folder of scratch work
force-app/main/default/scratch/
# But keep one file from a pattern that is too broad
**/*.tmp
!force-app/main/default/staticresources/keep.tmpIgnored files and tracking
Ignoring a file has three effects:
sf project deploy previewandsf project retrieve previewdo not list it.sf project deploy startdoes not send it, even if you name its directory with--source-dir.- A retrieve does not write it into your project, even if the org has a change to that component.
The third effect surprises people. If you ignore a component and someone edits it in the org, you will not receive that change. Do not ignore real metadata you expect to stay in sync.
Changing .forceignore on an existing project
Adding a pattern does not delete anything. Files that match stay on disk and in the org. They just stop being tracked and moved. If a component is already in the org and you now ignore it, the CLI treats it as outside the project.
Editing the file is a local change, so you can test it safely. Use the preview commands, which change nothing:
sf project deploy preview --target-org my-scratch
sf project retrieve preview --target-org my-scratchCompare the output before and after your edit. Files that disappear from the list are now ignored.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Pattern is too broad | Real components stop deploying | Narrow the pattern or add a `!` exception |
| `.forceignore` is not at the project root | Patterns are not applied as expected | Move it next to `sfdx-project.json` |
| Ignoring a component the org edits | Org changes never reach your project | Remove the pattern, or accept that the component is unmanaged |
| Expecting `.forceignore` to replace `.gitignore` | Ignored files still appear in Git | Keep both files, since they serve different tools |
Exercise
Your team keeps Jest tests in __tests__ folders next to each Lightning web component. Deploys to your scratch org fail because the CLI tries to send them. Which line do you add to .forceignore, and which command do you run to confirm the fix without changing the org?
Show answer
Add **/__tests__/** to .forceignore at the project root. Then run sf project deploy preview --target-org my-scratch. The test files should no longer appear in the list of local changes.