Sub topic 6
Reading and setting version numbers
This chapter assumes you have a Dev Hub, a linked namespace, and an sfdx-project.json with a package directory, as covered in earlier chapters. You should be comfortable running sf commands and reading JSON.
Every second-generation managed package (2GP) version has a four-part number. You set it in sfdx-project.json, and the CLI turns it into an alias you use to install and promote. A wrong number, or a wrong guess about how it behaves, causes failed builds and upgrade problems that are hard to undo.
The four segments
A version number has the form major.minor.patch.build.
| Segment | Who sets it | Typical meaning |
|---|---|---|
| major | You | Large releases or breaking changes in your own conventions |
| minor | You | New features within a major release |
| patch | You | Fixes, and patch versions of a released package |
| build | You or the CLI | Counts attempts to build the same major.minor.patch |
Salesforce does not enforce what "major" or "minor" mean semantically. The platform only enforces ordering and uniqueness rules. Choose a convention and keep to it, because subscribers and your own ancestry chain depend on it.
The first three segments identify a release. The build number identifies one attempt at building that release. You can create many builds of 1.1.0, such as 1.1.0.1, 1.1.0.2 and 1.1.0.3, while you fix test failures or code problems. Only one build of a given major.minor.patch can be promoted to released.
The NEXT keyword
In the package directory you normally write the build segment as NEXT:
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Spring update",
"versionNumber": "1.1.0.NEXT",
"versionDescription": "Adds the reporting tab",
"ancestorVersion": "HIGHEST"
}When you run sf package version create, the CLI replaces NEXT with the next unused build number for 1.1.0. If builds 1 and 2 exist, the new version is 1.1.0.3. You do not edit the file between attempts.
You could hard-code a build number such as 1.1.0.5. This is rarely a good idea. If that number already exists, the create command fails. It also gives you no benefit over NEXT, because the build number carries no meaning beyond ordering.
NEXT belongs only in the package's own versionNumber. It is not a valid value in a dependency entry.
The LATEST keyword
LATEST is for dependencies. In a dependency entry it resolves to the newest matching build of another package when you create your version:
"dependencies": [
{
"package": "BasePackage",
"versionNumber": "1.2.0.LATEST"
}
]Here 1.2.0 is fixed and only the build is resolved. Resolution happens at version-create time, and the resolved version is recorded on the new package version. Rebuilding later can therefore pick a different dependency build. If you need a repeatable build, pin a dependency by package alias instead, for example "package": "[email protected]". Dependencies are covered in a later chapter.
Version numbers and aliases
The CLI shows versions in two forms.
- The number in the project file uses dots:
1.0.0.NEXT. - The alias that
sf package createandsf package version createwrite intopackageAliasesuses an at sign and a hyphen:[email protected].
The alias is the version number with the last dot replaced by a hyphen. Each version also has a subscriber package version ID that starts with 04t. That ID is what the platform uses internally, and you can pass it to install and promote commands instead of an alias.
sf package version list --packages AcmeApp --target-dev-hub devhub
sf package version report --package [email protected] --target-dev-hub devhubThe list command shows every build with its version number, its 04t ID, whether it is released, and the ancestor and code coverage values. Use report when you need the details of one version.
Rules that constrain your choices
Some rules apply to every package.
- Each full number must be unique. You cannot create a second
1.1.0.3for the same package. - Only one build per
major.minor.patchcan be released. After you promote1.1.0.3, you cannot promote1.1.0.4. To ship more changes, increase at least one of the first three segments. - Released versions are permanent. You cannot unrelease a version. For a managed package, you can delete only beta versions.
- Beta versions install only in scratch orgs and sandboxes. Production orgs can install released versions only.
Managed packages add rules that tie into ancestry and upgrades, which a later chapter covers in full.
- Subscribers can upgrade only to a higher version in the same ancestry line. A new version's number must therefore be higher than its ancestor's number.
- The patch segment is special. In a managed package, a nonzero patch number is for a patch version created from a released version. Normal feature releases use
.0for the patch, such as1.1.0and1.2.0. The pushing and patching chapter returns to this. - Ancestor choice interacts with numbering. Setting
ancestorVersiontoHIGHESTselects the highest released version.NONEis valid only for the first version, or for the case where you are starting a new line deliberately.
Choosing which segment to bump
The decision below follows a common convention. It is a recommendation, not a platform rule.
Retrying is the most common case. If 1.1.0.1 failed code coverage, fix the code and run the create command again with the same file. The result is 1.1.0.2. After you promote that build, the next change needs 1.2.0.NEXT or higher.
A worked sequence
Suppose 1.0.0.1 was your first beta and you promoted it. You now want to ship a new feature.
- Change
versionNumberto1.1.0.NEXTand setancestorVersiontoHIGHEST. - Run
sf package version create --package AcmeApp --installation-key-bypass --code-coverage --wait 20 --target-dev-hub devhub. - The CLI resolves the number to
1.1.0.1and adds the alias[email protected]. - A test fails in a later change. You fix it and create again, which produces
1.1.0.2. - You promote
[email protected]. Build1.1.0.1stays a beta that nobody should install, and1.1.0can never be released again.
Common errors
- Forgetting to change
versionNumberafter a release. After you promote1.1.0.2, building from the unchanged1.1.0.NEXTproduces1.1.0.3. Promoting it fails because1.1.0is already released. Change the minor segment first. - Hard-coding the build number. A fixed build number fails as soon as that number exists. Use
NEXT. - Using
LATESTwhere you need repeatable builds. Two builds of your package a week apart can embed different dependency versions. - Setting a lower version than the ancestor. The create command rejects a version that does not rank above its ancestor.
- Reading the alias as a different number.
[email protected]means version1.1.0.2, not1.1.0with some separate revision.
Exercise
Your package has released version 2.3.0.4. The file now says "versionNumber": "2.3.0.NEXT" and you build twice, getting 2.3.0.5 and 2.3.0.6. You try to promote 2.3.0.6. What happens, and what should you have done before building?
Show answer
The promote fails because major.minor.patch 2.3.0 already has a released build (2.3.0.4), and only one build per major.minor.patch can be released. Before building you should have raised at least one of the first three segments, for example to 2.4.0.NEXT, and set the ancestor to HIGHEST. The two builds you created are betas that cannot be released.
Exercise
A teammate wants repeatable builds and replaces "BasePackage" with "1.2.0.LATEST" by "[email protected]". What is the effect, and what do they lose?
Show answer
The dependency is pinned to build 1.2.0.4, so every rebuild of your package embeds that same dependency version. They lose automatic pickup of newer builds of 1.2.0, so they must edit the alias whenever they want a newer build.