Sub topic 4
Source Format: The Salesforce DX Layout
Assumed background
This chapter assumes you know what Apex classes, custom objects, and custom fields are. It also assumes you can read XML, move around a project directory, and run basic Git commands. It builds on the earlier chapters on metadata types, Metadata API format, and the package.xml manifest. Decomposition, -meta.xml files, sfdx-project.json, and the conversion commands each get their own chapter. Here you get the overall picture of how a source format project is organized.
What source format is
Source format is the on-disk layout used by Salesforce DX projects. It describes the same metadata as Metadata API format, but it is arranged for people and tools that work with files, mainly version control, code review, and the Salesforce CLI.
Metadata API format groups components by type and keeps each component in one file, with a package.xml manifest listing what is included. That layout suits a single retrieve or deploy request. It suits teamwork less well. A custom object such as Account becomes one large Account.object file holding every field, list view, validation rule, and record type. Two developers who each add a field to the same object edit the same file, and Git reports a merge conflict.
Source format avoids this by splitting large components into smaller files. Each field gets its own file, so adding two different fields produces two separate, small diffs.
The project skeleton
A source format project is a directory with an sfdx-project.json file at its root. That file is what marks the directory as a project. Running the CLI generator creates the usual starting layout:
sf project generate --name my-projectThe result looks like this:
my-project/
sfdx-project.json
.forceignore
manifest/
package.xml
config/
project-scratch-def.json
force-app/
main/
default/
classes/
lwc/
objects/
triggers/Each item has a distinct job:
sfdx-project.jsonholds project configuration, including which folders contain source.force-appis a package directory, a folder the CLI treats as source. You can rename it or add more.main/defaultis the conventional path inside a package directory. It is a convention, not a requirement, but most tooling and examples assume it.manifest/package.xmlis optional. You can still use a manifest when you want to retrieve or deploy a specific list of components..forceignorelists files the CLI should skip, much like.gitignore.
The minimal configuration file is short:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}packageDirectories tells the CLI where to look. sourceApiVersion sets the API version used when the CLI retrieves or deploys your source.
The same components in both layouts
The next tree shows one Apex class and one custom object in each layout.
Metadata API format
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account.object
Source format
sfdx-project.json
force-app/main/default/
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account/
Account.object-meta.xml
fields/
Rating_Notes__c.field-meta.xmlThree differences stand out:
- The root changes. Metadata API format starts from
package.xml. Source format starts fromsfdx-project.jsonand a package directory. - The object becomes a folder.
Account.objectturns into anAccount/directory. Its settings stay inAccount.object-meta.xml, and child components move into subfolders. - File names follow a pattern. In source format, metadata files are named
<name>.<type>-meta.xml, for exampleRating_Notes__c.field-meta.xml. The type is part of the name, so you can identify a file without opening it.
Apex classes look the same in both layouts. Source files such as .cls sit next to a small companion file that stores settings such as the API version and status.
What a decomposed file looks like
A field file in source format contains only that field's definition:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Rating_Notes__c</fullName>
<label>Rating Notes</label>
<type>Text</type>
<length>100</length>
<required>false</required>
</CustomField>The root element is CustomField, not CustomObject. In Metadata API format, this same XML would sit inside a <fields> element within the large Account.object file. Source format lifts it out into its own file.
If you change length from 100 to 255, Git shows a one-line change in a file that holds one field. A reviewer can see exactly what changed and nothing else.
How components map to folders
Not every type is decomposed. The table shows common patterns you will meet in a project.
| Component | Where it lives in source format | Pattern |
|---|---|---|
| Apex class | `classes/MyClass.cls` and `classes/MyClass.cls-meta.xml` | Source file plus companion file |
| Custom object | `objects/Account/Account.object-meta.xml` | Folder with an object file |
| Custom field | `objects/Account/fields/Rating_Notes__c.field-meta.xml` | One file per field |
| Lightning web component | `lwc/myComponent/` with `.html`, `.js`, and `.js-meta.xml` files | Bundle folder |
| Static resource | `staticresources/` with a `.resource-meta.xml` file and the resource content | Content plus companion file |
Other children of a custom object follow the same idea as fields. List views, record types, and validation rules each get their own folder under the object, such as listViews/ or validationRules/. The chapter on decomposition covers the full list.
Retrieving and deploying in source format
Source format changes how you refer to components. In Metadata API format you always go through a manifest. In source format you can point at paths or at metadata names, and the CLI works out the rest.
# Retrieve one Apex class by metadata name
sf project retrieve start --metadata ApexClass:MyClass
# Retrieve one field. The CLI writes it to the field's own file.
sf project retrieve start --metadata CustomField:Account.Rating_Notes__c
# Deploy everything under a folder
sf project deploy start --source-dir force-app/main/default/classes
# Deploy using a manifest instead
sf project deploy start --manifest manifest/package.xmlBehind the scenes, the CLI converts your source files into Metadata API format, sends them to the org, and reports the result. When you retrieve, it does the reverse and writes decomposed files into your package directory. You work with the friendly layout, and the Metadata API still receives what it expects.
The following diagram shows that flow.
Why the layout works well with Git
Four properties explain why teams adopt source format:
- Small diffs. One field, one file. Changes stay local.
- Fewer merge conflicts. Two people adding different fields to
Accountnever touch the same file. - Readable history. The file path tells you what changed. A change to
fields/Rating_Notes__c.field-meta.xmlis clearly about that one field. - Predictable location. Because each component has a fixed path, you can find, review, and script against it.
The cost is that a single object is now spread over many files. To view the whole Account object you open a folder, not one file. A deploy of just one field also needs the CLI to assemble the right pieces, which is why you should use the CLI and not copy files by hand.
Common errors
These problems appear often when people start with source format:
- Running CLI commands outside the project. Without a reachable
sfdx-project.json, project commands such assf project deploy startfail. Change into the project root first. - Pointing at a folder not listed in
packageDirectories. The CLI only treats listed folders as source. A new folder with valid metadata is ignored until you add it. - Moving a metadata file but not its companion. If you rename
MyClass.clsand leaveMyClass.cls-meta.xmlbehind, the pair breaks and the deploy fails. - Editing the
Account.object-meta.xmlfile and expecting fields to be inside it. In source format, fields live underfields/. Adding a<fields>element to the object file is not the intended structure. - Treating
main/defaultas required. The CLI does not need that exact path. Teams that rename it must keepsfdx-project.jsonconsistent with the real folders.
Summary
Source format organizes metadata into a Salesforce DX project. A root sfdx-project.json declares package directories, and the metadata sits under paths such as force-app/main/default. Large components like custom objects are split into folders of small files, and every metadata file follows the <name>.<type>-meta.xml pattern. The CLI converts between this layout and Metadata API format whenever you deploy or retrieve, so you get small diffs in Git without changing how the org receives metadata.
Exercise
A teammate adds the custom field Rating_Notes__c to Account in a source format project. Which file does Git show as new, and why does this reduce merge conflicts compared with Metadata API format?
Show answer
Git shows a new file at force-app/main/default/objects/Account/fields/Rating_Notes__c.field-meta.xml. In Metadata API format, every field on Account sits inside the single Account.object file, so two people adding fields both edit that file and can conflict. In source format each field has its own file, so the two changes touch different files and merge cleanly.
Exercise
You run sf project deploy start --source-dir force-app/main/default/classes from your home directory and the command fails. Name one likely cause and the fix.
Show answer
The command was run outside the project. The CLI cannot find sfdx-project.json, so it does not know the project's package directories. Change into the project root, the directory that contains sfdx-project.json, and run the command again.