Source tracking, retrieve and deploy
sfd retrieve, sfd tracking, and how sfd deploy keeps source tracking in
step. Source: src/ops/retrieve.mjs, src/ops/tracking.mjs, src/ops/deploy.mjs,
src/handlers.mjs (the project queue), src/format.mjs (summarizeChanges, summarizeRetrieve,
formatRetrieve, formatTracking), and the deploy, retrieve and tracking
adapters in src/cli/ (option checks).
Tests: test/ops-project.test.mjs, test/format.test.mjs, test/cli.test.mjs.
The Salesforce work is done by @salesforce/source-deploy-retrieve and
@salesforce/source-tracking, loaded from the sf install (see the
README). sfd only chooses the selectors, applies
the conflict rules below, and formats the result.
Retrieve
sfd retrieve # tracked remote changes (changes-only)sfd retrieve -d force-app/main # path selectorsfd retrieve -m ApexClass:Foo # metadata selector (repeatable)sfd retrieve -x manifest/package.xml-d, -m and -x are mutually exclusive; passing more than one is a usage
error (exit 2). With no selector, retrieve is “changes-only”: it pulls the
remote changes that source tracking reports, including deletions.
Source tracking can report types the Metadata API cannot retrieve, such as the
SFD DebugLevel that sfd creates for trace flags. The server
answers with an “Entity type … is not available in this api version” warning.
As in sf, sfd prints this as a warning and it does not make the retrieve fail.
If a named component fails, the retrieve still fails with exit 1.
Selector retrieves merge into the project. New components go to the project’s default package directory.
Conflicts and tracked orgs
A conflict is a component that changed both locally and in the org since the last sync.
| Org | Invocation | Result |
|---|---|---|
| Tracks source | No selector, conflicts exist | Refused: nothing written, exit 1 |
| Tracks source | Selector, conflicts among the selected components | Refused: nothing written, exit 1. Conflicts outside the selection do not block it |
| Tracks source | Any form, --ignore-conflicts |
Overwrites conflicting local files |
| Tracks source | No selector, no remote changes | Exit 0 with No remote changes to retrieve. |
| No source tracking | No selector | Usage error, exit 2 |
| No source tracking | Selector | Overwrites local files; no conflict check |
A refusal is a normal response with success: false and status: "Conflicts".
The text output names the conflicting count and the --ignore-conflicts rerun.
The JSON output lists each conflict under conflicts.
Refusals are checked before any file is written, so a refused retrieve leaves
the project unchanged. After a successful retrieve on a tracked org, sfd calls
updateTrackingFromRetrieve, so the next changes-only run does not report the
same files again.
Tracking
sfd tracking # local, remote and conflicting changessfd tracking --local # local changes and conflictssfd tracking --remote # remote changes and conflictssfd tracking --conflicts # conflicts only- Requires an org with source tracking; otherwise usage error, exit 2.
--localand--remoteare mutually exclusive.- Conflicts are always listed, except with
--conflicts, which lists only conflicts. - Paths are relative to the project directory.
- Text output groups the result under
local,remoteandconflicts, and collapses each source file with its companion-meta.xml. The JSON response keeps every file separately. - Ignored entries are dropped.
Deploy and tracking
After a successful deploy to an org that tracks source, sfd calls
updateTrackingFromDeploy, as sf project deploy start does. A check-only
deploy (--check-only) changes nothing and does not update tracking.
Serialization
Deploy, retrieve and tracking share one queue inside the daemon (projectQueue in
src/handlers.mjs). Source Deploy Retrieve and source tracking keep
process-wide state, so these operations never run concurrently in one daemon,
except for the orphaned library poll described below. Operations that do not
touch the project (query, describe, REST, Apex, test) are not queued.
When policy resolves a username, the daemon checks that username again when the
operation reaches the front of the queue. If an alias changed while the
operation waited, the operation fails with org-changed and exits 1.
SfProject.clearInstances() is only called through clearProjects(), which
throws unless the handler is running inside the queue. See
auth-cache.md.
A long deploy delays a retrieve or sfd tracking issued afterwards until it
finishes, times out, or its client disconnects. Disconnected requests waiting
in the queue are dropped before they start. On disconnect during a deploy or
retrieve wait, sfd releases the queue and logs the operation, org and job ID.
The org job is not cancelled, and source tracking is not updated by that wait.
The library poll is not cancelled. It keeps running in the background after the
queue has moved on, so it can overlap the next deploy, retrieve or tracking
call in the same project directory. A retrieve may still write files when that
poll completes. The poll ends at its own timeout, or when the daemon exits (idle,
stale, version or stop drain); if the daemon exits first, the retrieve’s files are
not written and sfd does not resume the wait. Retrieve again to pick up the
changes.
Exit codes and timeouts
| Situation | Exit |
|---|---|
| Success, including a changes-only retrieve with nothing to do | 0 |
| Deploy or retrieve failed, or a conflict refusal | 1 |
| Usage error: bad selector combination, no tracking, missing project | 2 |
| Request timed out, or the daemon is unreachable | 3 |
Deploy and retrieve wait up to 33 minutes by default; -w <minutes> changes
the wait. The client deadline is extended to that wait plus 60 seconds, and
SFD_TIMEOUT_SECONDS overrides it (see the README).
Changing this area
- Keep the conflict rules above in sync with
src/ops/retrieve.mjs. They are tested intest/ops-project.test.mjs(refusal before writes, ignore conflicts, deletions, queue sharing). - Output shape changes belong in
src/format.mjs, with cases intest/format.test.mjs. - The agent hook maps
project retrieve startbut not retrieve or deploy preview commands. See agent-hooks.md.