Sub topic 1
The Salesforce DX project and sfdx-project.json
Prerequisites
This chapter assumes you have:
- Basic command-line use.
- Familiarity with Salesforce orgs and metadata such as Apex classes, custom objects, and Lightning components.
- The Salesforce CLI (
sf) installed. - Basic Git knowledge.
You do not need an authorized org yet. Authorization is covered in a later chapter.
What a Salesforce DX project is
A Salesforce DX project is a local directory that holds your org's metadata as files, plus a configuration file named sfdx-project.json. The sf commands that retrieve and deploy metadata read this file to learn where your source lives. Run them from inside the project, or from a subdirectory of it.
The project is meant to live in version control. Git tracks the source files, and the CLI moves them between the project and an org.
Creating a project
Create a project with sf project generate:
sf project generate --name invoice-app
cd invoice-appThe command creates a layout like this:
invoice-app/
├── .forceignore
├── .gitignore
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
├── manifest/
│ └── package.xml
├── scripts/
└── sfdx-project.json| Path | Purpose |
|---|---|
| `sfdx-project.json` | Project configuration. Marks the project root. |
| `force-app/` | The default package directory. Your metadata source goes here. |
| `force-app/main/default/` | Where retrieved components are written, in folders such as `classes` and `objects`. |
| `manifest/` | Holds `package.xml` manifests. |
| `.forceignore` | Lists files the CLI skips. A later chapter covers it. |
| `config/` | Scratch org definition files. |
Reading sfdx-project.json
This is a typical sfdx-project.json:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "invoice-app",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "60.0"
}The important keys are:
packageDirectories: the directories the CLI treats as source. Each entry has apath. Exactly one entry is marked"default": true. New components retrieved without a specified location are written to the default directory.sourceApiVersion: the API version the CLI uses for retrieve and deploy. Components are deployed at this version unless you override it.namespace: the namespace of a managed or unlocked package you develop. Leave it empty if you have none.sfdcLoginUrl: the login URL for authorization. Change it for a sandbox or a My Domain login.
A project can have several package directories, which is useful for splitting a large codebase:
{
"packageDirectories": [
{ "path": "core", "default": true },
{ "path": "billing" }
],
"sourceApiVersion": "60.0"
}The CLI treats the contents of every listed directory as part of the project.
How the CLI uses the project
The diagram shows how a command locates your source.
If the CLI cannot find sfdx-project.json, project commands fail. The usual cause is running them from the wrong directory.
Common mistakes
- Running project commands outside the project folder.
- Editing
sourceApiVersionto a version the org does not support. The org then rejects the request. - Leaving two entries marked
"default": true, or none, in a multi-directory project. - Putting source in a folder that is not listed under
packageDirectories.
Exercise
A teammate clones your repository and runs sf project deploy start. The CLI reports that there is nothing to deploy, but force-app/ is full of files. Which file would you check first, and what would you look for?
Show answer
Check sfdx-project.json. Look for a packageDirectories entry whose path matches the folder holding the source (for example force-app). If the folder is missing from the list, the CLI ignores its contents. Also confirm you are running the command inside the project directory.
Exercise
Which key in sfdx-project.json sets the API version used for retrieve and deploy?
Show answer
sourceApiVersion.