Sub topic 1
The Salesforce DX project and sfdx-project.json
Every sf project retrieve and sf project deploy command works relative to a Salesforce DX project. The CLI reads the project's configuration to decide which local folders count as source, which API version to use, and where newly retrieved files go. This chapter covers the layout of a project, the keys in sfdx-project.json that matter for retrieve and deploy, and the mistakes people most often make.
Prerequisites
This chapter assumes you have:
- Basic command-line skills, such as changing directories and running commands.
- The Salesforce CLI (
sf) installed. Runsf --versionto check. - Some familiarity with Salesforce metadata such as Apex classes, custom objects, and Lightning components.
- Basic Git knowledge, because a DX project is meant to live in version control.
You do not need an authorized org yet. That comes in a later chapter.
What a DX project is
A Salesforce DX project is a directory with a file named sfdx-project.json at its root. The CLI treats that file as the marker of a project. When you run a project command, it searches the current directory and then each parent directory for that file. If it finds none, the command fails with a message saying the command must run inside a Salesforce project.
The project stores your org's metadata as text files in source format, which is designed to work well with Git. The next chapter explains source format in detail. Here it is enough to know that the files live under one or more package directories that you declare in sfdx-project.json.
Creating a project
Generate a project with the standard template:
sf project generate --name acme-app
cd acme-appThe command creates the following layout.
acme-app/
├── sfdx-project.json
├── .forceignore
├── .gitignore
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── lwc/
│ ├── aura/
│ ├── objects/
│ └── triggers/
├── manifest/
│ └── package.xml
└── scripts/Some details, such as the exact set of starter folders, vary slightly between CLI versions. The roles below do not change.
| Path | Role |
|---|---|
| `sfdx-project.json` | Project configuration. Declares package directories and the API version. |
| `force-app/` | The default package directory. Holds your source. |
| `force-app/main/default/` | Conventional location for metadata. Subfolders are named by type, such as `classes` or `objects`. |
| `manifest/` | Holds `package.xml` files for manifest-based retrieve and deploy. |
| `.forceignore` | Lists files the CLI must not push to or pull from an org. |
| `config/` | Scratch org definition files. |
| `.sf/` | Local CLI state, such as source tracking data. Do not commit it. |
The .sf/ folder (older projects may also have .sfdx/) appears after you first use an org from the project. The generated .gitignore already excludes it.
Reading sfdx-project.json
This is a typical file for a project with two package directories:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
},
{
"path": "unpackaged-extras",
"default": false
}
],
"name": "acme-app",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "60.0"
}packageDirectories
packageDirectories is the only key you must get right for retrieve and deploy. It is an array, and each entry has a path relative to the project root. The CLI considers only files under these paths as project source.
This has two practical effects:
- A file outside every package directory is invisible to
sf project deploy start --source-dir, even if it is valid metadata. - Passing
--source-dirwith a path that is not inside a package directory produces an error, because the CLI cannot map it to the project.
Exactly one entry should have "default": true. When you retrieve a component that does not yet exist locally, the CLI writes it into the default package directory. When a component already exists locally, the retrieve updates it where it already is, and the default setting is not used.
If you list more than one directory, the CLI treats them as one combined source tree. Two directories that both contain classes/AccountService.cls-meta.xml cause a duplicate-component problem, so each component should exist in only one place.
The package, versionName, versionNumber, ancestorId, and dependencies keys also belong in a package directory entry. They apply to second-generation packaging and are not needed for plain retrieve and deploy.
sourceApiVersion
sourceApiVersion sets the metadata API version the CLI uses when it retrieves and deploys. It also affects the <version> it expects in a manifest. A common surprise is that the project is on 60.0, while an org, a copied manifest, or a teammate's file uses another version. Mismatched versions can lead to metadata fields that exist in one version and not in the other. If you leave this key out, the CLI falls back to the org's API version, which makes behavior depend on the org.
You can override the version for a single run with the --api-version flag on supported commands, or set it for all runs with sf config set org-api-version. Keeping it in sfdx-project.json is better because the value is committed, so everyone and every CI job uses the same one.
The other keys
namespaceis the namespace prefix for a managed package you are developing. Leave it empty for ordinary projects.sfdcLoginUrlis the login host for org authorization. Change it tohttps://test.salesforce.comif you often authorize sandboxes, or to a My Domain URL.packageAliasesmaps friendly names to package IDs. It is only used with packaging.pluginsholds settings for CLI plugins and is ignored by retrieve and deploy.
Because the file is plain JSON, comments and trailing commas make it invalid. A syntax error here stops every project command.
How the CLI uses the project
The diagram shows how a command resolves what to act on.
Two points follow from this order. First, the project is resolved from your working directory, so running a command from an unrelated folder fails even when the project is nearby. Second, .forceignore is applied after package directories are read, so it only narrows the set of source files and never adds to it.
Working with the project
Inspect the effective configuration for a project with:
sf config listRun it from inside the project folder. It shows config values such as target-org, which can be set per project (stored in .sf/config.json) or globally with --global. A project-level setting wins over a global one, which lets different repositories point to different default orgs.
Add a second package directory by creating the folder and editing the JSON:
mkdir unpackaged-extrasThen add its entry to packageDirectories. If you want new retrieves to land there, move "default": true to that entry.
Common errors
- Command run from the wrong folder. The CLI cannot find
sfdx-project.json. Change into the project directory first. - Invalid JSON. A trailing comma or a missing quote makes the file unreadable. Validate it with a JSON-aware editor.
- A
paththat does not exist. A package directory entry whose folder is missing or misspelled causes an error when the CLI loads source. Create the folder or fix the path. - No default, or two defaults. With several package directories, mark exactly one as default. Otherwise the CLI rejects the file or picks a location you did not intend.
- Source outside package directories. Files you add in a stray folder are silently ignored by a directory-based deploy. Move them under a declared path.
- Committing
.sf/. It holds machine-specific tracking data and causes confusing conflicts between teammates.
Summary of what to remember
The sfdx-project.json file defines what the CLI considers your source. Package directories define the boundaries, one of them is the default landing place for new components, and sourceApiVersion pins the metadata API version. With these set correctly, the commands in the following chapters behave the same on every machine.
Exercise
You add a new Apex class file in a folder named scratch-work/ at the project root, then run sf project deploy start --source-dir force-app. The class does not reach the org. What are two possible fixes?
Show answer
The folder scratch-work/ is not listed in packageDirectories, and it is also not under force-app, so the CLI does not see the file. Either move the class into a declared package directory such as force-app/main/default/classes/, or add scratch-work as a new entry in packageDirectories and include it in the deploy, for example by passing --source-dir scratch-work as well.
Exercise
A project has two package directories, force-app and unpackaged-extras, and unpackaged-extras is marked "default": true. You retrieve an Apex class that already exists in force-app, and then a brand new Apex class that exists only in the org. Where does each file land?
Show answer
The existing class is updated in place under force-app, because retrieve writes existing components where they already live. The new class is written into unpackaged-extras, because that is the default package directory.