Sub topic 8
Building a package version
This chapter assumes you have a Dev Hub with unlocked packages enabled, a package created with sf package create, and an sfdx-project.json that lists it. It also assumes you know how Apex tests and code coverage work.
What a package version is
A package version is an immutable snapshot of the metadata in one package directory. Source files in Git can change after the build. The version stays as it was built. You can install that version into any org, and you can install it again later to get the same result.
Creating the version
Run this from the project root:
sf package version create --package Core --installation-key-bypass --code-coverage --wait 20 --target-dev-hub MyDevHub--packagenames the package, using the name or alias fromsfdx-project.json.--installation-key-bypassstates that no installation key is needed. You must pass either this flag or--installation-key.--code-coverageruns the Apex tests and records the coverage figure. A version needs this figure before you can promote it.--wait 20makes the command wait up to 20 minutes. Without it, the command returns straight away and you check progress later.
The build resolves 1.0.0.NEXT to the next free build number, for example 1.0.0.1. The CLI then adds an alias such as [email protected] to sfdx-project.json.
What happens during the build
The Dev Hub does the work in the cloud, not on your machine.
Because the build happens in a clean org, your package must contain everything it needs. Anything it references must either be inside the package or be declared as a dependency.
Checking the result
If you did not use --wait, or the wait timed out, check the request. The command prints a request ID that starts with 08c.
sf package version create report --package-create-request-id 08c000000000001 --target-dev-hub MyDevHubThe report shows the status, the version number, and the subscriber package version ID (starting with 04t). That ID is what you install.
Common errors
| Symptom | Likely cause |
|---|---|
| Build fails with "no such field" or "no such class" | A component references metadata that is neither in the package nor in a dependency |
| Coverage below 75% | Apex in the package lacks tests. Promotion will be blocked even if the build succeeds. |
| Command asks for a key flag | Neither `--installation-key` nor `--installation-key-bypass` was passed |
| Dependency not found | The dependency is missing from `sfdx-project.json`, or has no matching version |
Exercise
You build a version with --skip-validation and it succeeds. Why can't you promote it, and what do you do?
Show answer
Skipping validation means the tests never ran and no code coverage was calculated. Promotion needs a coverage figure of at least 75% for Apex. Build a new version with --code-coverage, then promote that one.