Sub topic 7
Pinning a version or floating with LATEST
This chapter assumes you can read sfdx-project.json, run sf commands against a Dev Hub, and read a four-part version number (MAJOR.MINOR.PATCH.BUILD). It covers one decision you make for every dependency: should the dependent package be locked to one exact version, or should it follow the newest build?
The two ways to point at a dependency
A dependency entry in sfdx-project.json names the package it needs and says which version of it to use. There are two styles.
- Floating. You give a version number that ends in the
LATESTkeyword, such as1.1.0.LATEST. When you create a new version of the dependent package, the CLI resolvesLATESTto the highest build number of the dependency that matches1.1.0. - Pinned. You give an alias that maps a package name and version to one specific subscriber package version ID, which starts with
04t. That ID identifies exactly one build, so it never changes.
The two styles solve different problems. Floating keeps related packages moving together. Pinning makes a build reproducible.
Floating with LATEST
Here is the project file from the chapter on declaring dependencies. The Sales App package depends on Utils.
{
"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"
}
}The dependency says "Utils, major 1, minor 1, patch 0, any build". Suppose you have built Utils 1.1.0-1, 1.1.0-2, and 1.1.0-3. When you create a new Sales App version, LATEST resolves to build 3. The resolved build is then recorded in the new Sales App version. It is not looked up again at install time.
This last point is easy to miss. LATEST is evaluated once, when you run sf package version create. Two Sales App versions created a week apart can depend on different Utils builds even though the project file never changed.
What LATEST does not do
- It does not change the major, minor, or patch numbers.
1.1.0.LATESTwill never resolve to1.2.0.1. To move to a new minor version you edit the file. - It does not update versions you have already created. Existing package versions keep the dependency they were built with.
- It does not decide whether the build is a beta or a released version. That depends on how you build, and on the promotion rule below.
Pinning with an alias
To pin, add an alias for the exact version and use that alias as the package name.
{
"path": "app",
"package": "Sales App",
"versionNumber": "2.0.0.NEXT",
"dependencies": [
{ "package": "[email protected]" }
]
}The alias lives in the packageAliases section, mapped to the 04t ID:
{
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"Sales App": "0Ho000000000002AAA",
"[email protected]": "04t000000000001AAA"
}
}The CLI adds an alias such as [email protected] automatically when you create a package version, so you rarely type the ID by hand. You can list the available versions and their IDs with:
sf package version list --packages Utils --target-dev-hub myDevHubPinning also works for packages that you do not own. If a vendor publishes a managed package, you add its 04t ID to packageAliases under a name you choose, and then depend on that alias.
Comparing the two styles
| Question | `1.1.0.LATEST` | `[email protected]` |
|---|---|---|
| What does it resolve to? | Highest matching build when you create the dependent version | Exactly one build, always |
| Who must change the file to get a newer Utils build? | Nobody, rebuild the dependent package | You, by editing the alias |
| Is the result repeatable? | No, it depends on which Utils builds exist at that moment | Yes |
| Typical use | Packages in one project that release together | Third-party packages, and production release branches |
| Main risk | A new build you did not test is picked up silently | Falling behind on bug fixes |
Beta, released, and the promotion rule
Dependencies interact with the beta and released states. A package version can only be promoted to released if every package it depends on is already released. If Sales App depends on a beta build of Utils, then sf package version promote for Sales App fails until that Utils build is promoted first.
This is where the choice has consequences:
- With
LATEST, the build you float to might be a beta that you never intended to ship. Check what it resolved to before you promote anything. - With a pin, you choose the build on purpose. Pin to a released build for any version of the dependent package that you plan to promote.
A reasonable pattern is to float during day-to-day development, so that Sales App keeps picking up fresh Utils builds, and to pin on the branch that you cut a release from.
The full sequence to build and release a dependency, then use it, looks like this:
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHub
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version promote --package "[email protected]" --target-dev-hub myDevHub
sf package install --package "[email protected]" --target-org myOrg --wait 10After promoting Utils 1.1.0-1, you can safely pin Sales App to [email protected] and then build and promote Sales App.
Predicting what will be resolved
Work through the resolution by asking three questions in order.
- Is the dependency an alias that maps to a
04tID? If yes, the answer is that ID. Nothing else matters. - Is it a version number ending in
LATEST? If yes, take the dependency package's builds whose MAJOR.MINOR.PATCH match, and choose the highest build number. - Is there any matching build? If not, package version creation fails. You must create the dependency version first, or correct the version number.
For example, with builds 1.0.0-4, 1.1.0-1, 1.1.0-2, and 1.2.0-1 of Utils:
1.1.0.LATESTresolves to1.1.0-2.1.0.0.LATESTresolves to1.0.0-4.1.3.0.LATESTmatches nothing and fails.
Because the result is recorded at creation time, it is worth inspecting it. Run sf package version report --package "Sales [email protected]" --verbose --target-dev-hub myDevHub and read the dependency information in the output, so you know which Utils build is baked in.
Failure modes
- Silent drift. Two builds of the same Sales App version number differ in their Utils build because Utils changed between builds. Fix: pin, or record the resolved dependency in release notes.
- Promotion blocked. Sales App will not promote because the floated Utils build is still a beta. Fix: promote Utils first, then rebuild Sales App, which resolves
LATESTagain. - Stale pin. A pin stays on
1.1.0-1while Utils ships fixes in1.1.0-5. Fix: update the alias and rebuild on a schedule. - Wrong alias. Typing
[email protected]when no such alias exists inpackageAliasesfails because the CLI cannot find the ID. Fix: list versions, or check the aliases the CLI created.
Exercise
Your Sales App depends on { "package": "Utils", "versionNumber": "1.1.0.LATEST" }. Utils has builds 1.1.0-1, 1.1.0-2 (both released) and 1.1.0-3 (beta). A teammate builds a new Sales App version. Then Utils build 1.1.0-4 is created. Which Utils build is recorded in the Sales App version, and does it change when 1.1.0-4 appears? What would you change to make the next release repeatable?
Show answer
The dependency resolves at creation time to the highest matching build that exists then, which is 1.1.0-3, and that is a beta, so Sales App cannot be promoted until Utils 1.1.0-3 is promoted. The recorded dependency does not change when 1.1.0-4 appears, because LATEST is not re-evaluated for existing versions. To make the next release repeatable, promote a Utils build, add an alias such as [email protected] mapped to its 04t ID, and change the dependency to { "package": "[email protected]" }.