Sub topic 1
What a Salesforce DX Project Is
A Salesforce DX project is a directory of source files that you keep in version control and that the Salesforce CLI (sf) reads to know what to deploy, retrieve, and package. This chapter gives the working model. Later chapters cover each file in detail.
Prerequisites
This chapter assumes you have:
- Basic Salesforce concepts: orgs, metadata, Apex, and Lightning components.
- The Salesforce CLI (
sf) installed. - Comfort with the command line and Git.
- The ability to read JSON.
You do not need an authorized org to follow the reading, but you will need one to run deploy commands in later chapters.
The Working Model
Before DX, many teams treated a Salesforce org as the source of truth. Changes were made in the org, and metadata was pulled out as needed. A DX project reverses this. The files in your repository are the source of truth, and orgs are places you push that source to or pull changes from.
A DX project has three parts that work together:
- Source files. Your Apex classes, Lightning web components, custom objects, and other metadata, stored as files on disk.
- Project configuration. A file named
sfdx-project.jsonat the project root. It tells the CLI where your source lives and how to treat it. - Supporting files. Files such as
.forceignore(what to leave out) and a scratch org definition (how to shape a temporary org).
The CLI finds the project by looking for sfdx-project.json in the current directory and then in its parent directories. If it finds none, project-aware commands such as sf project deploy start fail.
Creating a Project
The CLI generates a project with the standard files in place:
sf project generate --name my-project
cd my-project
ls -aThe listing shows sfdx-project.json, .forceignore, .gitignore, a config folder, a force-app folder, and a scripts folder. The next chapter tours each of these.
What the Project File Controls
sfdx-project.json is small, but every project-aware command reads it. A generated file looks like this:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}| Setting | What it tells the CLI |
|---|---|
| `packageDirectories` | Which folders hold your source |
| `sourceApiVersion` | Which API version to use for source operations |
| `sfdcLoginUrl` | Which login URL to use when authorizing an org |
| `namespace` | The namespace for the project, empty if none |
| `name` | A name for the project |
Common Mistakes at This Stage
- Running commands outside the project. Project commands need
sfdx-project.jsonin the current directory or a parent. Run them from inside the project folder. - Editing the JSON by hand and breaking it. A trailing comma or missing quote makes the whole file invalid, and every project command then fails.
- Treating an org as the source of truth. If changes exist only in an org, they are not in version control. Retrieve them into the project and commit them.
Exercise
You run sf project deploy start from your home directory, which is outside any DX project, and the command fails. What does the CLI look for, and where should you run the command instead?
Show answer
The CLI looks for sfdx-project.json in the current directory and then in its parent directories. Because none exists above your home directory, the command fails. Change into the project folder (the one containing sfdx-project.json, or a subfolder of it) and run the command there.
Exercise
Name the three parts of a DX project and give one example file for each.
Show answer
Source files (for example an Apex class under force-app), project configuration (sfdx-project.json), and supporting files (for example .forceignore or the scratch org definition file).