Sub topic 9
How the Project Drives Deploy and Retrieve Commands
This chapter assumes you can run sf commands, have an authorized org, and have read the earlier chapters on the project layout, packageDirectories, project-level settings, and .forceignore. Here you will see how those pieces combine when you run sf project deploy start and sf project retrieve start.
The working model
The CLI does not look at your whole disk when you deploy. It follows a fixed sequence:
- It walks up from the current directory until it finds
sfdx-project.json. That folder is the project root. - It reads
packageDirectoriesto learn which folders hold source. - It scans those folders and removes anything matched by
.forceignore. - It converts the remaining source-format files into the metadata the org's API expects.
- It sends the request using the target org's connection, and reports results.
The file sfdx-project.json supplies the scope (which folders) and some defaults (the API version). The org alias supplies the destination. .forceignore trims the scope.
Project root and target org
The CLI finds the project by searching the current directory and its parents. Running a command from force-app/main/default/classes works the same as running it from the root. Running it outside any project makes project-aware commands fail, because there is no packageDirectories to read.
The target org comes from one of two places:
- The
--target-orgflag (short form-o), which takes an alias or username. - The default target org saved in the project's local configuration.
You can set the default once and then omit the flag:
sf config set target-org my-scratch
sf project deploy start
sf project deploy start --target-org my-sandboxThe first deploy uses the default. The second overrides it for one command. Setting target-org without --global stores it in the project, so another project can use a different default.
Deploying: choosing what to send
With no flags, sf project deploy start deploys every file in every package directory. Narrow it with flags when you do not want that.
| Flag | What it selects |
|---|---|
| `--source-dir` (`-d`) | A folder or file path inside a package directory |
| `--metadata` (`-m`) | Metadata by type and name, such as `ApexClass:MyClass` |
| `--manifest` (`-x`) | The contents of a `package.xml` file |
| `--test-level` | Which Apex tests run during the deploy |
| `--dry-run` | Validate the deploy without saving changes |
Some examples:
sf project deploy start --source-dir force-app/main/default/classes
sf project deploy start --metadata ApexClass:AccountService
sf project deploy start --metadata "ApexClass:Account*"
sf project deploy start --dry-run --test-level RunLocalTestsEach path you pass to --source-dir must sit inside a directory listed in packageDirectories. A path outside them is rejected, even if the files are valid metadata. This is the main way a wrong path value in sfdx-project.json shows up in practice.
Retrieving: pulling org changes into source
sf project retrieve start does the reverse. It asks the org for metadata, converts it to source format, and writes it into your package directories.
sf project retrieve start --metadata ApexClass:AccountService
sf project retrieve start --source-dir force-app/main/default/objects
sf project retrieve start --manifest manifest/package.xmlWhere do retrieved files land? If a component already exists in your project, the CLI overwrites it in place, whichever package directory holds it. If it is new, the CLI writes it into the default package directory, which is the one marked "default": true. This is why that flag matters: with several directories, a new component goes to the default one unless you move it.
Retrieved files are also filtered by .forceignore. A retrieved profile that matches **/profiles/** is not written to disk. If a retrieve seems to return nothing, check the ignore file before assuming the org lacks the component.
Source tracking
Scratch orgs and some sandboxes have source tracking enabled. The org records which components changed there, and the CLI records which changed locally. Two commands use this:
sf project deploy preview
sf project deploy start
sf project retrieve preview
sf project retrieve startWith tracking on, deploy start with no flags sends only the local changes. deploy preview lists what would be deployed, what would be ignored, and any conflicts, without changing anything. retrieve start with no flags pulls only org-side changes. If both sides changed the same component, the command stops with a conflict error. You then choose which side wins:
sf project deploy start --ignore-conflicts
sf project retrieve start --ignore-conflicts--ignore-conflicts overwrites the other side without asking. Run the preview command first so you know what you will overwrite.
Orgs without tracking, such as production, have no change history for the CLI to use. Name what you want with --source-dir, --metadata, or --manifest. If you give none of them, the CLI deploys everything in the package directories.
Deploy and retrieve in a scratch org workflow
A full cycle combines the scratch org definition file from the earlier chapter with the commands above:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start
# edit in the org with Setup, then:
sf project retrieve preview
sf project retrieve startThe scratch org is created from the definition file. The first deploy sends the whole project, since the new org has nothing yet. After you change something in Setup, retrieve preview shows the changes the org has recorded, and retrieve start writes them into the default package directory, or over the existing files if the components are already in the project.
How project settings change the result
Several sfdx-project.json values affect these commands without appearing in them.
sourceApiVersionsets the API version used when the CLI converts source and calls the org. If it is higher than the org supports, the deploy fails. If it is lower, newer metadata fields may be dropped or rejected. You can override it for one command with--api-versionon retrieve, or withsf config set org-api-version.namespaceis applied when you deploy to an org that has the namespace registered. For a project that is not a namespaced package, leave it as an empty string.sfdcLoginUrlis used bysf org login webwhen you authorize an org. It does not affect deploy or retrieve directly, but a wrong value is a common reason an org cannot be authorized in the first place.- Package directory order matters. The CLI deploys the directories in the order listed. A directory that depends on components from an earlier one needs to come after it.
Reading the results
A deploy reports per-component results. Read the table at the end of the output. Each row has a state such as Created, Changed, Unchanged, or Failed, with a file path and, for failures, a problem message.
If you started a long deploy and lost the terminal, resume it with the job ID:
sf project deploy report --job-id 0Af000000000000AAAUse --wait to make the command block for a number of minutes, or --async to return right away and check later.
sf project deploy start --wait 20
sf project deploy start --asyncCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "This directory is not part of a Salesforce DX project" or similar | Command run outside the project tree | `cd` into the project, or check that `sfdx-project.json` exists |
| Path is not in a package directory | `--source-dir` points outside `packageDirectories` | Fix the path or add the folder to `packageDirectories` |
| Nothing deployed or retrieved | `.forceignore` matches the files, or tracking sees no changes | Run a preview command and review `.forceignore` |
| Conflict error | Same component changed locally and in the org | Compare, then use `--ignore-conflicts` on the side that should win |
| API version error | `sourceApiVersion` is newer than the org | Lower it, or use an org on a newer release |
| Dependent component not found | A directory is listed after the one that needs it | Reorder `packageDirectories` |
Exercise
You run sf project retrieve start --metadata ApexClass:NewHelper in a project with two package directories, core (not default) and feature (marked "default": true). NewHelper is not yet in the project. Where is the file written, and what could stop it from being written at all?
Show answer
It is written under the feature directory, because new components go to the default package directory. It would not be written if a .forceignore pattern matches the file path.
Exercise
You are in a production org without source tracking and want to deploy only one Apex class. Which flag do you use, and what happens if you pass no selection flags?
Show answer
Use --metadata ApexClass:ClassName or --source-dir pointing at the class file. With no selection flags, the CLI deploys everything in all package directories.