Sub topic 14
Troubleshooting common dependency and version errors
This chapter assumes you can read sfdx-project.json, run sf package commands, and know the difference between beta and released versions. It also assumes you know what the NEXT and LATEST keywords do. The goal is a repeatable way to find out which stage failed and why.
Exact error text changes between CLI and release versions. Treat the messages here as paraphrases, and match on the cause.
Start by finding the stage that failed
Every failure happens at one of four stages. Each stage has its own typical causes, so locate the stage first.
- Project validation. The CLI reads
sfdx-project.jsonand resolves aliases and dependencies before it contacts the Dev Hub. - Version creation. The Dev Hub builds a temporary org, deploys your metadata plus dependencies, and runs checks.
- Promotion. A beta version becomes released.
- Installation or upgrade. The version is deployed into a target org.
Stage 1: configuration errors
These fail fast and cost nothing, so fix them first.
Alias not found
A dependency such as { "package": "[email protected]" } needs a matching key in packageAliases. The CLI reports that it cannot find an ID for the package. Likely causes:
- The alias is misspelled. Aliases are case-sensitive and must match exactly, including the
@1.1.0-1suffix. - The version was created on another machine and its alias never reached your committed file. The
sf package version createcommand normally writes the new alias for you, so check that the change was committed. - The alias points at the wrong ID prefix. Package IDs begin with
0Ho. Package version IDs begin with04t. A pinned dependency alias must map to a04tID.
Dependency listed after its dependent
Packages build in the order they appear in packageDirectories. A dependency from the same project must appear before the package that uses it. This ordering is wrong:
{
"packageDirectories": [
{
"path": "app",
"package": "Sales App",
"versionNumber": "2.0.0.NEXT",
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
},
{
"path": "util",
"package": "Utils",
"versionNumber": "1.1.0.NEXT"
}
]
}Move the util entry above app, as in the first example of this tutorial. The same rule applies to dependencies of dependencies. Every package in the chain must precede the packages that need it.
Build number conflicts
NEXT asks the Dev Hub to assign the next free build number. If you hard-code a number such as 1.1.0.3 and that build already exists, creation fails. Use NEXT in versionNumber for the package you are building. Reserve explicit build numbers for pinned dependency references only.
Also remember the shape of a version. MAJOR.MINOR.PATCH.BUILD has four parts. A dependency declared with only three parts, such as 1.1.0, is incomplete for a version number. Use 1.1.0.LATEST or pin through an alias.
Stage 2: errors while creating a version
The build org is a clean slate. It contains only your package's metadata, the declared dependencies, and what the edition provides. Anything else your code needs will be missing.
Missing metadata dependencies
A typical symptom is a message such as "no such field" or "invalid type" for a component you can see in your own org. The component lives in another package, in an unpackaged directory, or in your org's settings. Fix it in one of these ways:
- Add the package that owns the component as a dependency.
- Move the component into the package directory that needs it.
- Add the required feature or setting to the scratch org definition file used for the build, such as
config/project-scratch-def.json. This applies when the dependency is on a platform feature rather than metadata.
Dependency resolves to an unexpected version
LATEST replaces the build number with the highest existing build for that major, minor, and patch. It does not stop at released builds, so a beta can be selected. If your tests pass today and fail tomorrow with no change from you, the dependency probably moved.
sf package version list --packages Utils --target-dev-hub myDevHub
sf package version report --package "[email protected]" --verbose --target-dev-hub myDevHubThe first command lists the builds that exist and their status. The second shows the details of one version. When reproducibility matters, pin the dependency with an alias so the build resolves the same 04t ID every time.
Timeouts
The --wait flag only limits how long the CLI waits. The build continues on the server. If the command times out, look the request up instead of starting a second build:
sf package version create list --target-dev-hub myDevHub
sf package version create report --package-create-request-id 08c000000000001AAA --target-dev-hub myDevHubStarting another build wastes a build number and can leave two similar betas to confuse your team.
Code coverage
Add --code-coverage when creating a version you intend to promote. Without it, the version builds but cannot be promoted, because the platform has no coverage data. Using --skip-validation speeds up builds and has the same consequence, so use it only for development betas.
Stage 3: errors when promoting
Promotion is a one-way step. Released versions cannot be changed back to beta, and they should not be deleted.
| Symptom | Likely cause | Fix |
|---|---|---|
| Promote rejected for coverage | Version built without `--code-coverage`, or Apex coverage is below the required minimum (75% for packages in most cases) | Add tests, then build a new version with `--code-coverage` |
| Promote rejected because of a dependency | A dependency is still a beta | Promote the dependency first, then rebuild your package against the released version |
| Promote says version is already released | Promotion was run twice, or a teammate promoted it | Run `sf package version list` to confirm status. No further action is needed |
| Version cannot be installed in production | The version is still beta | Beta versions install only in scratch orgs and sandboxes, so promote first |
The order for a chain of packages is always the same. Build and promote the lowest dependency first. Rebuild each dependent package against that released version. Then promote the dependents.
Stage 4: errors when installing and upgrading
Dependency not installed
Installing Sales App into an org that lacks Utils fails. The installation order is dependencies first, from the bottom of the chain upward. Install them one at a time:
sf package install --package "[email protected]" --target-org myOrg --wait 10
sf package install --package "Sales [email protected]" --target-org myOrg --wait 10For a whole project, deploy in the order the packages appear in packageDirectories, since that order already satisfies dependencies.
Installed dependency is too old
A dependency on Utils 1.1 is not met by an org that has 1.0 installed. Upgrade the dependency first. An org with a higher version than required is usually fine, though you should check for breaking changes in a major jump.
Installing a lower version
You cannot install a version lower than the one already in the org. If you need to remove a bad release, the options are to publish a newer fixed version, or to uninstall the package where that is allowed. Plan fixes as upgrades, never as rollbacks.
Ancestry problems
For managed packages, each new version has an ancestor, the earlier version it upgrades from. Common errors:
- The new version number is not higher than its ancestor.
- The ancestor is not a released version.
- Components that exist in the ancestor were removed or changed in a way the platform does not allow after release.
Check the ancestor setting in sfdx-project.json (ancestorVersion or ancestorId), and compare the old and new versions before you build. Remember that removing public or global Apex interface members from a released managed package is restricted.
Apex that behaves differently per caller
If a subscriber reports old behavior after an upgrade, check whether the code uses System.requestVersion(). Callers compiled against an older package version intentionally receive the older behavior. Look at the packageVersions entry in the component's -meta.xml file to see which version it was saved against. Update that entry when the caller should adopt the new behavior.
Org-dependent unlocked packages
An org-dependent unlocked package depends on metadata that exists in the target org rather than in a package. It builds without validating those references, so errors appear later. If an install fails on missing metadata, confirm the target org really has the components, or move them into a package and declare the dependency.
Exercise
A teammate's build of Sales App passed yesterday. Today it fails, and nobody changed the Sales App code. The dependency is declared as 1.1.0.LATEST. What is the most likely cause, and how do you prevent a repeat?
Show answer
Someone published a newer build of Utils 1.1.0, so LATEST resolved to a different version. Prevent it by pinning the dependency with an alias such as [email protected], which maps to one fixed 04t ID.
Exercise
sf package version promote for Utils succeeds, but promoting Sales App fails. Sales App was built last week against [email protected], a beta. What are the steps to fix it?
Show answer
Promote the Utils version that Sales App should depend on, update the dependency to that released version, build a new Sales App version, then promote it.
Exercise
An install of Sales App into a sandbox fails and Utils is not installed there. Which command runs first, and why?
Show answer
Install Utils first with sf package install. Dependencies must already exist in the target org before the packages that depend on them.