Sub topic 15
Org-dependent unlocked packages
This chapter assumes you can create a standard unlocked package and a package version, and that you know how sfdx-project.json lists package directories. It covers the one package type that is allowed to depend on metadata outside the package.
The problem it solves
A standard unlocked package must be self-contained. Every component it references must be in the package, in a package it depends on, or in the base Salesforce platform. The Dev Hub checks this when it builds a version. Many long-lived orgs fail this check. A Flow may reference a field that no package owns, or an Apex class may call a managed-package component that exists only in production.
An org-dependent unlocked package skips that check. It can reference metadata that exists only in the target org. The cost is that it is only guaranteed to install in orgs that already contain that metadata.
How it differs from a standard unlocked package
| Aspect | Standard unlocked | Org-dependent unlocked |
|---|---|---|
| References to unpackaged metadata | Not allowed | Allowed |
| Version build validates references | Yes, in a clean scratch org | Skipped |
| Install target | Any org that meets the dependencies | Only orgs that contain the referenced metadata |
| Use case | Reusable, portable modules | Staging step for an org with tangled metadata |
Creating one
You choose the type when you create the package record. You cannot change it afterwards, so decide first.
sf package create --name LegacySales --package-type Unlocked --path force-app/legacy-sales --no-namespace --org-dependent --target-dev-hub MyDevHubThe command adds a packageAliases entry to sfdx-project.json. You then build a version with sf package version create, as you do for any unlocked package.
Because the build does not prove that the package installs cleanly anywhere, test it in an org that resembles production. A full or partial sandbox works well.
sf package version create --package LegacySales --installation-key-bypass --wait 20 --target-dev-hub MyDevHub
sf package install --package "[email protected]" --target-org MyFullSandbox --wait 20Where it fits in a migration
Org-dependent packages work as a stepping stone when you move existing metadata into packages.
- Put the tangled metadata into an org-dependent package. It is now versioned and installable, even though it is not portable.
- Deploy it through your pipeline to sandboxes, then production.
- Over time, move the referenced metadata into packages of its own and declare them as dependencies.
- Create a new standard unlocked package from the cleaned-up metadata and retire the org-dependent one.
Common errors
- Using it for new modules. The package then cannot be built or installed in a scratch org without recreating its dependencies by hand.
- Setting the flag late.
--org-dependentis set when the package is created. If you forgot it, create a new package. - Skipping sandbox installs. The Dev Hub does not catch missing references, so the first failure shows up at deployment.
Exercise
Why can an org-dependent unlocked package build successfully in the Dev Hub and still fail to install in an org, and what practice reduces this risk?
Show answer
The version build does not validate references to metadata outside the package, so a missing component is not detected at build time. The install fails in any org that lacks that metadata. To reduce the risk, install every version into a sandbox that closely matches production before you promote and deploy it.