Sub topic 11
Declaring an Unlocked Package: package, versionName, versionNumber
This chapter assumes you can read sfdx-project.json, know what packageDirectories and the default directory are, and have a Dev Hub org enabled for packaging. You will turn a plain package directory into an unlocked package by adding three fields.
From source folder to package
A package directory with only path and default is just a source folder. The CLI deploys it, but it is not a package. Adding the package field marks the directory as the source for an unlocked package. Two more fields, versionName and versionNumber, describe the version you will build from it.
{
"packageDirectories": [
{
"path": "core",
"package": "core-pkg",
"versionName": "Spring release",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The three fields
| Field | Meaning | Example |
|---|---|---|
| `package` | The package name. It must match a package created in your Dev Hub. | `core-pkg` |
| `versionName` | A human-readable label for the version. It is free text. | `Spring release` |
| `versionNumber` | The version in `major.minor.patch.build` form. | `1.0.0.NEXT` |
package
The value is the package name, not its ID. The ID goes in packageAliases, which a later chapter covers. If the name in the file does not match a package in the Dev Hub, version creation fails.
versionName
This is a display label. Salesforce does not parse it. Change it when you want the release to carry a new title, such as Summer release.
versionNumber
The number has four parts: major, minor, patch, and build. The keyword NEXT in the build position tells the CLI to assign the next available build number when you create a version. So 1.0.0.NEXT produces 1.0.0.1, then 1.0.0.2, and so on. Raise the major, minor, or patch part by hand when you want a new release line.
Create the package and a version
Run sf package create once. It registers the package in the Dev Hub and writes its ID into packageAliases in your project file. Then run sf package version create each time you want a new version.
sf package create --name core-pkg --package-type Unlocked --path core --target-dev-hub my-devhub
sf package version create --package core-pkg --installation-key-bypass --wait 10 --target-dev-hub my-devhubThe --path value must equal the path of the directory in sfdx-project.json. The --name value must equal its package field.
Common mistakes
- Using a version number with only three parts. Include the fourth part, even if it is
NEXT. - Setting
packageto a name that does not exist in the Dev Hub. - Leaving
versionNumberunchanged after a release you meant to move forward. The build number still increments, but the major, minor, and patch parts stay the same.
Exercise
A directory has "versionNumber": "2.1.0.NEXT". You run sf package version create twice with no edits to the file. What version numbers do the two versions get?
Show answer
Each run takes the next available build number, so the versions are 2.1.0.1 and 2.1.0.2, assuming no earlier 2.1.0 builds exist. Only the build part changes. The major, minor, and patch parts change only when you edit them.
Exercise
Which field links a package directory to a package in the Dev Hub, and is it the package name or the package ID?
Show answer
The package field links them, and it holds the package name. The ID is stored separately in packageAliases.