Sub topic 7
Building a package version
This chapter assumes you have a Dev Hub with a linked namespace, a managed package created with sf package create, and an sfdx-project.json whose package directory has a versionNumber. You should also know how to use scratch orgs and the Salesforce CLI.
What a package version is
A package version is an immutable snapshot of the metadata in a package directory. Salesforce builds it from your source on the server side, not from your local org. Once built, the version cannot be edited. To change anything, you build a new version.
Running the build
Use sf package version create from the project root:
sf package version create --package AcmeApp --installation-key-bypass --code-coverage --wait 20 --target-dev-hub devhubEach flag has a job:
--packagenames the package, either by alias or by its0HoID.--installation-key-bypasssays installers do not need a password. Use--installation-keyinstead if you want one.--code-coveragemakes Salesforce run Apex tests and calculate coverage. A version without coverage data cannot be promoted.--wait 20keeps the command running for up to 20 minutes. Without it, the command returns at once and the build continues in the background.--target-dev-hubpoints at the org that owns the package.
What happens during a build
The new version is a beta. Its number comes from versionNumber. The NEXT keyword in 1.0.0.NEXT becomes the next free build number, so your first build is 1.0.0-1. The CLI also writes an alias such as [email protected] into sfdx-project.json, and you can use that alias in later commands.
Checking progress and results
If you did not use --wait, or the wait timed out, check the request:
sf package version create report --package-create-request-id 08c000000000001 --target-dev-hub devhubTo list versions and see their status, coverage, and release state:
sf package version list --packages AcmeApp --target-dev-hub devhubCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Build fails with a missing component error | The package directory references metadata outside itself | Move the metadata in, or add the other package as a dependency |
| Version builds but cannot be promoted | Coverage below 75%, or `--code-coverage` was not used | Add tests, then build a new version with the flag |
| Timeout message from the CLI | `--wait` was too short | The build usually continues. Run the report command |
| Namespace error | The namespace is not linked to the Dev Hub | Link it in the Dev Hub, then retry |
Exercise
Exercise
You ran sf package version create without --wait, and the command returned immediately. How do you find out whether the build succeeded, and why might you not be able to promote the result?
Show answer
Run sf package version create report with the request ID, or run sf package version list to see the version status. Promotion fails if the build did not calculate code coverage (no --code-coverage flag) or if coverage is below 75%.
Exercise
Your sfdx-project.json has "versionNumber": "1.0.0.NEXT". After three successful builds, what is the number of the third beta?
Show answer
1.0.0-3. NEXT resolves to the next build number each time, and the build number follows the hyphen in the version alias.