Sub topic 9
Depending on other packages and building extensions
This chapter assumes you can already build a package version with sf package version create and read sfdx-project.json. It covers how one package depends on another, how the dependencies entry resolves versions, and how to build an extension package on top of a base package.
Why packages depend on each other
A dependency means your package needs components from another package to work. Two common cases:
- Your app uses a shared library package that your company also uses in other apps.
- You ship an extension package that adds features to a base package, such as a vertical add-on for a core app.
Splitting code into packages keeps each unit smaller and lets each one release on its own schedule. The cost is that every version you build now depends on a specific version of another package, so you need to control which one.
Declaring a dependency
You declare dependencies in the package directory entry of sfdx-project.json. Each dependency names a package and, usually, 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 package value is a package name or alias defined in the packageAliases section of the same file. The versionNumber follows the major.minor.patch.build format from the version numbering chapter, and its build segment takes the keyword LATEST.
Pinning versus floating
You have two ways to pick the dependency version:
| Style | Example | Behavior |
|---|---|---|
| Floating | `"versionNumber": "1.2.0.LATEST"` | At build time, resolves to the newest build of 1.2.0 |
| Pinned | `"package": "[email protected]"` | Always uses that exact version |
Floating is convenient during development because you pick up fixes in the dependency automatically. Pinned is safer for release builds because the same source always produces the same dependency set. Many teams float on feature branches and pin on release branches.
The version you build records the exact dependency version it resolved to. Changing LATEST later does not change a version that already exists.
Dependency rules to remember
- A dependency must exist as a package version before you can build a package that uses it. Build and, when needed, promote the base package first.
- To promote a version, every package it depends on must already be released. A beta dependency is fine for a beta build, but promotion fails.
- Subscribers must install the dependency before, or together with, your package. Install the base package first to be safe, and confirm what your CLI and API version do automatically.
- A dependency in a different Dev Hub org is referenced by its subscriber package version ID, the
04tID, instead of an alias. - Your dependencies are part of what you ship. Choose the lowest version you actually need, because a high minimum forces subscribers to upgrade the dependency first.
Multiple packages in one project
One project can hold several packages. Each gets its own entry in packageDirectories. Order matters: a package must be listed after the packages it depends on, because the CLI processes the directories in order.
{
"packageDirectories": [
{
"path": "base",
"package": "AcmeBase",
"versionName": "Base 1.0",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE"
},
{
"path": "extension",
"package": "AcmeHealthExt",
"versionName": "Healthcare extension",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE",
"default": true,
"dependencies": [
{
"package": "AcmeBase",
"versionNumber": "1.0.0.LATEST"
}
]
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Both packages here use the acme namespace. Managed 2GP packages can share a namespace, which makes them behave like parts of one product. Components in packages that share a namespace can reference each other directly. If you use separate namespaces instead, you must expose the base components deliberately, as described below.
Exposing code to an extension
An extension package in another namespace can only call what the base package makes available. For Apex, you have two options:
- Mark classes and methods
global. They become part of the public API of your package, and the managed-package rules then limit how you can change them. - Use the
@NamespaceAccessibleannotation onpubliccode. Other packages can call it, but it stays out of the subscriber-facing API.
@NamespaceAccessible
public with sharing class PricingService {
@NamespaceAccessible
public static Decimal applyDiscount(Decimal amount, Decimal percent) {
return amount - (amount * percent / 100);
}
}An extension in another namespace can then call it with the base namespace prefix. Here base stands for the base package's namespace:
Decimal price = base.PricingService.applyDiscount(200, 10);This works only if the extension declares the base package as a dependency, so the code compiles at build time.
How the pieces fit
The diagram shows the order of operations when a base package and an extension are released separately.
Building and installing
Build the extension the same way as any other version. The CLI installs the dependency in the validation org before it tests your code.
sf package version create --package AcmeHealthExt --installation-key-bypass --code-coverage --wait 20 --target-dev-hub devhubYour code coverage applies to your own package only. It does not include the dependency's Apex. To see the resolved dependencies of a built version, report on it:
sf package version report --package [email protected] --verbose --target-dev-hub devhubInstall the base package, then the extension, in the target org:
sf package install --package [email protected] --target-org my-sandbox --wait 10
sf package install --package [email protected] --target-org my-sandbox --wait 10Common errors
- Dependency version not found. The version you named has not been built, or it exists only in a different Dev Hub. Check
sf package version listand the alias spelling. - Promotion fails because a dependency is a beta. Promote the dependency first, then rebuild your package against the released version.
- Compile errors for base classes in an extension. The base member is not
globalor@NamespaceAccessible, or the dependency entry is missing. - Wrong directory order in a multi-package project. The dependent package is listed before the package it needs.
- Upgrade blocked by a dependency. A new version of your package requires a newer base version than the subscriber has installed. Upgrade the base first.
Exercise
Your extension package depends on [email protected], which is still a beta. You build a beta of the extension successfully, but sf package version promote fails. What is the cause, and what do you do?
Show answer
A package version can be promoted only if all its dependencies are released. Promote [email protected] first (after it passes testing), then rebuild the extension against that released version, and promote the new extension version.
Exercise
A base package in the base namespace has a public Apex class that an extension in the ext namespace needs to call. What change does the base package need, and why can't you just fix it in the extension?
Show answer
The base package must expose the class, either by making it global or by adding @NamespaceAccessible to the class and the members the extension calls. This has to be done in the base package and released as a new version, because only the owning package controls what it exposes. The extension then raises its dependency to that new base version.