Sub topic 12
Setting ancestors so subscribers can upgrade
This chapter assumes you can already build a managed 2GP package version with sf package version create and promote it to released. You should also be comfortable reading sfdx-project.json. It covers how a package version's ancestor decides which subscribers can upgrade to it.
Why ancestry exists
A subscriber is an org that has installed your package. When you ship a new released version, a subscriber on an older version can upgrade in place only if that older version is in the new version's ancestry. Upgrading in place keeps the subscriber's data in your custom objects and fields.
An ancestor is the released version that a new version builds on. Each version points to one ancestor, so the pointers form a chain. Salesforce uses that chain for two jobs:
- It decides which installed versions can upgrade to which newer versions.
- It tells the build which earlier version to compare against, so you can't accidentally break subscribers.
If a subscriber's version isn't in the chain, there's no upgrade path. The only option is to uninstall, which deletes the package's data in that org.
Setting the ancestor in sfdx-project.json
You set the ancestor on the package directory entry. Two properties can do this, and you may use only one of them.
| Property | Accepts | Example |
|---|---|---|
| `ancestorVersion` | `NONE`, `HIGHEST`, or a `major.minor.patch` number of a released version | `"HIGHEST"` or `"1.0.0"` |
| `ancestorId` | A package alias or a `04t` subscriber package version ID | `"[email protected]"` |
The first version of a package has nothing to build on, so it uses NONE:
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE"
}Later versions normally use HIGHEST. The build looks up the highest released version of the package and uses it as the ancestor:
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Spring update",
"versionNumber": "1.1.0.NEXT",
"ancestorVersion": "HIGHEST"
}Use an explicit value when you need control, for example when you want the new version to build on a specific released version rather than the newest one:
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Spring update",
"versionNumber": "1.1.0.NEXT",
"ancestorId": "[email protected]"
}Don't put ancestorVersion and ancestorId in the same entry. The CLI rejects that combination.
Rules the ancestor must follow
- The ancestor must be a released version. A beta version can't be an ancestor, which is why promotion comes before the next release.
- The ancestor's version number must be lower than the new version's number.
HIGHESTresolves at build time. If the package has no released version yet, the build fails, so the first version needsNONE.- Release versions (those with a patch number of 0) form the main chain. Patch versions branch off a release version instead of extending the chain. The push upgrades chapter covers patches.
Every released version you want subscribers to upgrade through must stay in the chain. Never use NONE on a later version of a package that already has subscribers. That would start a new chain with no link to the old one.
What the build checks against the ancestor
When you build a version with an ancestor, Salesforce compares your source to the ancestor. If you've made a change that managed-package rules forbid, the build fails. Typical forbidden changes include:
- Deleting a custom object or custom field that was in a released version.
- Removing a
globalApex class, method, property, or interface member. - Changing the signature of a
globalmethod. - Changing a field's data type in a way that could lose subscriber data.
These checks protect subscribers who have already built reports, flows, and integrations on your released components. To retire a component, deprecate it in a release instead of deleting it. Check the managed-package component reference for what each metadata type allows.
A subscriber on 1.0.0 can upgrade straight to 1.2.0, because 1.0.0 is two links back in the chain. The beta 1.3.0 joins the chain only when you promote it.
Inspecting ancestry
Use displayancestry to see the chain for a package. Run it before a release to confirm that the version you expect is the one being used as the ancestor.
sf package version displayancestry --package AcmeApp --target-dev-hub devhubAdd --dot-code to print the tree as DOT source, which you can render with Graphviz:
sf package version displayancestry --package AcmeApp --dot-code --target-dev-hub devhubTo check one version's ancestor, report on it:
sf package version report --package [email protected] --target-dev-hub devhubThe output includes the ancestor ID and ancestor version for that build.
Building with the ancestor in place
Once sfdx-project.json names the ancestor, the build command is the same as before:
sf package version create --package AcmeApp --installation-key-bypass --code-coverage --wait 20 --target-dev-hub devhubAdd --skip-ancestor-check only for experiments, for example when you test a build that intentionally breaks compatibility. A version built with that flag can't be promoted, so it can never reach subscribers.
Common errors
- Build fails because the ancestor isn't released. You named a beta as the ancestor, or
HIGHESTfound nothing. Promote the earlier version, then rebuild. - Build fails on a deleted component. Restore the component, or deprecate it, so the new version stays compatible with the ancestor.
- Subscribers can't upgrade. Run
displayancestry. A common cause is that a version in the middle of your history was built withNONEor the wrong explicit ancestor, which split the chain. - The ancestor property is rejected. Check that you didn't set both
ancestorVersionandancestorId. - The wrong ancestor is used after a long gap.
HIGHESTpicks the highest released version at build time. If someone released a version in between, your build now sits on top of it. Pin an explicit ancestor if you need a different base.
Decision guide
| Situation | Setting |
|---|---|
| First version of a new package | `ancestorVersion: "NONE"` |
| Normal next release | `ancestorVersion: "HIGHEST"` |
| Build on a specific earlier release | `ancestorId` with an alias or `04t` ID |
| Testing a deliberately incompatible build | `--skip-ancestor-check`, never promoted |
Exercise
Your package AcmeApp has released versions 1.0.0 and 1.1.0. You are building 1.2.0. Which ancestor setting should the 1.2.0 entry use, and what happens if you build it with NONE instead?
Show answer
Use ancestorVersion: "HIGHEST" (or ancestorId set to the 1.1.0 alias). That makes 1.1.0 the ancestor, so subscribers on 1.0.0 or 1.1.0 can upgrade to 1.2.0 and the build checks 1.2.0 against 1.1.0 for removed or changed components. With NONE, 1.2.0 starts a new chain with no link to the older versions. Existing subscribers have no upgrade path, and the only way to move them is to uninstall, which deletes their package data.
Exercise
A teammate sets ancestorId to a beta version and the build fails. Why, and what are the steps to fix it?
Show answer
An ancestor must be a released version, and a beta isn't. Promote the beta with sf package version promote if it is ready to ship, or point the ancestor at the latest released version instead. Then rebuild.