Sub topic 2
Salesforce DX source format: what the files look like
This chapter assumes you know basic Git, can navigate a Salesforce DX project with a force-app directory, and can read simple XML. Merge conflicts are decided by file layout. Before you can resolve one, you need to know which files exist, which are split into small pieces, and which stay as one large file.
Two formats for the same metadata
Salesforce metadata exists in two formats.
- Metadata format is the older layout used by the Metadata API and change sets. Related metadata is packed into large files. A custom object file, for example, holds every field, list view and validation rule for that object.
- Source format is the layout used by Salesforce DX projects. Many types are decomposed into small files, one per component.
The Salesforce CLI converts between the two. Retrieving from an org into a DX project produces source format. You normally work only in source format, and Git only ever sees those files.
# Convert a DX project into metadata format, for example to inspect the old layout
sf project convert source --root-dir force-app --output-dir mdapi-outDecomposition is the reason some metadata types almost never conflict and others conflict constantly.
The project layout
A typical DX project has this shape:
my-project/
├── sfdx-project.json
├── .forceignore
├── .gitignore
├── .sf/
└── force-app/
└── main/
└── default/
├── classes/
│ ├── AccountService.cls
│ └── AccountService.cls-meta.xml
├── labels/
│ └── CustomLabels.labels-meta.xml
├── layouts/
│ └── Account-Account Layout.layout-meta.xml
├── lwc/
│ └── accountCard/
│ ├── accountCard.html
│ ├── accountCard.js
│ ├── accountCard.css
│ └── accountCard.js-meta.xml
├── objects/
│ └── Account/
│ ├── Account.object-meta.xml
│ ├── fields/
│ │ ├── Region__c.field-meta.xml
│ │ └── Tier__c.field-meta.xml
│ └── validationRules/
├── permissionsets/
│ └── Sales_Access.permissionset-meta.xml
└── profiles/
└── Admin.profile-meta.xmlA few parts matter for conflicts.
sfdx-project.json
This file declares which directories are package directories and which API version the project uses. It is plain JSON, and it conflicts when two branches add a package directory or change sourceApiVersion.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Naming convention
Every metadata file follows Name.type-meta.xml. The part before -meta.xml tells you the metadata type, such as permissionset, profile, field, layout or labels. Apex classes and triggers are different. The code lives in a .cls or .trigger file, and a small companion -meta.xml file holds the API version and status.
What Git should not track
The .sf/ and .sfdx/ directories hold local source-tracking state and org information. They belong in .gitignore. The .forceignore file is separate. It tells the CLI which files to skip when it deploys or retrieves.
Decomposed types
When a type is decomposed, each component gets its own file. Adding a custom field to Account creates one new file in objects/Account/fields/. If two developers add different fields on different branches, Git sees two new files with different names and merges them without any conflict.
Types that are decomposed in source format include:
- Custom fields, in
fields/. - Validation rules, in
validationRules/. - List views, in
listViews/. - Record types, in
recordTypes/. - Workflow rules, alerts and field updates, each in its own file.
- Sharing rules.
Conflicts can still happen here, but only when two branches edit the same component. That is a real competing edit, and a later chapter covers how to choose between such edits.
Single-file types
Some types are not decomposed, however large they get. Three matter most.
Profiles and permission sets
Each profile or permission set is one file. It contains a flat list of repeated elements such as fieldPermissions, objectPermissions, classAccesses and userPermissions. A busy permission set can run to thousands of lines.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<classAccesses>
<apexClass>AccountService</apexClass>
<enabled>true</enabled>
</classAccesses>
<fieldPermissions>
<editable>true</editable>
<field>Account.Region__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>Account.Tier__c</field>
<readable>true</readable>
</fieldPermissions>
<label>Sales Access</label>
<objectPermissions>
<allowCreate>true</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>true</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
</PermissionSet>Custom labels
All custom labels live in a single CustomLabels.labels-meta.xml file. Every new label is a new labels block in that one file.
<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
<labels>
<fullName>Welcome_Message</fullName>
<language>en_US</language>
<protected>false</protected>
<shortDescription>Welcome message</shortDescription>
<value>Welcome to Sales</value>
</labels>
</CustomLabels>Layouts, flows and similar files
Each page layout and each flow version is one file. A layout is a long list of sections, fields and related lists. A flow is a graph of elements, and each element carries locationX and locationY canvas coordinates. Moving a box on the canvas changes the file even when the logic stays the same.
How the CLI shapes the XML
The way the CLI writes XML helps merging more than most people expect.
- Files are UTF-8 with an XML declaration and a
xmlnsnamespace on the root element. - Indentation is four spaces. Nothing in the format needs tabs.
- Child elements are written in a consistent order. In the permission set above,
classAccessescomes beforefieldPermissions, andlabelcomes beforeobjectPermissions. That is alphabetical by element name. - Repeated elements are generally ordered by their key, such as the field name or Apex class name, so retrieving the same org twice produces the same file.
Consistent output means a diff shows only what really changed. It also means two additions with neighboring names, such as Account.Segment__c and Account.Tier__c, are inserted at the same position, and Git cannot tell whose block comes first. This is the main cause of conflicts in profiles and permission sets.
Finding out what a file contains
Before you resolve a conflict, find out which type of file it is. Use the file name, and use Git to list the paths in conflict.
# List the conflicted files
git diff --name-only --diff-filter=U
# Count lines to see how big each conflicted file is
wc -l force-app/main/default/permissionsets/*.xml
# Retrieve one current copy from an org for comparison
sf project retrieve start --metadata PermissionSet:Sales_Access --target-org my-sandbox| File pattern | Layout | Typical conflict |
|---|---|---|
| `*.field-meta.xml` | One file per field | Rare, only when two branches edit the same field |
| `*.permissionset-meta.xml` | One file per permission set | Frequent, from additions at the same position |
| `*.profile-meta.xml` | One file per profile | Frequent, and large |
| `CustomLabels.labels-meta.xml` | One file for all labels | Frequent, from adjacent additions |
| `*.layout-meta.xml` | One file per layout | Occasional, with long competing edits |
| `*.flow-meta.xml` | One file per flow version | Occasional, noisy because of coordinates |
| `*.cls`, `*.js`, `*.html` | Source code files | Ordinary code conflicts |
Exercise
Two developers each add a new custom field to the Account object on separate branches, and both also grant access to their field in the same permission set. Which of these changes is likely to produce a Git conflict, and why?
Show answer
The field files themselves will not conflict. Source format stores each field in its own file under objects/Account/fields/, so the two branches add two different files. The permission set will probably conflict, because it is a single file and both branches insert a new fieldPermissions block at neighboring positions in the same sorted list.
Exercise
A teammate opens a flow in Flow Builder, drags one element a few pixels, and saves. They commit the retrieved file. Why does this cause trouble for merging, and what in the file changed?
Show answer
Each flow element stores locationX and locationY canvas coordinates in the flow's XML file. Moving an element changes those values even though the flow's logic is the same. The commit adds noise that can conflict with another branch's real edit to the same element.