Sub topic 7
Building a package version
This chapter assumes you have already authorized a Dev Hub, linked your namespace, written sfdx-project.json, and run sf package create. It also assumes you can use the sf CLI and read JSON. Here you will build your first version and learn what happens during the build. You will also learn how to read the result and fix the usual failures.
What a package version is
A package version is an immutable snapshot of the metadata in one package directory. You build it from source, so the source files in your project are the only input. Nothing is retrieved from an org. After the build, the version cannot be edited. To change something, you build a new version.
Each build produces two identifiers:
| Identifier | Prefix | Meaning |
|---|---|---|
| Package version create request ID | `08c` | Tracks the asynchronous build job |
| Subscriber package version ID | `04t` | The ID you install, and the ID you promote |
The CLI also writes an alias such as [email protected] into the packageAliases section of sfdx-project.json. The number after the hyphen is the build number. The alias is a convenience for you. The 04t ID is what Salesforce uses.
Running the build
The command from the plan looks like this:
sf package version create --package AcmeApp --installation-key-bypass --code-coverage --wait 20 --target-dev-hub devhubEach flag matters:
--packagenames the package, by alias or by its0HoID. If your project has only one package directory, you can omit--path. Otherwise, add it.--installation-key-bypassstates that no password is needed to install. The alternative is--installation-key, which sets one. You must pick one of the two, or the command fails.--code-coveragemakes the build run all Apex tests in the package and calculate coverage.--wait 20keeps the terminal attached for up to 20 minutes. Without it, the command returns immediately and the build continues on the server.--target-dev-hubpoints at the Dev Hub that owns the package.
The version number comes from sfdx-project.json. In the earlier example, 1.0.0.NEXT tells Salesforce to use the next free build number for 1.0.0. You can override it on the command line with --version-number, and --version-name overrides the name.
What happens during a build
The build runs on Salesforce servers. Your CLI does not deploy anything itself. It uploads your source and then waits and polls.
Three consequences follow from this design.
- The temporary org is built from your scratch org definition file, usually
config/project-scratch-def.json. If your metadata needs a feature or setting, such as a platform feature or a license, it must be in that file. Otherwise the deployment fails in the build org even though it works in your own scratch org. - Only metadata inside the package directory ships. A field that your Apex references but that lives in another directory is a missing dependency. The build fails with a compile error.
- Because the build deploys into a clean org, hidden dependencies on data or settings in your development org show up here.
Checking progress and reading results
If you did not use --wait, or the wait expired, the build keeps running. You can check it with the request ID printed by the first command:
sf package version create report --package-create-request-id 08c000000000001AAA --target-dev-hub devhubThe status moves through Queued, InProgress, Success, or Error. When the build fails, the report lists error messages. Run the build again with --verbose for more detail during the wait.
To see all versions of a package, including their number, branch, coverage, and whether they are released, use:
sf package version list --packages AcmeApp --target-dev-hub devhubYou can use --verbose here too, to see extra columns such as ancestor and tags.
Code coverage and promotion
Managed packages need at least 75% Apex code coverage before you can promote a version to released. If you build without --code-coverage, the version records no coverage and cannot be promoted. You can still install it in scratch orgs and sandboxes for testing. In practice, you should build with --code-coverage for every version you might release, because a rebuild takes time.
Coverage is calculated across the package's own Apex, using the tests in the package. Tests in other packages or in your org do not count. If coverage is 74%, the build can succeed, but the later promote step fails. Check coverage in the version list --verbose output before you try to promote.
Branches and tags
Two optional flags help when many people build versions of the same package.
--branchrecords the source branch name, such asmainorfeature-x. You can then filter lists and look up the latest build for a branch.--tagattaches a free-text label, such as a commit hash. Tags are useful for tracing a version back to the exact source it came from.
sf package version create --package AcmeApp --installation-key-bypass --code-coverage --branch main --tag "$(git rev-parse --short HEAD)" --wait 20 --target-dev-hub devhubBranches matter most with .LATEST dependencies and in CI, which later chapters cover.
Ancestry during the build
For a managed package, the build checks the ancestorVersion setting. The first version uses "ancestorVersion": "NONE". For later versions, set a specific released version or HIGHEST, as in the spine's later example. Salesforce validates that the new version is a legal upgrade from its ancestor. For example, it checks that you did not delete a global Apex class that the ancestor released. If the check fails, the build stops with an error that names the offending component.
During development you can add --skip-ancestor-check to bypass that check. Treat it as a development-only shortcut, because the resulting version cannot be promoted.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Command fails before starting, mentioning an installation key | Neither `--installation-key` nor `--installation-key-bypass` was given | Add one of them |
| `Error` status with a compile failure | Code references metadata outside the package directory | Move the metadata in, or declare a dependency |
| Deploy error about a missing feature or setting | Scratch org definition file lacks it | Add the feature or setting to the definition file |
| Build succeeds but promote fails | Coverage under 75%, or no coverage calculated | Add tests, then rebuild with `--code-coverage` |
| Wait expires with a timeout message | Build still running on the server | Run `version create report` with the `08c` ID |
| Build fails an ancestor check | A released global component was removed or changed incompatibly | Restore it, or choose another ancestor |
What you have after a successful build
The result is a beta version. It is installable in scratch orgs and sandboxes, which makes it safe to test. It is not installable in production, and it cannot go to AppExchange. The next chapters cover testing beta versions and promoting them to released.
Exercise
Your build finishes with Success, but sf package version promote fails. The build command was sf package version create --package AcmeApp --installation-key-bypass --wait 20. What is the most likely cause, and what do you do?
Show answer
The build did not use --code-coverage, so no coverage was calculated and a managed package version cannot be promoted without at least 75% coverage. Rebuild with --code-coverage, which creates a new version with a new build number. Then check that coverage is at least 75% in sf package version list --verbose, and promote the new version.
Exercise
A build fails with a compile error that says a custom object is not found. The object exists in your scratch org. Name two possible causes.
Show answer
The object may be in a different package directory, so it is outside the package being built. Or it may exist in your scratch org only because of a feature or setting that the scratch org definition file used for the build does not enable. In both cases, the clean build org does not have what your development org had.