Sub topic 1
What second-generation packaging is and why it exists
Prerequisites
This chapter assumes you already know:
- Salesforce basics: orgs, metadata, and Apex.
- Salesforce DX basics: scratch orgs and source format.
- The command line and Git.
- How to read a JSON configuration file.
You do not need to have built a package before. This chapter gives you the working model that the rest of the tutorial builds on, and no packaging commands are needed to follow it.
The problem 2GP solves
A package is a container of metadata that you can install in other orgs. Salesforce has two generations of packaging.
First-generation packaging (1GP) came first. You build the app in a special org called a packaging org. The metadata in that org is the source of truth. To release, you upload a version from the org. This design causes real problems for teams that use modern development practices:
- The org holds the only authoritative copy of the app, so version control is a mirror at best.
- One packaging org maps to one package, so splitting a large app into smaller pieces is hard.
- Losing or damaging the packaging org puts the package at risk.
- Builds are manual uploads, which are hard to run in CI.
Second-generation packaging (2GP) reverses the direction. Your source files in version control are the source of truth. You describe the package in sfdx-project.json, and the Salesforce CLI asks a Dev Hub to build a version from that source. A Dev Hub is an org with the Dev Hub feature enabled. It keeps the records for your packages and versions.
Package types
Package type is a separate question from generation. The type decides how the package behaves after it is installed.
| Type | Namespace | Subscriber can edit components | Upgrades | AppExchange listing |
|---|---|---|---|---|
| Unmanaged | Optional | Yes, fully | None. Each install is a copy. | No |
| Unlocked (2GP) | Optional | Yes, with some limits | Yes | No |
| Managed (1GP or 2GP) | Required | Only what you expose | Yes | Yes, after security review |
Unmanaged packages are a one-time distribution of metadata. Unlocked packages are meant for your own org or your own customers' orgs, where administrators may need to change components. Managed packages are for software you distribute to customers you do not control. This tutorial focuses on 2GP managed packages.
Why the managed type exists
A managed package protects both sides of the relationship between vendor and subscriber. A subscriber is an org that installs your package.
- The namespace prefix (such as
acme__) keeps your component names from colliding with the subscriber's. - The Apex code in a released managed package is hidden from subscribers, which protects your intellectual property.
- Upgrade rules stop you from removing or changing certain components in ways that would break subscribers. For example, you cannot delete a global Apex class or a custom field that is already in a released version.
- Only managed packages can go through security review and be listed on AppExchange.
The next chapter covers these rules in detail. For now, remember that they apply to released versions. They are what make safe upgrades possible.
The working model of 2GP
Four pieces fit together:
- Dev Hub. An org that owns your package records and builds versions. It must be a production org or a Developer Edition org with Dev Hub enabled. A sandbox cannot be a Dev Hub.
- Namespace. A short unique prefix that you register in a Developer Edition org and link to the Dev Hub. A managed package needs one.
- Project. A Salesforce DX project whose
sfdx-project.jsonlists package directories, names, and version numbers. - Package and package versions. The package is the named record in the Dev Hub. A package version is an immutable snapshot of your source at one moment.
Immutable means a version never changes after it is built. If you find a bug, you build a new version. This is why 2GP fits CI well: every build is a new, identifiable artifact.
Packages and versions have different ID prefixes. A package ID starts with 0Ho, and a package version ID starts with 04t. The 04t ID is what you use to install a version, and it is what your install links contain.
You can see the Dev Hub's view of your packages with one command. This assumes you have already authorized the Dev Hub under the alias devhub, which a later chapter covers.
sf package list --target-dev-hub devhubWhat 2GP changes in practice
| Concern | 1GP | 2GP |
|---|---|---|
| Source of truth | Packaging org | Version control |
| Build tool | Upload button in the org | Salesforce CLI and Dev Hub |
| Packages per namespace | One per packaging org | Several packages can share a namespace |
| Dependencies | Limited | Declared in `sfdx-project.json` |
| Version testing | Install in test orgs | Create scratch orgs from the version |
Sharing a namespace across several packages is useful for large apps. You can split a monolith into a base package and extension packages, then version each one on its own schedule.
Common errors and misconceptions
- Treating the Dev Hub as the place you develop. You do not edit metadata in the Dev Hub. Develop in scratch orgs and commit source to Git.
- Choosing the wrong package type. The type is fixed at creation. If you create an unlocked package and later need a managed one, you create a new package.
- Forgetting the namespace link. A managed package cannot be created until the namespace is linked to the Dev Hub. The error message mentions the namespace, but the fix is in the Dev Hub setup, not in your project file.
- Expecting to edit a released version. Released versions are immutable. Changes go into the next version.
- Assuming a 1GP package converts automatically. Existing 1GP packages follow a separate migration path. The last chapter of this tutorial covers it.
Where this chapter fits
The rest of the tutorial follows the order of a real release. You prepare the Dev Hub and namespace, configure the project, create the package, build and test versions, promote a version to released, and install it. After that come ancestry for upgrades, security review, licensing, and CI.
Exercise
A teammate says, "Our team keeps source in Git, so we are already using second-generation packaging." Explain what is missing from that claim, and name the two things that must exist before you can create a 2GP managed package.
Show answer
Keeping source in Git is not enough. 2GP means a Dev Hub builds package versions from your source through the Salesforce CLI, with sfdx-project.json describing the package. Before you can create a 2GP managed package you need a Dev Hub (a production or Developer Edition org with Dev Hub enabled) and a namespace linked to that Dev Hub.
Exercise
You released version 1.0 of a managed package and found a bug in an Apex method. A colleague proposes editing the released version in place. Why is that impossible, and what do you do instead?
Show answer
Package versions are immutable snapshots. You fix the bug in source, commit it, and build a new package version, which you test and then promote to released so subscribers can upgrade.