Sub topic 2
Reading and choosing version numbers
This chapter assumes you know Salesforce DX projects and the sf CLI, can read sfdx-project.json, and know what a Dev Hub is. By the end you will be able to read a version number, say what each part means, and decide which part to change for a given release.
The four parts of a version number
Every second-generation package version has a number in the form MAJOR.MINOR.PATCH.BUILD. Example: 2.1.3.7.
| Part | Who sets it | Typical meaning |
|---|---|---|
| MAJOR | You | Large or incompatible change |
| MINOR | You | New functionality that does not break existing users |
| PATCH | You | Bug fix with no functional change |
| BUILD | The platform, or you | Counter for each attempt at the same MAJOR.MINOR.PATCH |
The first three parts are a convention that you control. Salesforce does not check whether a MAJOR bump really contains a breaking change. The numbers are only labels you choose, so choose them consistently. Your dependents will rely on them when they decide which versions to accept.
The BUILD part exists because creating a version is not the same as releasing it. You may create several versions of 1.1.0 while you fix test failures. Each gets a new build number: 1.1.0.1, 1.1.0.2, 1.1.0.3. Only one of them is later promoted to released status. The next chapters cover beta and released status in detail.
Where the number lives
You declare the number for the next version in the package's entry in sfdx-project.json:
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionDescription": "Adds date helpers",
"versionNumber": "1.1.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA"
}
}Two fields look similar but do different jobs:
versionNumberis the real identity of the version. The platform uses it to order versions, to resolve dependencies, and to decide whether an install is an upgrade.versionNameis a display label such as "Utils 1.1" or "Spring release". It does not affect ordering or dependency resolution. You can change it between builds without consequence.
Keep the two in step by habit, because a mismatch confuses people reading package lists. The platform does not enforce it.
NEXT and LATEST
The build part can be a number or a keyword.
NEXTmeans "assign the next unused build number for this MAJOR.MINOR.PATCH". Use it in theversionNumberof the package you are building. If the last version you created was1.1.0.2, the next creation produces1.1.0.3. If you change the patch to1, the build counter starts again, giving1.1.1.1.LATESTmeans "the highest build number that exists for this MAJOR.MINOR.PATCH". Use it in adependenciesentry, where you are pointing at another package's version rather than naming your own.
You can also write a literal build number, such as 1.1.0.5. That is allowed in a dependency, where it narrows the match. In the package's own versionNumber it is rarely a good idea. You then have to keep the counter correct yourself, and a repeated number fails. NEXT removes that bookkeeping.
{
"path": "app",
"package": "Sales App",
"versionNumber": "2.0.0.NEXT",
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
}In this example 2.0.0.NEXT is the app's own number and 1.1.0.LATEST is the dependency. Putting LATEST on the app's own versionNumber, or NEXT in a dependency, is a common mistake. Neither keyword makes sense there. The chapter on pinning versions covers how LATEST is resolved.
Seeing the number in the CLI
When a version is created, the CLI turns the keyword into a concrete number and records an alias. An alias is a friendly name in packageAliases that maps to an ID. The alias format is [email protected]. Note the hyphen before the build number:
sf package version create --package Utils --installation-key-bypass --wait 20 --target-dev-hub myDevHub
sf package version list --packages Utils --target-dev-hub myDevHubAfter the first command finishes, packageAliases gains an entry like this:
"[email protected]": "04t000000000001AAA"The 04t ID is the subscriber package version ID. It is the ID that install commands and exact-version dependencies use. The dotted number 1.1.0.1 and the alias [email protected] name the same version, in two spellings.
You can also set the number on the command line instead of editing the file. The --version-number flag overrides versionNumber for that one run:
sf package version create --package Utils --version-number 1.2.0.NEXT --installation-key-bypass --wait 20 --target-dev-hub myDevHubUse this sparingly. If the file and the command history disagree, the next person who reads the file will be misled. Prefer editing sfdx-project.json and committing the change.
Choosing which part to change
The decision depends on what your dependents can tolerate. The following chart is a starting point.
Some practical rules follow from this.
- When you raise a higher part, reset the lower parts to zero. After
1.4.2, a new feature gives1.5.0, not1.5.2. - Do not reuse a MAJOR.MINOR.PATCH once a version with that number has been promoted to released. A released version is fixed. Further work needs a higher number.
- Treat each build number as disposable. Betas you create while testing are expected to pile up. Do not read meaning into a build number such as
1.1.0.14, other than "the fourteenth attempt". - Decide with your dependents in mind. A package that others pin to
1.1.0.LATESTwill pick up every new1.1.0build. It will not pick up1.2.0. A change you call a patch therefore reaches them automatically, so be strict about what counts as one.
Edge cases to watch for
The type of package changes some details.
- Unlocked packages let you decide the numbering freely, within the rules above.
- Second-generation managed packages place stricter limits on what changes between versions, which affects how you can use MINOR and PATCH. A later chapter on ancestry and upgrades covers this.
- First-generation managed packages use a two-part major.minor number for ordinary releases, with a patch number only for patch releases. They have no build number in this sense. If you maintain both generations, do not assume the four-part reading applies to the older one.
Another trap is that a build number is only unique within one MAJOR.MINOR.PATCH. 1.1.0.3 and 1.2.0.3 are different versions with an unrelated counter. Never compare build numbers across different MAJOR.MINOR.PATCH values.
Summary of keywords
| Keyword | Where it is valid | Resolves to |
|---|---|---|
| `NEXT` | The package's own `versionNumber` | The next unused build number for that MAJOR.MINOR.PATCH |
| `LATEST` | A `dependencies` entry | The highest existing build number for that MAJOR.MINOR.PATCH |
| A literal number | Either place | Exactly that build |
Exercise
Your package's last created version is 3.2.1.4. You fix a bug that does not change behavior otherwise, and versionNumber in sfdx-project.json is currently 3.2.1.NEXT. Which version number do you expect to get from the next create command, and which part would you change if you instead added a new feature?
Show answer
The next create produces 3.2.1.5, because NEXT takes the next unused build for the same MAJOR.MINOR.PATCH. If you add a new feature, change versionNumber to 3.3.0.NEXT. That raises MINOR and resets PATCH to 0, and the build counter then starts at 1, giving 3.3.0.1.
Exercise
A teammate writes "versionNumber": "2.0.0.LATEST" on the package they are building. What is wrong, and what should it be?
Show answer
LATEST is meant for pointing at an existing version in a dependencies entry. The package's own versionNumber should use NEXT, so 2.0.0.NEXT, so that the platform assigns a new build number.