Sub topic 5
Keeping the XML valid while you edit
This chapter assumes you can run basic Git merge commands, know the layout of a Salesforce DX project, and can read simple XML. Here you will learn how to edit a conflicted metadata file so that the result is still valid XML that Salesforce accepts.
Why validity matters
Git treats a metadata file as plain text. It has no idea that the file is XML. When a merge conflicts, Git writes marker lines into the file, and those lines are not legal XML. Until you remove them, the file cannot be parsed, retrieved, or deployed.
Git can also produce a conflict-free merge that is still broken. If two branches change nearby lines, Git may combine them without asking you, and the combined text can have an unclosed tag or a duplicated element. So you need to check validity even for files that did not conflict.
What can go wrong
These are the errors that show up most often when you edit a conflicted file by hand:
- Leftover marker lines such as
<<<<<<< HEAD,=======or>>>>>>> feature/new-field. - A half-deleted block, where you removed an opening
<fieldPermissions>tag but kept its closing tag. - A duplicated element, such as two
fieldPermissionsblocks for the same field. - Wrong nesting, where a child element ends up outside its parent after a copy and paste.
- A changed encoding or lost XML declaration, often caused by an editor that rewrites the file.
Edit by whole blocks
Metadata files are built from repeating blocks, such as one fieldPermissions element per field. Work in whole blocks. Select from an opening tag to its matching closing tag, then keep it or delete it.
<<<<<<< HEAD
<fieldPermissions>
<editable>false</editable>
<field>Account.Tier__c</field>
<readable>true</readable>
</fieldPermissions>
=======
<fieldPermissions>
<editable>true</editable>
<field>Account.Segment__c</field>
<readable>true</readable>
</fieldPermissions>
>>>>>>> feature/new-fieldHere each side contains one complete block. Keeping both means deleting exactly three lines: the <<<<<<< line, the ======= line, and the >>>>>>> line. Do not touch the lines in between.
A check loop
After each file, run three quick checks. They go from cheapest to most thorough.
# 1. No markers are left anywhere in the project
grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app
# 2. The file is well-formed XML (xmllint comes with libxml2)
xmllint --noout force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
# 3. Salesforce accepts the metadata, without saving changes
sf project deploy start --dry-run --source-dir force-app --target-org my-sandboxThe first check finds markers. The second finds malformed XML, and it reports a line and column. The third finds metadata that is well-formed but invalid, such as a reference to a field that does not exist in the target org.
Order of the checks
Editor habits
- Turn off any setting that reformats XML on save. Reformatting changes indentation and attribute order, and Git then sees changes on every line.
- Keep the indentation of the surrounding file, which is four spaces in standard source format.
- Keep the file's line endings. Converting between LF and CRLF makes a whole file look changed.
- Stage a file with
git addonly after all three checks pass.
Exercise
After a merge you run xmllint --noout on a permission set and it prints no errors. A teammate says the file is therefore safe to commit. What check is still missing, and what kind of problem can it find?
Show answer
The deployment dry run (sf project deploy start --dry-run) is still missing. xmllint only confirms the file is well-formed XML. The dry run can find problems Salesforce rejects, such as duplicate fieldPermissions entries for the same field or references to fields or objects that do not exist in the target org.