Sub topic 13
Common pitfalls and how to verify a conversion
This chapter assumes you know the two layouts, the package.xml manifest, sfdx-project.json, and the sf project convert commands from earlier chapters. It covers the mistakes that appear most often when you convert between formats, and a short routine to check that nothing was lost.
Why conversions go wrong
A conversion rearranges files. It does not change what your org does. Problems almost always come from one of three places: a wrong input directory, a missing companion file, or a manifest that does not match the files on disk. Each of these can give you an output that looks fine but is incomplete.
Pitfall 1: Wrong root directory or project setup
--root-dir must point at the folder that holds the source, such as force-app, not at the project root. If you point at the wrong folder, the command can finish with few or no components.
Source-to-Metadata API conversion also reads sfdx-project.json. If packageDirectories lists a path that does not exist, or the file is missing, the CLI cannot treat the folder as a source-format project.
Pitfall 2: Missing or mismatched `-meta.xml` files
Apex classes, triggers, and many other components need a companion file. MyClass.cls must have MyClass.cls-meta.xml next to it. If you rename or copy one file and not the other, the pair breaks.
Bundles and static resources are a common source of this. Check that every file that needs a companion has one with the exact same base name.
Pitfall 3: Manifest does not match the files
A package.xml names types and members. If a member is listed but no file exists, a deploy reports an error. If a file exists but no manifest entry covers it, the component is silently left out of a manifest-driven retrieve or deploy.
Conversion also writes a package.xml into the output folder. If you hand-edit the original manifest and forget the generated one, the two can disagree.
Pitfall 4: Files excluded by `.forceignore`
.forceignore hides files from source tracking, deploy, and retrieve. A pattern that is too broad, such as **/objects/**, can remove components from the conversion without any warning. When a component is missing from the output, read .forceignore first.
Pitfall 5: Assuming decomposition is lossless in a diff
In source format, Account.object becomes a folder with one file per field. A line-by-line comparison of the two layouts will never match, so you cannot verify a conversion with a plain diff of the original and the output. Verify by component, not by file.
A verification routine
Use a round trip. Convert to the other format, convert back, and compare with the original.
# 1. Source -> Metadata API format
sf project convert source --root-dir force-app --output-dir mdapi_output
# 2. Check the generated manifest lists what you expect
cat mdapi_output/package.xml
# 3. Metadata API format -> source format, into a new folder
sf project convert mdapi --root-dir mdapi_output --output-dir roundtrip
# 4. Compare the round trip with the original source
diff -r force-app roundtrip/main/defaultThe final diff should report no differences, or only trivial ones such as line endings or XML ordering. Note that roundtrip is written as a package directory, so the original force-app/main/default contents appear under roundtrip/main/default. Adjust the second path if your layout differs.
flowchart TD
A[Source format] --> B[Convert to Metadata API format]
B --> C[Inspect package.xml]
C --> D[Convert back to source format]
D --> E{diff against original}
E -->|No differences| F[Conversion verified]
E -->|Differences| G[Check .forceignore and meta files]
G --> BThe diagram block above uses a plain mermaid fence for display only. The platform diagram block follows.
Further checks
- Count components. Compare the number of
.clsfiles in both layouts. - Open the generated
package.xmland confirm the<version>matchessourceApiVersion. - Deploy to a scratch org or sandbox with a check-only run before touching production.
sf project deploy start --metadata-dir mdapi_output --dry-run --wait 10Exercises
Exercise
A converted output folder has no Apex classes, but the source folder has several. The command reported no errors. Name two causes you would check first.
Show answer
Check that --root-dir points at the folder that actually holds the source (for example force-app), and check .forceignore for a pattern that excludes the classes. A missing -meta.xml companion file is another valid check.
Exercise
Why does a plain diff -r between a Metadata API format folder and a source format folder fail as a verification method?
Show answer
Decomposition splits objects such as Account.object into a folder with separate field files, so the file names and contents differ by design. Use a round trip back to the same format, then compare.