Sub topic 10
Getting the order right: packages and dependencies
This chapter assumes you can read sfdx-project.json, run sf commands, and know what a Dev Hub is. It covers why order matters and how to get it right when you create, install, and deploy packages that depend on each other.
Why order matters
A package that depends on another package cannot be installed until the package it needs is already in the target org. Salesforce checks this at install time. If the dependency is missing or at too low a version, the install fails.
The same applies when you build versions. A dependent package version can only be created once the version it points to exists.
Order in sfdx-project.json
List packages in packageDirectories so that each package comes after every package it depends on. The CLI uses this order when it deploys source to a scratch org. A dependency listed after its dependent can fail because the metadata it needs is not there yet.
{
"packageDirectories": [
{ "path": "util", "package": "Utils", "versionNumber": "1.1.0.NEXT", "default": false },
{
"path": "app",
"package": "Sales App",
"versionNumber": "2.0.0.NEXT",
"default": true,
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
}
],
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"Sales App": "0Ho000000000002AAA"
}
}Here Utils comes first because Sales App depends on it.
The build and install sequence
For packages in the same project, work from the bottom of the dependency chain upward.
The commands follow the same sequence:
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version promote --package "[email protected]" --target-dev-hub myDevHub
sf package version create --package "Sales App" --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package install --package "[email protected]" --target-org myOrg --wait 10
sf package install --package "Sales [email protected]" --target-org myOrg --wait 10What the dependency version decides
The version you declare controls what must already be installed.
| Declaration | What the org needs |
|---|---|
| `1.1.0.LATEST` | Utils 1.1.0 or later in the 1.1.0 line, resolved to the newest build when you create the version |
| `[email protected]` alias | That exact version, identified by its `04t` ID |
LATEST is resolved when you create the dependent version, not when you install it. After that, the dependent version records the specific version it needs. A beta dependency can be used by a beta dependent version, but you can only promote the dependent version after its dependencies are released. Promote the dependency first.
Installing several packages at once
If you install packages one by one, install the dependency first. You can also list dependencies in sfdx-project.json and let the CLI resolve them with sf package install for the top package in a scripted pipeline. Do not rely on this. A script that installs each package explicitly in order is easier to debug.
When you install a package into a scratch org during development, sf project deploy start pushes source in the order of packageDirectories. Install released dependencies as packages and keep only your own work as source.
Common order mistakes
- Listing the dependent package before its dependency in
packageDirectories. - Promoting the dependent version before its dependency is released.
- Installing the dependent package into an org that lacks the dependency.
- Creating a dependent version while the dependency's alias points to an old version.
Exercise
You have packages Core, Billing (depends on Core), and Portal (depends on Billing). In what order should they appear in packageDirectories, and in what order do you install them into a new org?
Show answer
Core, Billing, Portal in both cases. Each package must come after the package it depends on, in the project file and at install time.