Sub topic 9
How the project drives deploy and retrieve commands
This chapter assumes you have generated a DX project, you know where sfdx-project.json lives, and you have an authorized org or a scratch org. It shows how the project file decides what sf project deploy start and sf project retrieve start send and receive.
What the CLI reads from the project
When you run a deploy or retrieve command, the CLI looks for sfdx-project.json in the current directory or a parent directory. It then combines four inputs:
packageDirectoriesgives the folders to scan for source.sourceApiVersionsets the API version used for the operation..forceignoreremoves files that match its patterns.- The target org, either the default or the one you pass with
--target-org, receives or supplies the metadata.
The CLI does not look at folders outside the listed package directories. A file in scripts/ or in an unlisted folder is never deployed.
Deploying
Run the command from inside the project:
sf project deploy start --target-org my-scratchWith no other flags, the command deploys every package directory. To limit the scope, use one of these flags:
sf project deploy start --source-dir force-app/main/default/classes
sf project deploy start --metadata ApexClass:MyController
sf project deploy start --manifest manifest/package.xmlEach path you give to --source-dir must sit inside a package directory. A path outside them fails with an error.
Before deploying, run a preview to see what would change:
sf project deploy preview --target-org my-scratchThe preview lists files to deploy, files to delete, and conflicts, and it changes nothing in the org.
Retrieving
Retrieve works in the other direction. The CLI downloads metadata in metadata format and converts it to source format. It then writes the files into your package directories.
sf project retrieve start --target-org my-scratch
sf project retrieve start --metadata CustomObject:AccountNew files land in the default package directory, the one marked "default": true. Files that already exist in another package directory are updated where they are.
Source tracking
Scratch orgs and some sandboxes support source tracking. The CLI records which components changed locally and which changed in the org. Running deploy start or retrieve start with no flags then moves only the changes in the right direction.
If the same component changed on both sides, the CLI reports a conflict and stops. Resolve it with --ignore-conflicts to overwrite the org, or retrieve first to overwrite your local file. Pick the direction deliberately, since both overwrite work.
The role of `.forceignore`
A pattern in .forceignore applies to both directions. A file ignored locally is never sent, and a retrieved component that matches a pattern is not written to disk. This is why a retrieve can appear to "lose" a profile file if **/profiles/** is ignored.
A short worked example
sf project generate --name demo
cd demo
sf org create scratch --definition-file config/project-scratch-def.json --alias demo-scratch --set-default
sf project deploy start
sf project retrieve startThe scratch org is created from the definition file, and the first deploy sends everything under force-app. The retrieve then pulls any changes made in the org, such as a field added through Setup, into force-app/main/default.
Common errors
| Symptom | Likely cause |
|---|---|
| "No source-backed components present" | The package directory path is wrong or the folder is empty |
| File never deploys | A `.forceignore` pattern matches it |
| Retrieved file lands in an unexpected folder | The component is new, so it goes to the default directory |
| Conflicts reported | The component changed in both the org and locally |
Exercise
You added a new Apex class in force-app/main/default/classes and run sf project deploy start against a production org. Nothing is deployed. Why might this happen, and what should you do?
Show answer
Production orgs do not support source tracking, so a command with no scope flags has no change list to send. Name the scope, for example sf project deploy start --source-dir force-app/main/default/classes. If the file still does not appear, check that .forceignore does not match it.