5

Although we use git repos to track all our projects, I oftentimes want to do another check against a production file before I overwrite it with an sfdx force:source:deploy command (because as much as a git repo is kept up-to-date, you never know what renegade changes have been made to Production...)

Is there a simple sfdx workflow or command to bring in that file and compare against my local project version? Context: I use VS Code and git for day-to-day development.

9

VS Code has an option to detect conflicts on deployment for non-source-tracked orgs. Go to Settings > Salesforce Feature Previews > Salesforcedx-vscode-core: Detect Conflicts At Sync. Note that this is still a limited feature, so some caution is advised.

In the interim, if I want to check for differences, I usually do:

git checkout -b temp-branch
git add force-app
git commit -m "temp commit for compare"
sfdx force:source:retrieve -p force-app
git diff

This gives me a line-by-line analysis of everything. I can also use --name-only just to get a feel for which files are affected.

1
  • Just a simple git checkout -b prod-20200729 and sfdx force:source:retrieve -p force-app -u MyProductionOrg did the trick very nicely. Thank you! Jul 30 '20 at 0:15
5

Have you tried using the Salesforce official Diff Feature on the Extension Pack for Salesforce

https://developer.salesforce.com/tools/vscode/en/user-guide/source-diff/

From the terminal, run sfdx plugins:install @salesforce/sfdx-diff.

After the installation is complete, @salesforce/sfdx-diff appears in the list of installed plugins when running sfdx plugins.

Use the vscode menu to take the diff

enter image description here

This requires enabling the preview flag from your vscode settings

enter image description here

3
  • 1
    Wow, not sure why this plugin is not part of the normal sfdx package, nice find! This is really nifty, but oftentimes my default org is set to a sandbox, but want to do the diff against the Production org. Changing the default org is quick enough in VS code though, will give it a try Jul 27 '20 at 22:59
  • Worth noting - the underlining sfdx command only works when right-clicking the file and using the menu dropdown as in your screenshot. If you try to do the command line: sfdx force:source:diff -p force-app/main/default/classes/MyController.cls -u MyProductionOrg, you get the following error message (as of Summer '20): "This command is currently in a beta program for usage with the Salesforce Extensions for VSCode." Jul 27 '20 at 23:01
  • @BrianMiller you can use the --json flag in your command to output the file locations that you can then diff. See my separate answer.
    – bbbco
    Nov 4 '20 at 3:53
1

After installing the sfdx:source:diff tool from @Mohith Shrivastava's response, you can use the --json flag to return the locations of the remote and local files that VS Code uses to diff. You can then pass those files to git diff to see the diff in the command line.

Here is the whole command:

SFDX_DIFF_JSON=$(sfdx force:source:diff -p force-app/main/default/classes/MyController.cls -u MyProductionOrg --json)
git diff $(echo ${SFDX_DIFF_JSON} | jq '.result.remote' -r) $(echo ${SFDX_DIFF_JSON} | jq '.result.local' -r)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.