Sub topic 12
Declaring dependencies between packages
This chapter assumes you can create an unlocked package and a package version, and that you know the structure of sfdx-project.json. It covers how one package declares that it needs another, how the CLI resolves that declaration, and the errors you are likely to hit.
Why dependencies exist
A package version is a snapshot of metadata. If a Sales package contains a Lightning page that shows a custom field from Core, the field must exist before Sales is installed. A declared dependency tells the platform this requirement, so it can check it at build time and at install time.
Without a declared dependency, version creation fails because the compiler cannot resolve the reference. With one, the build succeeds and the install is blocked if the target org lacks the required version of Core.
Declaring a dependency
Dependencies live in the dependencies array of a package directory in sfdx-project.json. Each entry names a package and a version.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/sales",
"package": "Sales",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "Core", "versionNumber": "1.0.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Three points about this file:
Salesdepends onCoreat1.0.0.LATEST. The build resolves this to the newest Core build with major.minor.patch of 1.0.0.Coreis listed beforeSales. Order matters because the CLI processes package directories in the order listed, and a package must appear after the packages it depends on.- The dependency names the package, not a directory. The name must match a
packageAliasesentry created bysf package create.
Ways to reference a version
| Form | Example | Meaning |
|---|---|---|
| `.LATEST` | `1.0.0.LATEST` | Newest build of that major.minor.patch. Can be a beta. |
| Explicit build | `1.0.0.3` | Exactly that build number. |
| Alias with version ID | `"package": "[email protected]"` | Exactly that version, by alias. |
| Omitted version | `{ "package": "Core" }` | Only allowed when Core is in the same project and the build uses its current source. |
.LATEST is convenient during development and unpredictable in release pipelines. Two builds of Sales made a day apart can bind to different Core builds. For release builds, pin an explicit build number.
Released versions only
When you build a package version, you can ask for released versions only with --skip-validation off and the default behavior of resolving to the latest released build when you use the RELEASED keyword. For example, 1.0.0.RELEASED resolves only to a promoted Core version. Use it when Sales must never depend on an unpromoted beta.
Resolution at build time
When you run sf package version create --package Sales, the CLI does the following.
- It reads the dependency list for Sales.
- For each entry it asks the Dev Hub for the matching Core version.
- It stores the resolved Core version ID in the new Sales version.
- It installs those dependencies in the scratch org used for validation, in order, then installs the Sales source on top.
sf package version create --package Sales --installation-key-bypass --code-coverage --wait 20 --target-dev-hub MyDevHubIf no Core version matches, the command fails before any validation. The usual fix is to build Core first.
Dependency order in a pipeline
Install order matters in the target org too. Installing Sales when the required Core version is absent fails. With a recent CLI, sf package install of Sales can install missing dependencies when you pass --security-type and the dependencies are available in the Dev Hub's records, but do not rely on this for production. Install each package explicitly, in dependency order, so the log shows what happened.
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20Remember that a version must be promoted before it can be installed in production. Because Sales records a specific Core version, you must promote Core first. Promoting Sales while its Core dependency is still a beta is rejected.
Checking what a version depends on
Use sf package version report to see the dependencies recorded in a version.
sf package version report --package "[email protected]" --verbose --target-dev-hub MyDevHubThe verbose output includes the dependency list with resolved version IDs. Compare this to your intent when a build behaves differently than expected.
Common errors
- "No version found" or an unresolved dependency. The version number does not match any Core build. Check the major, minor and patch parts, and confirm the build finished.
- A circular dependency. Core depends on Sales and Sales depends on Core. The platform does not allow cycles. Move the shared metadata into a third package that both depend on, or merge the two.
- Missing metadata that is not declared. Sales references a Core Apex class but lists no dependency. The build fails with an unresolved symbol. Add the dependency.
- Wrong directory order. A dependent package listed before its dependency can break source resolution in the validation org. Reorder the entries.
- Pinned to an old build. Sales keeps using
[email protected]after Core gains a field Sales now needs. Bump the pinned build, then create a new Sales version.
Design guidance
Keep the dependency graph shallow and one-directional. A common layout is a base package with shared objects and utilities, and several feature packages that depend on the base but not on each other. Each extra level adds build time, because validation must install every upstream package in a scratch org. Long chains also make a hotfix to the base package ripple through every package above it, because each dependent needs a new version to adopt the fix.
Exercise
Sales declares a dependency on Core at 1.0.0.LATEST. Core's latest 1.0.0 build is a beta that was never promoted. You build a Sales version and try to install it in production. What happens, and how would you prevent the problem?
Show answer
Production installs only released versions, and a version that depends on an unpromoted beta cannot be promoted, so the install fails. Promote the Core build first, then rebuild Sales so it resolves to the released build. To prevent the problem, pin an explicit released build number or use 1.0.0.RELEASED in release pipelines.
Exercise
Core and Sales each reference metadata in the other. The build for either package fails. What is the cause and the usual fix?
Show answer
The two packages form a circular dependency, which is not allowed. Move the shared metadata into a third package that both depend on, or combine Core and Sales into one package.