Sub topic 9
Validating the Resolution with a Deployment Dry Run
This chapter assumes you can run git merge, you know the layout of a Salesforce DX project, and you can run sf commands against a sandbox or scratch org. It covers what to check after you resolve a conflict and before you commit the merge.
Why a clean merge is not a valid merge
Removing the conflict markers makes Git stop complaining. It does not tell you the metadata is correct. A resolved file can still fail in three ways:
- Malformed XML. A leftover marker, a missing closing tag, or a duplicated root element.
- Schema errors. The XML parses but breaks Metadata API rules, for example elements in the wrong place, or a permission that refers to a field that does not exist.
- Org-level errors. The metadata is valid in isolation but fails against the target org, for example a flow that references a deleted custom field, or a profile that grants access to an Apex class that is missing.
Each failure is caught by a different check. The checks are cheap, so run them in order, from fastest to slowest.
Step 1: Search for leftover markers
Git marks a conflict with three kinds of lines: <<<<<<<, =======, and >>>>>>>. Search the whole source directory, not only the file you just edited. A merge can conflict in several files, and it is easy to resolve one and forget another.
grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-appIf the command prints nothing, no markers remain at the start of a line. Be aware of two limits:
grepexits with status 1 when it finds nothing. That is the success case here, so do not treat it as an error in a script.- A line of
=======can legitimately appear inside a long text value, such as a custom label or a description. If a match turns out to be real content, leave it alone.
To list the files Git still considers unresolved, use:
git diff --name-only --diff-filter=UGit builds this list from its index, not from the file contents. A file stays on the list until you run git add on it. The list is therefore not proof that markers remain, and an empty grep is not proof that you staged the file.
Step 2: Check that the XML is well-formed
An XML document is well-formed when it has one root element, every tag is closed, and the tags nest properly. A parser such as xmllint checks this without needing any Salesforce connection.
xmllint --noout force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlWith --noout, xmllint prints nothing on success and prints the line and column of the first error on failure. To check every metadata file that the merge touched, combine it with Git:
git diff --name-only HEAD MERGE_HEAD -- force-app \
| grep -E '\.xml$' \
| xargs -I{} xmllint --noout "{}"MERGE_HEAD points to the tip of the branch you are merging, and it exists only while a merge is in progress. If a file was deleted on the other branch, xmllint reports that it cannot load the file. That is expected for deleted files, so read the messages before you assume something is broken.
Typical well-formedness errors after a conflict resolution:
| Symptom from `xmllint` | Likely cause |
|---|---|
| `Start tag expected, '<' not found` | A marker line is still at the top of the file, or the file is empty. |
| `Opening and ending tag mismatch` | You deleted one side of a conflict that included a closing tag. |
| `Extra content at the end of the document` | Both sides of a block were kept, and a root closing tag now appears twice. |
| `Premature end of data in tag` | The `>>>>>>>` line removed more than the marker. |
Well-formedness is a necessary check, but it is a weak one. xmllint knows XML syntax. It does not know what a fieldPermissions element is, so it cannot tell you that an element is in the wrong place or that a field name is wrong.
Step 3: Run a deployment dry run
A dry run sends the metadata to an org and runs the full deployment validation, but does not save any change. It is the only check that tests your resolved files against real Metadata API rules and real org contents.
sf project deploy start --dry-run --source-dir force-app --target-org my-sandboxPoints to know about this command:
- Choose the org with care. Use a sandbox or scratch org that is a good match for the production target. A dry run against an empty scratch org can fail because a referenced object does not exist there, even though your resolution is correct.
- It deploys the whole directory.
--source-dir force-appvalidates everything, which is the safest choice after a merge, because a conflict in one file can break something that refers to it. For a large project this can take a while. To save time, you can limit the scope to the files the merge changed, with the risk of missing a dependency in an unchanged file. - Tests depend on the target. A deployment to a sandbox does not require Apex tests unless you ask for them. If the files you merged include Apex classes or triggers, add a test level so the dry run exercises them:
sf project deploy start --dry-run --source-dir force-app \
--target-org my-sandbox --test-level RunLocalTests- The command fails the shell. A failed dry run returns a non-zero exit status, so you can use it in a script or a CI job without parsing the output.
Reading the failure output
The command prints a table of component failures. Each row names the file, the line and column, and the message. The row tells you which file to reopen, so start from the first row. Later errors are sometimes a result of earlier ones.
Common messages after a merge:
| Message (shortened) | What it usually means | What to do |
|---|---|---|
| `no CustomField named Account.Segment__c found` | A permission set refers to a field that exists on only one branch. | Merge the field's metadata too, or remove the permission. |
| `Duplicate field permission` or a duplicate-value error | Both sides added the same element, and you kept both. | Remove one copy. |
| `Element ... is out of order` or an unexpected-element error | An element was moved to a position that the schema does not allow. | Reorder the element. |
| `Unknown user permission` | A permission name was changed on one branch and is not valid in the target org. | Check the name against the org. |
Fix the file, then run the dry run again. The loop is short, and repeating it is normal.
What a dry run does not prove
A passing dry run means the org accepts the metadata. It does not mean the merge has the meaning you intended. Consider these cases:
- Silent loss of a change. If you kept only one side of a competing edit, the dry run passes, and the other developer's change is gone. Re-read the diff against both parents.
- Wrong access levels. A permission set with
editableset totrueinstead offalseis valid XML and a valid deployment. Only a reviewer who understands the intent can catch it. - Different org state. The sandbox can differ from production in the objects, fields, and features it has. Passing in one does not guarantee passing in the other.
To compare the result with each side, use:
git diff HEAD -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git diff MERGE_HEAD -- force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xmlThe first command shows what the merge changes compared with your branch. The second shows what it changes compared with the incoming branch. In a correct additive merge, each diff shows only the other side's additions.
Finishing the merge
When all three checks pass and the diffs look right, stage the resolved files and commit.
git add force-app/main/default/permissionsets/Sales_Access.permissionset-meta.xml
git commitGit opens an editor with a prepared merge message. Keep it, and add a line about what you resolved if the reason is not obvious from the diff. If you cannot get the checks to pass and the state is unclear, back out and start again:
git merge --abortThis returns the working directory to its state before the merge, so it is safe as long as you have no uncommitted work you want to keep. Commit or stash that work before you start a merge.
A script that combines the checks
This script stops at the first failure and returns a non-zero exit status. It assumes xmllint and the sf CLI are installed, and that the org alias is the first argument.
#!/usr/bin/env bash
set -euo pipefail
ORG="${1:?Usage: check-merge.sh <target-org-alias>}"
echo "Checking for conflict markers..."
if grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app; then
echo "Conflict markers found. Resolve them first." >&2
exit 1
fi
echo "Checking XML well-formedness..."
find force-app -name '*.xml' -print0 | xargs -0 -n1 xmllint --noout
echo "Running deployment dry run against ${ORG}..."
sf project deploy start --dry-run --source-dir force-app --target-org "${ORG}"
echo "All checks passed."The if grep form matters. With set -e, a bare grep that finds nothing would stop the script, even though finding nothing is the good result.
Exercise
You resolved a conflict in a permission set by keeping both sides. grep finds no markers and xmllint --noout prints nothing, but the dry run fails with an error that a custom field cannot be found. What is the most likely cause, and what are two ways to fix it?
Show answer
The permission set now includes a fieldPermissions entry for a field that exists only on the other branch, so the field's metadata is not in the project you are validating. Either also bring in the field's metadata file from the other branch (it should already arrive with the merge if the field was added there, so check that it was not dropped or resolved away), or remove the permission entry if the field is not meant to ship yet. Then run the dry run again.
Exercise
Why does the script use if grep ...; then instead of running grep on its own line with set -e enabled?
Show answer
grep returns exit status 1 when it finds no matches. With set -e, that would stop the script, but no matches is the success case. Wrapping grep in if lets the script test the result without triggering the early exit.