Sub topic 2
Generating a Project and Tour of the Standard Layout
This chapter assumes you have the Salesforce CLI (sf) installed, Git available, and a basic grasp of Salesforce metadata such as Apex classes and Lightning web components. You will generate a project, then look at each top-level file and folder to see what it does and which CLI commands read it.
What the generator produces
The sf project generate command creates a Salesforce DX project. It makes a new folder, writes the configuration files the CLI expects, and creates an empty source folder tree. It does not contact any org, so you can run it offline.
sf project generate --name my-project
cd my-project
ls -aThe --name flag sets the folder name and also the name value written into sfdx-project.json. By default, the command uses the standard template. That template is the right choice for most projects. Other templates exist, such as empty and analytics. Run sf project generate --help to see the ones your CLI version supports.
You can also pass --output-dir to choose where the folder is created. Without it, the folder is created in your current directory.
The standard layout
A freshly generated project looks like this. Your version of the CLI may add a few extra files, such as editor settings or lint configuration.
my-project/
├── .forceignore
├── .gitignore
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── lwc/
│ └── objects/
├── scripts/
└── sfdx-project.jsonThe next table summarizes each entry and who reads it.
| Entry | Purpose | Read by |
|---|---|---|
| `sfdx-project.json` | Project configuration: package directories, API version, login URL, namespace, packages | The CLI, on almost every project command |
| `force-app/` | Default package directory that holds your metadata source | Deploy, retrieve, and source tracking |
| `config/project-scratch-def.json` | Scratch org definition: edition, features, settings | `sf org create scratch` |
| `.forceignore` | Patterns for files the CLI should skip | Deploy, retrieve, and source tracking |
| `.gitignore` | Patterns for files Git should skip | Git |
| `scripts/` | A place for helper scripts, such as Apex or shell scripts | You and your CI jobs |
sfdx-project.json
This file is what makes a folder a Salesforce DX project. The CLI looks for it in the current directory and then in each parent directory. Because of that search, you can run sf project deploy start from a subfolder of your project and it still works. Chapter 4 covers the lookup in detail. For now, remember that if this file is missing or invalid, most project commands fail.
The generated file is small:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Here is what each key does at this stage:
packageDirectorieslists the folders that contain your source. The generated project has one,force-app, marked as the default.nameis a label for the project.namespaceis empty, which means no namespace is registered for this project.sfdcLoginUrlis the URL the CLI uses when you log in to an org without specifying one.sourceApiVersionis the API version the CLI uses for source operations such as deploy and retrieve.
Your generated value for sourceApiVersion depends on your CLI version, so it may differ from 61.0. Later chapters cover each key in depth.
force-app and the source folders
force-app is the default package directory. Inside it, main/default is where the source format places metadata. Each metadata type gets its own folder. Apex classes go in classes, Lightning web components in lwc, and custom objects in objects.
The names main and default are conventions, not requirements. The CLI finds metadata by walking the whole package directory and recognizing files by their type-specific folder names and suffixes. Some teams use more than one folder under force-app, such as force-app/main/default and force-app/test/default, to separate production code from test-only code. The CLI treats all of it as one package directory because they share the force-app path.
A generated project often has only some of the type folders. The CLI creates others when you create or retrieve a component of that type. For example, the following command creates an Apex class and places it in the right folder:
sf apex generate class --name AccountService --output-dir force-app/main/default/classesThe command writes two files: AccountService.cls and AccountService.cls-meta.xml. The second file holds metadata about the class, such as its API version and status. You must commit both files, because a deploy needs both.
config/project-scratch-def.json
This file describes the shape of a scratch org. A scratch org is a temporary, source-driven Salesforce org that you create from a definition and discard when you finish. The generated definition looks like this:
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}The file name and folder are conventions. You pass the path to the CLI with --definition-file, so you can keep several definitions, for example one per team or feature set. A later chapter covers the available keys.
.forceignore
.forceignore uses gitignore-style patterns. Files that match are excluded from deploys, retrieves, and source tracking. A typical file contains patterns like these:
# Ignore profile files and local tooling output
**/profiles/**
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**The generated file ignores tooling files that are not metadata, such as jsconfig.json and Jest test folders. If you do not ignore these, the CLI may try to deploy them and fail. The .forceignore file is separate from .gitignore. A file can be ignored by Git and still deploy, or the reverse.
.gitignore and scripts
.gitignore keeps generated or private files out of version control. The standard template ignores the .sf and .sfdx folders, which hold local CLI state such as org aliases and source tracking data, along with node_modules.
The scripts folder is empty at first. It is a convention for anonymous Apex scripts, data seed scripts, and CI helpers. The CLI never reads it on its own.
How the pieces work together
The configuration files feed the commands in a predictable way. The CLI reads sfdx-project.json to learn where source lives, filters that source through .forceignore, and sends the result to the org you target. The scratch org definition only matters when you create a scratch org.
The following commands show the whole flow. They create a scratch org from the definition file, then deploy and retrieve source.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start
sf project retrieve startThe --set-default flag makes the new org the default target, so the later commands need no --target-org flag. With an empty source tree, deploy and retrieve have little to do. They become useful once you add metadata.
Common errors at this stage
These problems appear often when you first work with a generated project.
- "This directory does not contain a valid Salesforce DX project." You ran a project command outside any folder that has
sfdx-project.jsonin it or above it. Runpwdand change into the project folder. - Invalid JSON after a manual edit. A trailing comma or a missing quote makes the whole file unreadable. Check it with a JSON validator or your editor's JSON mode.
- Metadata outside a package directory. A file placed in a folder that is not listed in
packageDirectoriesis not deployed or retrieved. Move it underforce-appor add the folder to the file. - Missing
-meta.xmlfile. Apex classes and several other types need their companion file. A deploy that omits it fails. - Tooling files deployed by mistake. If the deploy complains about an unknown file, add a pattern for it to
.forceignore.
Exercises
Exercise
You run sf project deploy start from my-project/force-app/main/default/classes, and the command works. Which mechanism makes this possible, and what would break it?
Show answer
The CLI searches for sfdx-project.json in the current directory and then in each parent directory until it finds one. It finds the file at my-project/, so the command works from a subfolder. It would fail if no sfdx-project.json exists in the folder or any parent, or if the file contains invalid JSON.
Exercise
A teammate adds a Jest test folder named __tests__ inside an LWC component, and the next deploy fails because of it. Which file do you change, and how does it differ from .gitignore?
Show answer
Add or confirm a pattern such as **/__tests__/** in .forceignore. The .forceignore file controls what the CLI deploys, retrieves, and tracks, while .gitignore controls only what Git tracks. A file can be ignored by one and not the other.