Sub topic 11
Controlling files with .forceignore
This chapter assumes you can read a source-format project layout and run sf project commands. It also assumes you know what sfdx-project.json does. Here you will learn how to keep files out of deploys, retrieves, and conversions.
What .forceignore does
A source-format project often holds files that Salesforce should not treat as metadata. Examples are editor configuration, Jest tests for Lightning Web Components, and profiles you manage in the org only.
The .forceignore file lives in the project root, next to sfdx-project.json. It lists patterns, and the CLI skips any file in a package directory that matches one. The CLI applies the file in these places:
sf project deploy startandsf project retrieve startwhen you use source-format options such as--source-dir,--manifest, or--metadata.sf project convert source, so ignored files do not appear in the converted output.- Source tracking, which means ignored files are not reported as local changes.
The CLI does not use .forceignore to filter a directory you deploy with --metadata-dir. That directory is already in Metadata API format, and the CLI sends it as it is.
Pattern syntax
The syntax follows .gitignore rules:
- A line that starts with
#is a comment. *matches any characters within one folder level.**matches across folder levels.- A leading
!re-includes something an earlier pattern excluded. - A trailing
/limits a pattern to directories.
Here is a typical file:
# Not metadata
package.xml
**/jsconfig.json
**/.eslintrc.json
# LWC Jest tests
**/__tests__/**
# Manage profiles in the org only
**/profiles/**
# Ignore all permission sets except one
**/permissionsets/*
!**/permissionsets/Sales_Admin.permissionset-meta.xmlThe first block comes from the default file that a new DX project generates. A root package.xml is ignored so the CLI does not read it as a component. The __tests__ folders hold Jest tests that the org cannot store.
Paths are relative to the project root, so **/profiles/** matches force-app/main/default/profiles/Admin.profile-meta.xml and the same folder in any other package directory.
Checking what is ignored
Edit the file, then check the result before you deploy. A simple check is to convert and list the output:
sf project convert source --root-dir force-app --output-dir mdapi_output
find mdapi_output -type f | sortIf a file you expect is missing, a pattern matched it. If a file you wanted ignored is present, the pattern did not match. Common causes are a wrong path depth or a missing **.
Pitfalls
Ignore a component as a whole. A source file and its companion -meta.xml file belong together, so if you ignore one and not the other, deploys can fail with missing-file errors.
Other things to watch for:
- A broad pattern such as
**/profiles/**also blocks retrieves. The CLI will not write profile files into your project, which can look like a failed retrieve. - A
!rule only works if it comes after the pattern it overrides. Later lines win. .forceignorefilters files, not metadata types. Usepackage.xmlor--metadatato choose types, and use.forceignoreto remove files inside what you selected.- Ignoring files affects what you convert, so a package built from an ignored-file project may omit components you still need in a Metadata API format deploy.
Exercise
Your project has Jest tests in force-app/main/default/lwc/myCmp/__tests__/. You run sf project convert source and the tests appear in the output, which breaks a later deploy. Which .forceignore line fixes this, and why does it not help if you deploy the converted folder with --metadata-dir?
Show answer
Add **/__tests__/** to .forceignore in the project root. The convert step then skips those files. A --metadata-dir deploy does not read .forceignore, so any test files already in that folder would still be sent. Regenerate the folder after fixing the pattern.