Sub topic 11
Controlling files with .forceignore
This chapter assumes you can read a source-format project, know what sfdx-project.json does, and have run sf project convert source at least once. It covers the .forceignore file: what it filters, how its patterns behave, and the mistakes that cause confusing deploy and convert results.
What .forceignore does
A source-format project holds more than deployable metadata. It can contain test harness files, editor configuration, JavaScript tooling files, and component files you never want in a particular org. .forceignore is a plain text file in the project root, next to sfdx-project.json. It tells the Salesforce CLI to skip matching files when it reads or writes source.
The CLI consults it for these operations:
- Deploying from a source directory or manifest.
- Retrieving, where ignored files are not written into your project.
- Converting source format to Metadata API format.
- The source tracking commands, which report local and remote changes in a scratch or sandbox org.
It does not touch Git. A file listed in .forceignore is still tracked by Git unless .gitignore also lists it. The two files solve different problems: .gitignore decides what goes into version control, and .forceignore decides what the CLI treats as Salesforce metadata.
The default file
Generating a project with sf project generate creates a starter .forceignore:
# List files or directories below to ignore them when running deploy,
# retrieve, and source tracking commands.
package.xml
# LWC configuration files
**/jsconfig.json
**/.eslintrc.json
# LWC Jest
**/__tests__/**Each entry has a reason:
package.xmlkeeps a manifest from being treated as a component inside a package directory. Source format does not need a manifest in the package directory, and the converter generates one in its output.jsconfig.jsonand.eslintrc.jsonsupport editors and linters. Salesforce does not accept them as part of a Lightning web component bundle.__tests__folders hold Jest unit tests for Lightning web components. They live next to the component for convenience but must not be deployed.
Pattern syntax
.forceignore uses the same pattern rules as .gitignore. Paths are relative to the project root, and you always write them with forward slashes, even on Windows.
| Pattern | Matches |
|---|---|
| `# text` | A comment line. Blank lines are ignored too. |
| `*.tmp` | Any file ending in `.tmp`, at any depth. |
| `**/__tests__/**` | Everything inside any folder named `__tests__`, at any depth. |
| `/scratch/` | Only a `scratch` directory in the project root, because of the leading slash. |
| `force-app/main/default/staticresources/` | One specific directory. The trailing slash restricts the match to directories. |
| `!keep.cls` | Negation. It re-includes a file that an earlier pattern excluded. |
Order matters because later patterns override earlier ones. Negation has one restriction inherited from Git. If a pattern excludes a parent directory, you cannot re-include a file inside it. To keep one file from a folder, exclude the folder's contents with folder/* and then add the negation.
# Ignore every file in the folder, then bring one back
force-app/main/default/classes/legacy/*
!force-app/main/default/classes/legacy/KeepMe.cls
!force-app/main/default/classes/legacy/KeepMe.cls-meta.xmlThe example lists the companion -meta.xml file explicitly. This is the most common source of broken results, covered below.
How the filter fits into a conversion
The ignore step happens when the CLI builds its list of source files, before it writes any output.
Because ignored files never reach the converter, the generated package.xml does not list them. A file that was excluded is simply absent from the output, and the CLI usually does not warn you about it.
A realistic .forceignore
A team repository often grows beyond the default. Here is a more complete file with comments explaining each choice:
# Manifests are inputs to commands, not deployable components
package.xml
# LWC tooling
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
# Local experiments that live in the repo but are not for any org
force-app/main/default/classes/sandbox_only/*
!force-app/main/default/classes/sandbox_only/README.md
# Do not deploy example data
**/*.csv
# Profiles are managed through permission sets in this project
**/profiles/**The last entry is a policy decision, not a default. Ignoring profiles removes them from every deploy and retrieve, so make that choice deliberately and tell the team.
Verifying what is ignored
Do not guess whether a pattern works. Test it with commands that show the result.
First, check the effect on a conversion. This script converts the project and lists the output so you can look for missing or unexpected files:
#!/usr/bin/env bash
set -euo pipefail
rm -rf mdapi_output
sf project convert source --root-dir force-app --output-dir mdapi_output
# Every converted file, sorted
find mdapi_output -type f | sort
# Look for a file you expected to be ignored
if find mdapi_output -name '*.csv' | grep -q .; then
echo "WARNING: CSV files were converted" >&2
fiSecond, check a deployment without changing the org. A validation-only deploy reports which components the CLI found:
sf project deploy start --source-dir force-app --dry-run --wait 10If a component you expected is not in the result table, a pattern is probably excluding it. If a component you wanted to exclude appears, the pattern is wrong or sits in the wrong place in the file.
For Git, git check-ignore -v path explains .gitignore decisions. There is no equivalent for .forceignore, so the conversion listing is your main check.
Pitfalls
Ignoring the code file but not the companion
Apex classes, triggers, and some other types need a -meta.xml file next to the source (covered in the companion files chapter). If you ignore MyClass.cls and leave MyClass.cls-meta.xml, the CLI sees a metadata file with no code and the operation fails or produces a partial result. Write patterns that cover both, for example **/classes/Legacy*, which matches the class and its metadata file.
Ignoring one part of a bundle
Lightning web components and Aura bundles are folders. The platform expects the bundle to be complete, so ignoring one file inside a bundle, other than tooling files the platform rejects anyway, gives you a bundle that cannot deploy. Ignore the whole bundle folder or none of it.
Ignored files and retrieve
Retrieving metadata for a component whose file path matches an ignore pattern writes nothing locally. This can look like a failed retrieve. If a retrieve reports success but a file is missing, check .forceignore first.
Ignored changes in source tracking
Source tracking skips ignored files. A change made in the org to an ignored component never appears in sf project retrieve preview, and a local edit never appears as a pending deploy. This is the intended behavior, but it surprises people who forget they added a broad pattern.
Wrong path style
Patterns written with backslashes, such as force-app\main\default\classes, do not work. Use forward slashes on every operating system.
Broad patterns written in a hurry
A pattern like *.xml excludes every metadata file in the project. A pattern such as **/test* excludes classes named TestUtil and Testimonial as well. Anchor patterns with a leading directory, and check the conversion output after each change.
Choosing between .forceignore and .gitignore
| Need | Use |
|---|---|
| Keep build output and `node_modules` out of the repository | `.gitignore` |
| Keep Jest tests and tooling configuration out of deployments but in the repository | `.forceignore` |
| Keep a file out of both | List it in both files |
| Exclude a component from one deployment only | A manifest or `--metadata` selection, not a permanent ignore rule |
The last row matters. .forceignore is a permanent project rule. If you only want to deploy part of the project this once, select that part on the command line and leave the ignore file alone.
Exercise
A project's .forceignore contains force-app/main/default/classes/Old*. After running sf project convert source, the output contains classes/OldHelper.cls-meta.xml is missing, but a teammate reports a deploy error naming OldHelper. What is the most likely cause, and how do you check it?
Show answer
The pattern excludes both OldHelper.cls and OldHelper.cls-meta.xml, so neither file is converted. The error most likely comes from another component, such as a trigger or a class in the output, that references OldHelper. Check by listing the converted output with find mdapi_output -type f | sort and searching the source for the name with grep -r OldHelper force-app. Then either stop ignoring the class or remove the references.
Exercise
You want to ignore every file in force-app/main/default/staticresources/ except Logo.png and its companion file. Write the patterns.
Show answer
Exclude the folder's contents, then re-include the two files: force-app/main/default/staticresources/* !force-app/main/default/staticresources/Logo.png !force-app/main/default/staticresources/Logo.resource-meta.xml Excluding the folder itself with a trailing slash would not work, because Git-style rules cannot re-include a file inside an excluded directory.