Sub topic 13
Dependencies between packages and directory order
This chapter assumes you can read sfdx-project.json, know what an entry in packageDirectories looks like, and have seen the package, versionNumber, and packageAliases fields from the earlier chapters. Here you will learn how one package declares that it needs another, and why the order of the entries in packageDirectories matters.
Why dependencies exist
An unlocked package is installed as a unit. If feature-pkg contains an Apex class that calls a class in core-pkg, Salesforce must know that core-pkg has to be installed first. Without that declaration, creating a package version for feature-pkg fails, because the referenced component can't be found.
You declare the relationship in the dependencies array of the package directory that needs something. The array belongs to the dependent package, not to the package it depends on.
The dependencies array
Each entry in dependencies names a package and says which version is required. The entry for feature-pkg from the earlier example looks like this:
{
"path": "feature",
"package": "feature-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"default": false,
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]
}The package value must be a name that exists as a key in packageAliases, or the name of a package directory in the same project. The versionNumber has four parts: major, minor, patch, and build.
There are two ways to pin a version:
| Style | Example | Meaning |
|---|---|---|
| Version number with `LATEST` | `"versionNumber": "1.0.0.LATEST"` | Use the newest build of version 1.0.0 of a package from this project. |
| Version alias | `"package": "[email protected]"` | Use one exact package version. The alias maps to a `04t` subscriber version ID in `packageAliases`. |
LATEST is convenient while you develop two packages side by side, because each new build of the base package is picked up automatically. An exact version alias is the better choice for a package you don't own, since nothing changes under you.
Dependencies on packages outside the project
For a package built in another project, there is no directory to find. You add its subscriber package version ID to packageAliases under a readable alias, then refer to that alias in dependencies:
{
"packageDirectories": [
{
"path": "feature",
"package": "feature-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"default": true,
"dependencies": [
{ "package": "[email protected]" }
]
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"feature-pkg": "0Ho000000000001AAA",
"[email protected]": "04t000000000000AAA"
}
}The alias string is a convention ([email protected]). The CLI treats it as an opaque key, so what matters is that the key in dependencies matches the key in packageAliases exactly. The IDs here are placeholders.
Directory order
The order of entries in packageDirectories is meaningful. List a package before every package that depends on it. In the two-package project from the spine, core comes first and feature second, which is correct because feature-pkg depends on core-pkg.
The same rule applies to a chain. With three packages where app needs feature and feature needs core, the array order is core, feature, app.
Two details about order:
- Order is about dependencies, not about which directory is the default.
"default": truemarks the directory where the CLI puts new source by default, and only one directory can have it. It can be the first, the last, or any entry in between. - A directory with no
packagefield is plain source, not a package. It is deployed with the rest of the project but is not versioned or installed on its own. Keep it out of the middle of a dependency chain unless a package genuinely needs its contents.
A three-package example
Here is the full file for the chain above:
{
"packageDirectories": [
{
"path": "core",
"package": "core-pkg",
"versionName": "Spring release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "feature",
"package": "feature-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"default": false,
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]
},
{
"path": "app",
"package": "app-pkg",
"versionName": "Initial version",
"versionNumber": "0.1.0.NEXT",
"default": false,
"dependencies": [
{ "package": "feature-pkg", "versionNumber": "0.1.0.LATEST" }
]
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"core-pkg": "0Ho000000000000AAA",
"feature-pkg": "0Ho000000000001AAA",
"app-pkg": "0Ho000000000002AAA"
}
}Notice that app-pkg lists only feature-pkg. It does not need to list core-pkg again in this form, because installing feature-pkg already requires core-pkg. List a package directly only when your own code references it directly. If app also calls core classes, add it explicitly, so the declaration stays true if feature later drops its own dependency.
Building the versions in order
Create each package once, then build versions from the bottom of the chain up. The --path value must match a path in sfdx-project.json.
sf package create --name core-pkg --package-type Unlocked --path core --no-namespace
sf package create --name feature-pkg --package-type Unlocked --path feature --no-namespace
sf package version create --package core-pkg --installation-key-bypass --wait 10
sf package version create --package feature-pkg --installation-key-bypass --wait 10sf package create writes the new package ID into packageAliases. sf package version create also records an alias such as [email protected] for each version it builds. If feature-pkg uses 1.0.0.LATEST, the second version build resolves to the newest core-pkg build that matches 1.0.0, so build core-pkg first or the dependency resolves to an older version.
Deploying source with dependencies
Source deploys read the same packageDirectories array. sf project deploy start with no flags deploys all package directories in the project, so it relies on that order and on every referenced component being present somewhere in the deploy. To deploy one package directory, name it:
sf project deploy start --source-dir featureThis works only when everything feature references is already in the target org. In a fresh scratch org, deploy core first.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a dependency is not found or a package alias is unknown | The `package` value in `dependencies` has no matching key in `packageAliases` | Add the alias, or correct the spelling. Keys are case sensitive. |
| Version create fails with a missing component or unresolved reference | The base package's directory sits after the dependent one, or the dependency is missing | Move the base directory earlier and add the dependency entry. |
| `LATEST` resolves to an older build than you expected | The base package version was not rebuilt after your change | Run `sf package version create` for the base package first. |
| Error about a circular dependency | Package A depends on B and B depends on A | Move the shared components into a third package that both depend on. |
| Dependency version not found | The `versionNumber` names a version that was never built | Build it, or lower the number to one that exists. |
Exercise
Your project lists feature (which depends on core-pkg) first in packageDirectories, then core. Package version creation for feature-pkg fails with an unresolved reference. What are the two things to check, and what is the fix?
Show answer
Check that feature has a dependencies entry whose package value is core-pkg and that core-pkg exists as a key in packageAliases. Then check the directory order. core must come before feature in packageDirectories. Move the core entry above feature, then build a core-pkg version first so that 1.0.0.LATEST resolves, and build feature-pkg after it.
Exercise
What is the difference between { "package": "core-pkg", "versionNumber": "1.0.0.LATEST" } and { "package": "[email protected]" } as dependency entries?
Show answer
The first points to a package built in this project and resolves to the newest build of version 1.0.0 at the time you create a version. The second points to one exact, already built version of a package, identified through its alias in packageAliases, so it never changes unless you edit the alias or the entry.