Sub topic 4
Creating a package version
This chapter assumes you have a Dev Hub with second-generation packaging enabled, a Salesforce DX project, and a package already created with sf package create. It also assumes you know how sfdx-project.json describes a package. Here you will build a version from source, read its result, and handle the common failures.
What a package version is
A package version is an immutable snapshot of the metadata in one package directory. The Dev Hub builds it from your local source, not from an org. Once built, you can't edit it. To change anything, you build a new version.
The create command
sf package version create \
--package Utils \
--installation-key-bypass \
--code-coverage \
--wait 20 \
--target-dev-hub myDevHubEach flag does one job:
--packageaccepts the package name or its0HoID. It must match an entry inpackageAliases.--installation-key-bypassstates that installers don't need a password. Without it, the command requires--installation-key.--code-coveragemakes the build compute Apex coverage. Unlocked packages need at least 75% coverage before promotion.--wait 20blocks for up to 20 minutes. Without it, the command returns a request ID and the build continues in the background.
What happens during a build
The version number comes from the package directory entry in sfdx-project.json. If it ends in .NEXT, the Dev Hub replaces NEXT with the next unused build number.
<br>
The temporary org is why dependencies matter at build time. Every package listed in dependencies must already exist as a version that the Dev Hub can install.
Reading the result
When the build succeeds, the CLI prints a table. The important fields are the SubscriberPackageVersionId (starts with 04t) and the Version. The CLI also adds an alias such as [email protected] to packageAliases, which you can use for installs and dependencies.
If you used --wait and it timed out, the build is still running. Check it with the request ID, which starts with 08c:
sf package version create report --package-create-request-id 08c000000000001AAA --target-dev-hub myDevHubYou can list recent builds with sf package version create list.
<br>
Useful flags
| Flag | Purpose |
|---|---|
| `--branch` | Records the source branch so you can later filter versions |
| `--tag` | Attaches a free-text label such as a commit ID |
| `--skip-validation` | Builds faster by skipping dependency and coverage checks |
| `--definition-file` | Supplies a scratch org definition for the build org's features and settings |
<br>
Common build failures
- Dependency not found. The dependency's version isn't in
packageAliases, or the alias points to a version that doesn't exist. - Coverage below 75%. The build can succeed, but promotion fails later. Check the coverage value in the report.
- Apex test failure. Tests run during validation. A failing test fails the build.
- Missing metadata. The package directory references a component that lives in another directory. Either move it or declare a dependency.
- Version number not higher. A fixed version number that already exists is rejected. Use
.NEXTto avoid this.
Exercise
Your build times out after --wait 5. What do you do, and what ID do you need?
Show answer
The build continues on the Dev Hub, so don't rerun it. Use sf package version create report with the 08c package-create-request ID printed by the original command. When it succeeds, the report shows the 04t subscriber package version ID.