Sub topic 3
Source Format and How It Maps to Metadata
This chapter assumes you can generate a project with sf project generate, you know what a custom object, field, and Apex class are, and you can read XML. It explains why the files under force-app look the way they do and how they correspond to what the org stores.
Two formats for the same metadata
Salesforce stores configuration as metadata. The Metadata API has always exchanged that metadata in one shape, called metadata format. Salesforce DX adds a second shape, called source format. Both describe the same org configuration. They differ in how the files are arranged on disk.
| Aspect | Metadata format | Source format |
|---|---|---|
| Entry point | `package.xml` manifest | `sfdx-project.json` package directories |
| Custom object | One large `.object` file | A folder of small files, one per child component |
| Typical use | Older tooling, Metadata API deploys, zip files | Version control, scratch orgs, `sf project` commands |
| File suffix | `.object`, `.layout`, `.profile` | `.object-meta.xml`, `.field-meta.xml`, and so on |
The reason for source format is version control. A metadata-format object file can hold hundreds of fields, list views, and validation rules. Two developers who each add a field edit the same file, so Git reports a merge conflict. In source format each field has its own file, so the same two changes don't collide.
How a custom object is decomposed
Source format splits a parent component into a core file plus one file for each child component. A child component is a piece of configuration that belongs to an object, such as a field or a validation rule. Each child type gets a folder named for the type.
force-app/main/default/objects/
└── Invoice__c/
├── Invoice__c.object-meta.xml
├── fields/
│ ├── Amount__c.field-meta.xml
│ └── Status__c.field-meta.xml
├── listViews/
│ └── All_Open.listView-meta.xml
├── recordTypes/
│ └── Standard.recordType-meta.xml
└── validationRules/
└── Amount_Positive.validationRule-meta.xmlThe core file Invoice__c.object-meta.xml keeps only settings of the object itself, such as its label, sharing model, and name field. Each child file holds a single component.
Because the fence in the chapter must use the diagram form, here is the diagram of the mapping.
A field file is ordinary Metadata API XML for a single CustomField:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Status__c</fullName>
<label>Status</label>
<type>Text</type>
<length>20</length>
<required>false</required>
<externalId>false</externalId>
</CustomField>The file name must match the component name. Renaming Status__c.field-meta.xml by hand without updating <fullName> leads to confusing deploy errors.
The `-meta.xml` suffix
Files that end in -meta.xml hold metadata about a component. Components with source code, such as Apex, split into two files: the code and its companion metadata file.
force-app/main/default/classes/
├── InvoiceService.cls
└── InvoiceService.cls-meta.xml<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>Triggers follow the same pattern with .trigger and .trigger-meta.xml. Always commit both files together. A .cls file without its -meta.xml companion fails to deploy.
Components with no code body, such as layouts, permission sets, and profiles, use a single file whose name ends in -meta.xml, for example permissionsets/Invoice_Admin.permissionset-meta.xml.
Bundles
Some components are bundles, meaning one component made of several files that live in a folder named for the component.
force-app/main/default/lwc/invoiceList/
├── invoiceList.html
├── invoiceList.js
├── invoiceList.css
└── invoiceList.js-meta.xmlAura components use aura/<name>/ with files such as <name>.cmp, <name>Controller.js, and <name>.cmp-meta.xml. For bundles the folder name is part of the component identity. Rename the folder and the files together, or the CLI can't match them.
Static resources use a related arrangement. The resource lives in staticresources/, and a .resource-meta.xml file describes it:
force-app/main/default/staticresources/
├── logo.png
├── logo.resource-meta.xml
├── charts/
│ └── chart.min.js
└── charts.resource-meta.xml<?xml version="1.0" encoding="UTF-8"?>
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
<cacheControl>Private</cacheControl>
<contentType>image/png</contentType>
</StaticResource>A folder such as charts/ is zipped automatically at deploy time, and contentType for that resource is typically application/zip.
Folder names and type recognition
Under force-app/main/default, folder names match Metadata API directory names: classes, triggers, lwc, aura, objects, layouts, permissionsets, staticresources, and so on. The CLI identifies most components by their file suffix. Bundles, decomposed object children, and folder-based types (reports, dashboards, email templates) also depend on the parent folder.
The path main/default is a convention. The CLI doesn't require it. What matters is that the folder is inside a directory listed in packageDirectories. The next chapters on sfdx-project.json cover that. You can have more than one package directory, and each one can contain its own main/default tree.
Converting between formats
You will meet metadata format when you integrate with older tooling, receive a package from a vendor, or use a tool that expects package.xml. The CLI converts in both directions.
# Source format to metadata format
sf project convert source --root-dir force-app --output-dir mdapi-out
# Metadata format back to source format
sf project convert mdapi --root-dir mdapi-out --output-dir force-app-convertedAfter the first command, mdapi-out contains package.xml, classes/, and a single objects/Invoice__c.object that merges the separate field, list view, and validation rule files. The second command reverses the process and recreates the folder-per-object structure.
Two options narrow the conversion:
--manifest path/to/package.xmlconverts only the components the manifest lists.--metadata ApexClasslimits the conversion to a metadata type.
Edge cases and gotchas
- Profiles and permission sets are not decomposed. Each remains one large XML file, so merge conflicts there are still common. Teams often keep profiles out of source control with
.forceignoreand use permission sets instead. - Retrieved files are rewritten. A retrieve normalizes XML ordering and formatting. A diff after retrieving can show reordered elements even when nothing changed in the org.
- A missing companion file breaks deploys.
MyClass.clsneedsMyClass.cls-meta.xml. The reverse also fails. - Names are case sensitive in file systems that are. On Linux,
invoiceListandInvoiceListare different folders. On macOS and Windows they aren't, so a mismatch can pass locally and fail in CI. - Mixed formats in one directory. Putting a
.objectfile underforce-appcan confuse the source resolver, because that file name ends without-meta.xml. Convert it first. - Custom labels and some other types are single files.
labels/CustomLabels.labels-meta.xmlholds every label unless you enable the decomposition setting available for some types. Check release notes for the types you use.
Putting it together
When you run sf project deploy start, the CLI walks the package directories, groups files into components using the rules above, and converts them to metadata format in memory before sending them to the org. sf project retrieve start does the reverse, writing decomposed files back into your project. You work in source format, and the org still receives metadata format.
Exercise
A teammate adds Discount__c.field-meta.xml under objects/Invoice__c/fields/ and a second teammate adds Region__c.field-meta.xml in the same folder on another branch. Why does Git merge both branches without conflict, and what would the same change look like in metadata format?
Show answer
In source format each field is its own file, so the two branches add different files and Git has nothing to reconcile. In metadata format both fields live inside the single Invoice__c.object file, so both branches edit the same file, often at nearby lines, and Git reports a conflict.
Exercise
You run sf project convert source --root-dir force-app --output-dir mdapi-out. Name two things you expect to find in mdapi-out that do not exist as such in force-app.
Show answer
A package.xml manifest listing the converted components, and a single merged file such as objects/Invoice__c.object that contains the fields, list views, and validation rules that were separate files in source format.