Sub topic 8
Building a package version
A package version is an immutable, installable snapshot of the metadata in one package directory. This chapter covers what happens when you build one, which flags matter, and how to read the result and the common failures.
This chapter assumes you have a Dev Hub with a package record already created (sf package create). It also assumes an sfdx-project.json that lists the package directory, and that you can run Apex tests and read coverage numbers.
What a build does
Running sf package version create does not deploy to your org or to a scratch org you control. The CLI sends the contents of the package directory to the Dev Hub. The Dev Hub then does the following:
- Validates
sfdx-project.jsonand the package definition. - Resolves the dependencies you declared.
- Deploys the metadata into a temporary, isolated org to prove it deploys cleanly.
- Runs Apex tests if you asked for code coverage.
- Stores the result as a package version and gives it a subscriber package version ID that starts with
04t.
Because the build runs from the files in your package directory, only committed and saved local changes matter. Build from a clean checkout so the version matches a known commit.
The build command
This is the command from the project plan:
sf package version create --package Core --installation-key-bypass --code-coverage --wait 20 --target-dev-hub MyDevHubEach flag has a purpose:
| Flag | Purpose |
|---|---|
| `--package Core` | The package to build. You can use the alias or the `0Ho` ID. |
| `--installation-key-bypass` | Allows installation without a password. |
| `--installation-key` | Sets a password that installers must supply. Use this or the bypass flag, not both. |
| `--code-coverage` | Runs the Apex tests and records coverage. |
| `--wait 20` | Blocks for up to 20 minutes instead of returning immediately. |
| `--target-dev-hub` | The Dev Hub that owns the package. |
You must choose either --installation-key or --installation-key-bypass. If you leave out both, the command fails.
The --skip-validation flag makes the build faster by skipping the deploy check and the tests. A version built this way can never be promoted, so use it only for quick experiments in scratch orgs.
Version numbers during a build
The versionNumber in sfdx-project.json has four parts: major, minor, patch and build. With 1.0.0.NEXT, the Dev Hub assigns the next unused build number for that major.minor.patch. Your first build is 1.0.0.1, the next is 1.0.0.2, and so on.
The CLI shows versions in alias form with a dash, such as [email protected]. When a build succeeds, the CLI writes that alias into the packageAliases section of sfdx-project.json:
{
"packageAliases": {
"Core": "0HoXXXXXXXXXXXXXXX",
"[email protected]": "04tXXXXXXXXXXXXXXX"
}
}Commit this change. Other developers and your pipeline need the alias to install the version and to resolve dependencies.
If a package should keep an upgrade path, set the ancestor in the package directory entry. The ancestorVersion property names the version this build descends from. The ancestorId property does the same by ID. If the project lists neither, the CLI warns you, and the choice affects which upgrades are allowed later. The Upgrades and uninstalling chapter covers this in detail.
Reading build status
With --wait, the CLI prints progress and finishes with the result. If the build takes longer than the wait time, the command stops waiting but the build continues on the Dev Hub. The output includes a request ID that starts with 08c. Use it to check the build later:
sf package version create report --package-create-request-id 08cXXXXXXXXXXXXXXX --target-dev-hub MyDevHubA request moves through the statuses Queued, In Progress, Success or Error. To see all builds for a package:
sf package version create list --target-dev-hub MyDevHubTo inspect a finished version, including its coverage and whether it is released:
sf package version report --package "[email protected]" --verbose --target-dev-hub MyDevHubThe --verbose flag adds fields such as the ancestor and the coverage details.
Coverage during the build
Without --code-coverage, the version records no coverage. That is acceptable for a beta that you will only install in scratch orgs or sandboxes. A version can only be promoted if coverage was calculated and the package average is at least 75%. Every Apex class in the package counts toward this average, not only the classes your tests touch.
Therefore, build with --code-coverage in any pipeline stage that may lead to promotion. The tests that run are those in the package directory. Tests in other packages do not run, and your package cannot count on them for coverage.
Common failures
Most build errors come from the isolation of the temporary org. The org contains only your package, its declared dependencies and the standard platform.
- Missing dependency. Your metadata references a custom object, field or class that lives in another package or in the org. Fix this by adding the package to
dependencies, or by moving the metadata into the package. - Dependency not found. The dependency entry names a version that does not exist. With
LATEST, the Dev Hub looks for the highest build, and it will not find one if you never built that package at that major.minor.patch. - Test failures. Tests that rely on org data, such as
SeeAllData=trueor records created by hand, fail in the temporary org. Tests must create their own data. - Coverage under 75%. The build can succeed, and the failure shows up only when you try to promote.
- Unsupported metadata. Some metadata types cannot be packaged. The error names the type.
- Build limits. The Dev Hub limits how many versions you can create per day. Pipelines that build on every commit can reach this. Build versions only for the branches that need them.
When an error message is unclear, run the build with --verbose and read the full output from the Dev Hub.
Build once, install many
The version you build is the same artifact everywhere. Build it once, then install that exact 04t ID in the QA sandbox, in staging and in production. Do not rebuild for each environment, because a rebuild produces a different version. The next chapters cover installing and promoting that version.
Exercise
A teammate runs sf package version create --package Core --installation-key-bypass --wait 10 --target-dev-hub MyDevHub. The build succeeds, but promotion later fails. What is the likely cause, and what is the fix?
Show answer
The build omitted --code-coverage, so no coverage was calculated and the version cannot be promoted. Rebuild with --code-coverage (and make sure the package average is at least 75%). This produces a new version, for example [email protected], which is the one to promote.
Exercise
A build with --wait 5 ends with the message that the wait time expired. Is the build lost? How do you find out the result?
Show answer
No. The build continues on the Dev Hub. Run sf package version create report --package-create-request-id <08c ID> --target-dev-hub MyDevHub to see its status, or use sf package version create list to find the request.
Exercise
Why does a build fail with a reference to an object that exists in your sandbox, and how do you resolve it?
Show answer
The build runs in a temporary org that contains only the package, its declared dependencies and standard platform features. The object is not in any of those. Either move the object's metadata into the package, or put it in another package and declare that package under dependencies.