Sub topic 9
Depending on Other Packages and Building Extensions
This chapter assumes you can already create a managed package and build a version with sf package version create. It covers how one package depends on another, and how an extension package adds to a base package without modifying it.
Why packages depend on each other
A package depends on another when its metadata references components that live in the other package. Examples are a custom field, an Apex class, or a Lightning component. Second-generation packaging (2GP) records these dependencies in sfdx-project.json. The Dev Hub then checks them when you build a version.
Two common patterns use dependencies:
- Shared base. A common package holds shared objects and code, and several apps depend on it.
- Extension. A separate package adds features to a base managed package, often with its own namespace.
Declaring a dependency
Add a dependencies array to the package directory entry. Each item names a package and, for 2GP packages, a version number.
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Spring update",
"versionNumber": "1.1.0.NEXT",
"ancestorVersion": "HIGHEST",
"dependencies": [
{
"package": "BasePackage",
"versionNumber": "1.2.0.LATEST"
}
]
}The LATEST keyword in 1.2.0.LATEST resolves to the newest build of version 1.2.0 when you create your version. After that, the version records the exact build it resolved to. Later builds of the base do not change it.
Dependency rules
- The dependency must be a package version, not just a package. Without a version number, the build fails.
- A dependency must be released before you can promote a version that uses it. Beta dependencies are only for testing.
- Subscribers must install the dependency first, or install both together. The install command accepts an alias such as
[email protected]. - The order in the
dependenciesarray matters when you have several. Put base packages before packages that rely on them.
Building an extension package
An extension is a managed package whose components reference a base managed package. The base package owner decides what the extension may touch. Only components marked global in Apex, or exposed through the base package's public API, are visible. Custom fields on base objects can usually be referenced.
For an extension, the steps are:
- Install the released base package version in your scratch org.
- Create the extension package with
sf package create. - List the base package in
dependencies. - Build and test the extension version.
The extension gets its own version history, ancestry, and release schedule.
<br>
Common errors
| Symptom | Likely cause |
|---|---|
| Build fails: dependency not found | The package alias or version number is missing from `sfdx-project.json`. |
| Promotion fails | A dependency is still a beta version. |
| Install fails in a subscriber org | The dependency was not installed first. |
| Extension cannot see a class | The class in the base package is not `global`. |
Exercise
You build AcmeApp 1.1.0 with a dependency on BasePackage 1.2.0.LATEST, and the base's newest build is a beta. What happens when you try to promote AcmeApp 1.1.0?
Show answer
Promotion fails, because a released version cannot depend on a beta version. Promote the base package version first, then rebuild or promote AcmeApp.