Sub topic 4
Source format: the Salesforce DX layout
This chapter assumes you know what a metadata component is, can read XML, and have used Git and a terminal. You should also have seen the Metadata API format, where a package.xml manifest sits above folders of components.
What source format is
Source format is the on-disk layout used by Salesforce DX projects. It holds the same metadata as the Metadata API format. The difference is organization. Source format breaks large components into smaller files and puts everything under one or more package directories. Version control then shows small, readable diffs, and two developers can change different fields on the same object without touching the same file.
A source format project has no package.xml at its center. It has an sfdx-project.json file at the root, and that file marks the directory as a DX project.
The shape of a project
The next listing compares the two layouts for the same Apex class and the Account object.
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 project root holds
sfdx-project.jsoninstead ofpackage.xml. - Components live under a package directory,
force-app/main/default/by default, not at the top level. Account.objectis now a folder namedAccount. It contains a small object file and afields/subfolder with one file per field.
Apex classes look the same in both formats. The class body is MyClass.cls, and its companion MyClass.cls-meta.xml holds settings such as the API version.
flowchart TDOne field, one file
In source format, each custom field is its own file. This is the field from the listing above.
<?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>Adding a field creates a new file. Changing its length edits a few lines in that one file. In the Metadata API format, both changes would edit the large Account.object file, where merge conflicts are more likely.
The project file
sfdx-project.json tells the CLI which folders hold source and which API version to use.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}packageDirectorieslists the folders the CLI treats as source. Exactly one entry should have"default": true. New components generated by CLI commands go there.namespacestays empty unless you build a namespaced package.sourceApiVersionsets the API version used when the CLI deploys or retrieves your source. It plays the role that<version>plays inpackage.xml.
A later chapter covers multiple package directories in more detail.
Things to know when working in source format
- Run commands from inside the project. The CLI looks for
sfdx-project.jsonin the current directory or a parent. Without it, source-format commands fail. - Only listed directories count. A component placed outside every
pathinpackageDirectoriesis ignored by deploys. - Keep file names and folder names exact. The folder name (
classes,objects,fields) tells the CLI which metadata type a file is. A misplaced file may not be recognized. - Edit files directly. Because the layout is plain text, you can change a field file in an editor and deploy it without touching other components.
Check your understanding
Exercise
A teammate adds a custom field Region__c to the Account object in a source format project. Which file is created, where does it go, and why does this reduce merge conflicts compared with the Metadata API format?
Show answer
The CLI creates Region__c.field-meta.xml in force-app/main/default/objects/Account/fields/. Each field is its own file, so two developers adding different fields change different files. In the Metadata API format, both fields would be edited inside the single Account.object file, which makes overlapping edits and conflicts more likely.