Sub topic 4
Creating a package version
A package version is an immutable snapshot of the metadata in one package directory. This chapter covers how sf package version create builds that snapshot, what the flags do, and why builds fail.
This chapter assumes you can work with a Salesforce DX project and the sf CLI, and that you have a Dev Hub org you can authorize. It also assumes you have already created the package and read its sfdx-project.json entry.
What the command does
Creating a version is an asynchronous job that runs on the Dev Hub. The CLI does not build the package locally. It uploads the source from your package directory, and the Dev Hub does the following:
- Resolves the package directory and its declared dependencies.
- Creates a temporary build org from a definition file (or a default shape).
- Installs the dependencies, then deploys your metadata into that org.
- Runs Apex tests if you asked for code coverage.
- Records the result as a new package version with a
04tID.
The CLI returns a request ID that starts with 08c. You can wait on it or check it later. The 04t ID is the one you install from.
The configuration it reads
The command takes most of its settings from sfdx-project.json. The package directory entry supplies the version number, the version name, and the dependencies.
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionNumber": "1.1.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA"
}
}The packageAliases section maps the name Utils to the 0Ho package ID. The --package flag accepts either the alias or the ID. The .NEXT suffix on the build number tells the Dev Hub to assign the next unused build number for 1.1.0. You do not edit the build number by hand.
Running the command
This is a typical build of an unlocked package:
sf package version create \
--package Utils \
--installation-key-bypass \
--wait 20 \
--code-coverage \
--target-dev-hub myDevHubThe flags that matter most are in this table.
| Flag | Purpose |
|---|---|
| `--package` | Alias or `0Ho` ID of the package to version. |
| `--path` | Package directory to build. Use it as an alternative to `--package` when you want to name the directory. |
| `--installation-key` or `--installation-key-bypass` | Protect installs with a password, or explicitly opt out. You must choose one. |
| `--wait` | Minutes the CLI waits for the build. Without it the command returns right after submitting. |
| `--code-coverage` | Calculates Apex coverage so the version can be promoted later. |
| `--definition-file` | Scratch org definition used for the build org. |
| `--branch` | Label for the source branch. Versions on different branches can be told apart. |
| `--tag` | Free-text label stored with the version. |
| `--skip-validation` | Skips dependency and metadata validation for a faster build. |
| `--target-dev-hub` | Dev Hub that owns the package. |
Waiting versus polling
A build can take several minutes, or longer for large packages and managed packages with many tests. If --wait is shorter than the build, the CLI stops waiting but the job continues on the Dev Hub. Check on it with the request ID:
sf package version create report \
--package-create-request-id 08c000000000001AAA \
--target-dev-hub myDevHub
sf package version create list --target-dev-hub myDevHubThe status moves through Queued, In Progress, and then Success or Error.
What changes in your project
When the build succeeds, the CLI adds a new alias to packageAliases. The alias combines the package name and the version number with the build number:
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"[email protected]": "04t000000000001AAA"
}Commit this change. Other packages that depend on [email protected], and your install scripts, need the alias to find the 04t ID. If you run the build in CI, make sure the pipeline either commits the updated file or stores the 04t ID somewhere else.
Choosing version numbers when you build
The version number has the form MAJOR.MINOR.PATCH.BUILD. Some rules affect the build step:
- Each build for a given
MAJOR.MINOR.PATCHgets a new build number..NEXThandles this for you. - Once you promote a version, you cannot promote another version with the same
MAJOR.MINOR.PATCH. To ship again, raise at least one of those three numbers. - Beta versions can be rebuilt as many times as you like. Each build gets a new build number, and each is a separate
04tID.
You can use a fixed build number in sfdx-project.json, for example 1.1.0.5. This is rarely useful and risks a clash with an existing version, so most teams keep .NEXT.
Ancestry and the first build
For a managed package, or an unlocked package whose releases you plan to upgrade in place, each new released version must have a defined ancestor. The CLI checks this when you build. In sfdx-project.json you set ancestorVersion (for example "ancestorVersion": "HIGHEST") or ancestorId. If you are building the first version of a package, or you deliberately start a new line, use "ancestorVersion": "NONE". The --skip-ancestor-check flag exists for special cases, but using it can leave you with a version that cannot be promoted as you intended. Ancestry gets its own chapter, so this chapter only flags it as a source of build errors.
Dependencies during the build
The build org installs every dependency listed for the package directory before it deploys your metadata. Three consequences follow:
- Dependencies must already exist as package versions. A pinned dependency such as
[email protected]must resolve to a04tID inpackageAliases. - A dependency that uses
.LATESTresolves at build time. The same source built twice on different days can pick up different dependency versions. - Dependencies are recorded in the new version. Installing it later pulls in the same dependency versions that the build used.
Packages in the same project that depend on each other must appear in dependency order in packageDirectories, with the dependency listed first.
Features and settings in the build org
Your metadata must deploy into a clean org. If your code relies on a feature, such as multi-currency or a Chatter setting, the build org needs that feature too. Supply it through a definition file:
{
"orgName": "Utils package build",
"edition": "Developer",
"features": ["MultiCurrency"],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Pass it with --definition-file config/project-scratch-def.json. Remember that a subscriber's org must have the same feature enabled before it can install the version. If the feature cannot be assumed, look at org-dependent unlocked packages, which are covered later in this tutorial.
Code coverage and promotion
Without --code-coverage, the build skips coverage calculation and the version cannot be promoted. This is the most common reason a beta version turns out to be useless for release. For unlocked and managed packages, promotion requires at least 75% Apex coverage across the package. Tests run in the build org, so tests that depend on data or settings outside the package fail there even though they pass in your scratch org.
--skip-validation makes builds faster, which suits quick iteration on a beta. A version built this way can never be promoted, and coverage is not calculated. Use it only for throwaway testing.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Missing required flag for installation key | Neither `--installation-key` nor `--installation-key-bypass` was given | Add one of them. |
| Package alias or ID not found | Alias missing from `packageAliases`, or wrong Dev Hub | Check the alias and `--target-dev-hub`. |
| Dependency cannot be resolved | Dependency version was never built, or the alias is misspelled | Build the dependency first and confirm the alias exists. |
| Deployment errors in the build org | Metadata references a feature, setting, or object that the package does not contain | Add the feature to the definition file, or add the missing metadata to the package. |
| Cannot promote the version | Built without `--code-coverage`, coverage under 75%, or built with `--skip-validation` | Rebuild with `--code-coverage` and add tests. |
| Ancestor error | `ancestorVersion` or `ancestorId` is missing or inconsistent with existing released versions | Set the ancestor explicitly. |
Read the full error list with sf package version create report. The first failure often causes later ones, so fix errors from the top down.
Exercise
You run sf package version create --package Utils --installation-key-bypass --wait 5 and the CLI times out after five minutes. What is the state of the build, and how do you find the result?
Show answer
The CLI stopped waiting, but the build keeps running on the Dev Hub. Check it with sf package version create report --package-create-request-id <08c ID> or list requests with sf package version create list. Also note that this build did not use --code-coverage, so the resulting version cannot be promoted.
Exercise
Your build fails because an Apex class references a field that exists in your scratch org only because of a feature you enabled manually. Where do you fix it, and why does the build org matter?
Show answer
Add the feature or setting to the scratch org definition file and pass it with --definition-file. The build org is created fresh from that file, with only your declared dependencies installed, so anything that is not in the package or the definition file is missing during the deploy.
Exercise
A teammate says that rebuilding the same source gives "the same version". Is that right?
Show answer
No. Each build gets a new build number with .NEXT and a new 04t ID, even if the source is unchanged. A .LATEST dependency may also resolve to a different version on a later day.