Sub topic 6
Declaring a dependency on another package
This chapter assumes you know how to read a MAJOR.MINOR.PATCH.BUILD version number, can edit sfdx-project.json, and have a Dev Hub with at least one package already created. It covers how one package declares that it needs another.
Why declare a dependency
A package often uses components that live in another package. A Sales App might call Apex classes that ship in a Utils package. If you build the Sales App version without telling the platform about Utils, version creation fails because the referenced components can't be found.
A declared dependency tells the build, and later the install, that Utils must be present. The platform checks it when the version is created. It checks it again when someone installs the package.
Declare it in sfdx-project.json
Dependencies go in the dependencies array of the package directory that needs them. Each entry names a package and, optionally, a version number.
{
"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"
}
}Three details matter here:
- The
packagevalue in a dependency must be an alias or name that exists inpackageAliases. Utilsis listed beforeSales App. Packages in the same project are built in the order they appear, so a dependency must come first.1.1.0.LATESTasks for the newest build of version 1.1.0. Chapter material on pinning and floating covers this choice in detail. For now, treat it as "any build of 1.1.0".
How the pieces connect
Dependencies on an installed package
A dependency doesn't have to come from your project. If Utils was created in another project, or by another team, you still declare it the same way. Add its alias to packageAliases and reference it in dependencies. The alias maps to a 0Ho package ID or, for a specific version, to a 04t subscriber package version ID.
Common mistakes
- Missing alias. A
packagename with no matching entry inpackageAliasesis rejected. - Wrong order. Listing the dependent package before its dependency in the same project can make the version build fail.
- Version that doesn't exist. Asking for
1.1.0.LATESTwhen only 1.0.0 versions exist fails, because no build matches. - Circular dependency. Utils can't depend on Sales App while Sales App depends on Utils.
Try it
Create the Utils version first, then the Sales App version. The second command only works if the first has produced a version the dependency can resolve.
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version create --package "Sales App" --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHubExercise
Your Sales App directory declares a dependency on Utils with version 1.1.0.LATEST, but version create fails. Name two things in sfdx-project.json you would check.
Show answer
First, check that Utils has an entry in packageAliases. Second, check that a Utils version 1.1.0 build exists and that Utils is listed before Sales App in packageDirectories. A missing alias, a missing version, or the wrong order each causes this failure.