Sub topic 11
Declaring an Unlocked Package: package, versionName, versionNumber
This chapter covers the fields that turn a plain package directory into an unlocked package. It assumes you can read sfdx-project.json, know what packageDirectories and the default flag do, and have the sf CLI authorized against a Dev Hub org. Later chapters cover packageAliases and dependencies in detail. Here the focus is the three fields that identify a package and its version.
From a source folder to a package
A package directory is only a folder of source until you add package fields. With only path and default, the CLI deploys the folder as ordinary source. With the package field set, the CLI treats the folder as the contents of an unlocked package that you can build into versions.
An unlocked package is a versioned bundle of metadata. You create it in a Dev Hub org, build immutable versions of it, and install those versions in other orgs. Each package directory maps to at most one package.
The fields that matter most are listed below.
| Field | Required for packaging | Purpose |
|---|---|---|
| `path` | Yes | Folder that holds the package source. |
| `package` | Yes | Name of the package, matching the name given at creation. |
| `versionName` | Recommended | Human-readable label for the version. |
| `versionNumber` | Yes | Four-part version, `major.minor.patch.build`. |
| `default` | Yes, on one directory | Marks the directory used when a command needs a default. |
The package field
The package value is the package name you pass to sf package create. It also acts as the key that links the directory to an entry in packageAliases.
sf package create --name core-pkg --package-type Unlocked --path core --target-dev-hub my-devhubThis command does three things. It creates the package record in the Dev Hub. It receives a package ID that starts with 0Ho. It writes an alias to packageAliases in sfdx-project.json, and it also adds the package field to the matching directory if the entry is not already there.
After the command runs, the project file looks like this:
{
"packageDirectories": [
{
"path": "core",
"package": "core-pkg",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"core-pkg": "0Ho000000000000AAA"
}
}The ID shown is a placeholder. Your own file contains the real ID returned by the Dev Hub.
If the package value does not match a key in packageAliases, version creation fails because the CLI cannot resolve the name to an ID. Keep the two spellings identical, including case.
The versionNumber field
versionNumber has four parts separated by dots: major.minor.patch.build. For example, 1.2.0.NEXT.
- major and minor track compatibility and feature changes. You choose when to raise them.
- patch is for fixes. It is relevant mainly for managed packages and is usually
0for unlocked packages. - build is a counter. Use the keyword
NEXTand the platform assigns the next number when you create a version.
The NEXT keyword is the normal choice. If you write 1.2.0.NEXT and the latest build for 1.2.0 is 3, the new version becomes 1.2.0.4. You do not edit the file between builds.
A fixed build number such as 1.2.0.5 is valid, but you take responsibility for never repeating it. A repeated number fails with an error saying the version already exists.
Version numbers in dependencies
Version numbers also appear in a dependencies entry. There you can use LATEST instead of NEXT. LATEST means "the newest built version that matches the other three parts".
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]Use NEXT only for the package you are building. Use LATEST only for the packages you depend on. Mixing them up is a common source of confusing errors.
The versionName field
versionName is a free-text label such as Spring release or ver 0.1. It appears in the Dev Hub and in the installed package list. It does not affect ordering, compatibility, or installation. Two versions can share a name, and renaming is a metadata-only change.
You can also set versionDescription for longer notes, and ancestorId or ancestorVersion to define the version lineage. Those fields are optional and are not required to build an unlocked package.
Creating a version
Once the fields are in place, create a version from the directory. The --package flag takes the package name or alias.
sf package version create --package core-pkg --installation-key-bypass --wait 20 --target-dev-hub my-devhubThe CLI reads the matching directory, resolves any dependencies, and sends the source to the Dev Hub. The Dev Hub builds the version in a temporary scratch org that it creates for you. If the build succeeds, you get a subscriber package version ID that starts with 04t. The CLI adds it to packageAliases under a key such as [email protected].
The flags used above work as follows:
--installation-key-bypassmeans the version has no install password. Without it, you must supply--installation-key.--wait 20makes the command wait up to 20 minutes for the build.--target-dev-hubnames the Dev Hub org. You can omit it if you have set a default Dev Hub.
The following diagram shows how the fields feed this process.
Version rules and edge cases
Several rules cause errors that look unrelated to their cause.
- A directory must be inside the project. The
pathvalue is relative to the project root and must exist. A typo causes a "path not found" error when you create the version. - Only one default. Exactly one directory should have
"default": true. With a two-package project, the other should befalse. - Version numbers must go up. For a given package, a new version must not be lower than an existing released version in the same major and minor line. The build number resets when you change major, minor, or patch.
- Versions are immutable. You cannot change the contents of
1.0.0.4after it is created. Fix the source and build1.0.0.5. - Promotion is separate. A new version is a beta until you run
sf package version promote. Only promoted versions can be installed in production orgs. - Namespace must match. If the project has a
namespace, it must be a namespace linked to the Dev Hub. For an unlocked package with no namespace, leave the value as an empty string.
Deploying a packaged directory
The package fields do not change how sf project deploy start works. The command still deploys source from all package directories, or from the ones you name with --source-dir. This means you can use scratch orgs and ordinary deploys while developing, and create versions only when you are ready to publish.
sf project deploy start --source-dir core
sf package version create --package core-pkg --installation-key-bypass --wait 20The first command pushes the source to your current default org. The second builds an installable version from the same folder.
Troubleshooting package fields
| Symptom | Likely cause | Fix |
|---|---|---|
| "No package named core-pkg" | `package` value is missing from `packageAliases` or is misspelled | Match the spelling or rerun `sf package create`. |
| "Version number is invalid" | Fewer or more than four parts, or `NEXT` in the wrong place | Use `major.minor.patch.NEXT`. |
| "Path does not exist" | Wrong `path` or missing folder | Correct the path relative to the project root. |
| Version creation hangs or times out | Build takes longer than `--wait` | Increase `--wait`, then check status with `sf package version create report`. |
| Dependency not found | `LATEST` points at a version that was never built | Build the dependency first, or use a specific version. |
Exercise
A project has a directory with "package": "core-pkg" and "versionNumber": "1.0.0.NEXT". The latest built version is 1.0.0.3. You run sf package version create again with no file changes. What version number does the new version receive, and what field would you change to start a new 1.1 line?
Show answer
The new version is 1.0.0.4, because NEXT asks the platform for the next build number. To start a 1.1 line, change versionNumber to 1.1.0.NEXT. The build counter then restarts for that line, and the first version built is 1.1.0.1.
Exercise
You renamed a package directory entry from "package": "core-pkg" to "package": "Core-Pkg", and now version creation fails to find the package. Why, and how do you fix it?
Show answer
The package value must match a key in packageAliases exactly, including case. Core-Pkg has no alias entry. Change the package value back to core-pkg, or add a matching alias that points to the same 0Ho package ID.