Sub topic 6
Declaring a dependency on another package
This chapter assumes you can read sfdx-project.json, run sf commands against a Dev Hub, and know what a package version number looks like (MAJOR.MINOR.PATCH.BUILD). It covers how one package states that it needs another, and how Salesforce resolves that statement.
What a dependency means
A dependency says that your package's metadata references components defined in another package. For example, a Sales App Apex class might call a utility class in a Utils package. If Sales App is installed where Utils is missing, the reference breaks.
A declared dependency has two effects:
- When you create a package version, Salesforce validates your metadata against the dependency version it resolves.
- When someone installs your version, the target org must already contain a compatible version of the dependency. Installation of your package fails if it does not.
The dependency is part of the package version that you create. Changing sfdx-project.json later does not alter versions that already exist.
The `dependencies` array
Dependencies live inside a package directory entry in sfdx-project.json. Each entry names a package and, usually, the version you need.
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionNumber": "1.1.0.NEXT",
"default": false
},
{
"path": "app",
"package": "Sales App",
"versionName": "Sales App 2.0",
"versionNumber": "2.0.0.NEXT",
"default": true,
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"Sales App": "0Ho000000000002AAA"
}
}Points to notice:
- The
packagevalue in a dependency is a name or an alias frompackageAliases. It must resolve to a package ID (0Ho...) or a package version ID (04t...). Utilsappears beforeSales AppinpackageDirectories. For packages in the same project, the order matters: a dependency must be listed before the package that uses it.versionNumberin the dependency uses the four-part format. Here it asks for any build of 1.1.0, andLATESTselects the newest one.- The dependency is on a package, not on a directory.
Sales Appcannot reach into theutilfolder's source. It sees Utils only through the package version it resolves.
Dependencies inside and outside the project
Utils above is built from the same project. You can also depend on a package that someone else owns, or one you build in another repository. In that case you have no source for it, so you reference a specific package version through an alias.
{
"path": "app",
"package": "Sales App",
"versionNumber": "2.0.0.NEXT",
"dependencies": [
{ "package": "[email protected]" }
]
}The alias then goes in packageAliases:
{
"packageAliases": {
"Sales App": "0Ho000000000002AAA",
"[email protected]": "04t000000000001AAA"
}
}The 04t ID identifies one immutable package version, which is why no versionNumber is needed on the dependency. The alias naming convention [email protected] is what the CLI writes when you create a version. Using the same format keeps your file readable.
Managed package dependencies
A dependency on a managed package works the same way, with the version's 04t ID. Make sure the package is installed in the Dev Hub's scratch orgs and in any org where you install your own package. Salesforce checks that the dependency's namespace is available when it validates your version.
How resolution works
When you run sf package version create, the CLI reads the dependencies for the package directory and sends them with the request. Salesforce resolves each one:
- If the entry is an alias for a
04tID, that exact version is used. - If the entry is a package name with a
versionNumber, Salesforce matchesMAJOR.MINOR.PATCHand picks the build according to the keyword in the build position. - The resolved version ID is stored inside the new package version.
The diagram shows the flow for the Sales App example.
Two consequences follow. A floating dependency (LATEST) is frozen at creation time, so the same package version always installs against the same dependency version. Also, the same sfdx-project.json can resolve to different dependencies on different days, because newer Utils builds may exist. Pin with a 04t alias when you need a reproducible build.
Beta and released dependencies
Package versions are either beta or released. The version of a dependency affects what you can do with your own version.
- A beta version of your package can depend on a beta or a released version.
- You cannot promote a package version to released if any of its dependencies is still beta. Promote the dependency first.
- Released versions are the only ones you can install in production orgs.
The promotion order is therefore the dependency order: Utils first, then Sales App.
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHub
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 install --package "[email protected]" --target-org myOrg --wait 10When LATEST resolves to a beta Utils build, Sales App can still be built as beta. Promoting Sales App then fails until you either promote that Utils version or change the dependency to a released one. The RELEASED build keyword (for example 1.1.0.RELEASED) selects only the newest released build, which avoids this trap.
Choosing pinned or floating
| Approach | Example | Good for | Risk |
|---|---|---|---|
| Alias to `04t` ID | `[email protected]` | Reproducible builds, external packages | You must update the alias to take fixes |
| `.LATEST` | `1.1.0.LATEST` | Packages in the same project under active development | Resolves to a beta build you cannot promote with |
| `.RELEASED` | `1.1.0.RELEASED` | Taking only stable builds | Fails to resolve if no build has been released |
A common pattern is .LATEST during development in a scratch org and a pinned alias for builds you intend to promote.
Rules of thumb
- Keep the dependency graph acyclic. Two packages cannot depend on each other.
- Depend on the lowest version that has what you need. A higher minimum forces customers to upgrade the dependency sooner.
- Dependencies are transitive. If Utils depends on Core, installing Sales App also requires Core, so list the full chain consistently in your project.
- One dependency entry applies to one package directory only. Repeat it for each package that needs it.
Common mistakes
- Wrong order in
packageDirectories. Listing Sales App before Utils produces an error when the CLI resolves the project. Reorder the entries. - Missing alias. A dependency name with no matching alias, and no package in the project, cannot be turned into an ID. Add the alias.
- Version that does not exist. Asking for
1.2.0.LATESTwhen only 1.1 builds exist fails resolution. Check withsf package version list. - Beta dependency at promotion time. See above. Promote the dependency first.
Exercise
Sales App declares the dependency { "package": "Utils", "versionNumber": "1.1.0.LATEST" }. Utils 1.1.0-3 exists as a beta version and Utils 1.1.0-2 is released. You create Sales App 2.0.0-1 and try to promote it. What happens, and what are two ways to fix it?
Show answer
LATEST resolves to the newest build, 1.1.0-3, which is beta. Salesforce refuses to promote Sales App while a dependency is beta. Fix it by promoting Utils 1.1.0-3 first and then promoting Sales App. Alternatively, change the dependency to 1.1.0.RELEASED or pin the alias [email protected], then create a new Sales App version and promote that.