Sub topic 13
Common pitfalls and how to verify a conversion
This chapter assumes you know what a package.xml manifest is, how Metadata API format differs from source format, and how to run sf project convert. It covers the errors people hit most often and gives a repeatable way to check that a conversion kept everything.
Why conversions fail quietly
Conversion is a file transformation, not a deployment. The CLI reads files, splits or merges them, and writes the result. It rarely stops with an error when something is missing. A component can be skipped, a file can be left out, or a setting can be changed, and the command can still report success. This is why you verify after every conversion.
The usual causes fall into four groups:
- Files that the CLI does not treat as part of the project.
- Companion files that are missing or do not match their main file.
- Settings in
sfdx-project.jsonthat point at the wrong place. - Wrong assumptions about which format a command expects.
Pitfall 1: Pointing a command at the wrong format
Each command expects one layout. sf project convert source expects source format. sf project convert mdapi expects Metadata API format. sf project deploy start --metadata-dir expects Metadata API format. sf project deploy start --source-dir expects source format.
If you pass a source-format folder to convert mdapi, the CLI will not find the package.xml and the .object files it expects. You will get an empty or partial output folder.
A quick check for each layout:
| Layout | Signs you are looking at it |
|---|---|
| Metadata API format | A `package.xml` at the root, folders such as `classes/` and `objects/`, and one `Account.object` file per object |
| Source format | An `sfdx-project.json` above it, `-meta.xml` suffixes on almost every file, and objects as folders with `fields/` inside |
Pitfall 2: Missing or mismatched companion files
Apex classes, triggers, and some other types need a companion file that ends in -meta.xml. For MyClass.cls, the companion is MyClass.cls-meta.xml. It holds the API version and the status. The two files must have the same base name.
Common mistakes:
- Adding a
.clsfile by hand and forgetting the-meta.xmlfile. - Renaming one file of the pair and not the other.
- Copying a companion file and leaving its
apiVersionat an old value.
The result is a component that is skipped during conversion or rejected at deployment. Check that each pair exists with this command.
# List every .cls file that has no matching -meta.xml file
for f in force-app/main/default/classes/*.cls; do
[ -f "${f}-meta.xml" ] || echo "Missing companion: $f"
donePitfall 3: Bundles and static resources treated as single files
Some types are folders, not files. Lightning web components and Aura components are bundles. A bundle is a folder of related files that Salesforce treats as one component. Static resources can also be a folder of content, with a -meta.xml file next to it that holds the content type.
Common mistakes:
- Moving or deleting one file from a bundle, such as the
.jsfile of a Lightning web component, and leaving the rest. - Renaming the bundle folder without renaming the files inside it.
- Forgetting the
.resource-meta.xmlfile for a static resource. - Expecting a bundle to be split the way a custom object is. Bundles are kept together. Only certain types, such as custom objects, are decomposed.
Pitfall 4: A decomposed object that is not fully present
In source format, a custom object is a folder. The object file holds the object's own settings. Fields, record types, list views, and other child items sit in their own files in subfolders.
When you convert back to Metadata API format, the CLI merges those child files into one Account.object file. If a child file is missing from the source folder, the field is missing from the output. Nothing warns you.
The most common ways to lose child files:
- A
.forceignorepattern that is broader than intended. - A partial retrieve that returned only some fields.
- A merge conflict resolved by deleting a file.
Pitfall 5: `.forceignore` hides more than you think
The .forceignore file lists paths that the CLI skips during retrieve, deploy, and convert. A pattern such as **/*.field-meta.xml removes every field. A pattern written for one folder can match files in another.
To see the effect, run a conversion with and without the pattern and compare the output. If counts differ in a way you did not expect, read the file line by line.
# Too broad: removes every field in every object
**/fields/**
# Narrower: removes only fields of one object
**/objects/Account/fields/**Pitfall 6: `sfdx-project.json` points at the wrong place
The CLI reads packageDirectories to decide which folders to scan. Problems here are easy to miss:
- A
paththat does not exist produces an empty result. - Two package directories that contain the same component make the result depend on directory order.
- A
sourceApiVersionthat is older than the version of a component you use can cause fields or settings to be dropped or rejected.
Check the file whenever a conversion produces fewer files than you expect.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}Pitfall 7: Reusing an output folder
If you convert into a folder that already has files, old files stay. The output then mixes new content with stale content. A deleted field can still appear in the output because its old file was never removed.
Always convert into an empty or new folder.
rm -rf mdapi_output
sf project convert source --root-dir force-app --output-dir mdapi_outputDeleting a generated output folder is safe, because it can be recreated from source. Do not run this on a folder that holds work you have not saved anywhere else.
How to verify a conversion
Verification has four steps. Do them in order, because each step catches a different kind of problem.
Step 1: Check the output
Confirm the output folder exists and has the shape you expect. For Metadata API format, look for package.xml and the type folders.
ls mdapi_output
cat mdapi_output/package.xmlRead the package.xml. Every type you expect should be listed, and the version should match your sourceApiVersion.
Step 2: Compare counts
Count the main files on each side. Counts will not match one for one, because source format has many more files. Compare components instead.
# Apex classes: source format has two files per class, so divide by 2
ls force-app/main/default/classes/*.cls | wc -l
ls mdapi_output/classes/*.cls | wc -l
# Fields in source format
find force-app -name "*.field-meta.xml" | wc -lThe class counts should match exactly. The field count in source format should match the number of <fields> entries in the merged object files.
grep -c "<fields>" mdapi_output/objects/Account.objectFor one object, compare that number with the number of field files under Account/fields/. Standard fields that you did not customize are not in either side, so the two counts match only for fields that are in the project.
Step 3: Round trip
Convert the result back and compare it to the original.
rm -rf roundtrip
sf project convert mdapi --root-dir mdapi_output --output-dir roundtrip
diff -r force-app/main/default roundtripExpect small differences in element order or whitespace. Missing files, missing elements, or changed values are real problems. Because round trips can reorder content, run this in Git as well. Put the result in a branch and review git diff to see exactly what changed.
Step 4: Validate with a check-only deploy
The strongest check is to ask a real org whether it accepts the output. A validation deploy runs everything except the final save.
sf project deploy start --metadata-dir mdapi_output --dry-run --wait 10Use a sandbox or scratch org for this. The --dry-run flag checks the deployment without saving changes, but you should still not point it at an org you are unsure about.
If validation passes, the files are well formed and complete enough for the org. It does not prove that nothing was dropped, because a missing field that nothing depends on will not cause an error. Steps 2 and 3 cover that gap.
Quick troubleshooting table
| Symptom | Likely cause | What to check |
|---|---|---|
| Output folder is empty | Wrong format passed, or `path` in `sfdx-project.json` is wrong | The `--root-dir` value and `packageDirectories` |
| A class is missing from output | No matching `-meta.xml` file | File names of the pair |
| Fields missing from `Account.object` | Field files absent or ignored | `.forceignore` and `fields/` folder |
| Deleted component still appears | Output folder was reused | Delete the folder and convert again |
| Deploy says the API version is invalid | Mismatched `version` or `apiVersion` | `package.xml`, `sfdx-project.json`, and companion files |
| Bundle fails to deploy | Part of the bundle is missing | All files in the bundle folder |
Habits that prevent most problems
- Convert into a new folder each time.
- Commit before converting, so Git shows every change.
- Keep
.forceignorepatterns narrow and comment them. - Run a validation deploy before a real one.
- Keep one API version across
package.xml,sfdx-project.json, and companion files unless you have a reason to differ.
Exercise
You convert a source-format project to Metadata API format. The output Account.object has 12 <fields> entries, but force-app/main/default/objects/Account/fields/ holds 14 field files. A validation deploy of the output passes. Name two likely causes and the check you would run for each.
Show answer
Likely causes are (1) a .forceignore pattern that removes some field files during conversion, and (2) field files whose names do not match their fullName, or that are malformed or in a folder outside the configured package directories. For the first, read .forceignore and run the conversion with the pattern removed to see whether the count becomes 14. For the second, compare each *.field-meta.xml file name with its <fullName> and check that the files sit under a path listed in packageDirectories. The validation deploy passing does not rule out missing fields, because nothing in the org depends on them, so the count comparison is the check that found the problem.
Exercise
Why should you delete the output folder before each conversion, and why is the round trip with diff -r not enough on its own to prove the project is correct?
Show answer
Conversion does not remove old files, so a reused folder can keep a component you already deleted and make the output look correct when it is not. A round trip can only compare files that survived the first conversion. If a file was dropped in both directions, or the original never had it, the diff shows nothing. You also need component counts and a validation deploy against an org.