Sub topic 17
Moving an existing 1GP package to 2GP
This chapter covers converting a first-generation (1GP) managed package into a second-generation (2GP) managed package, so existing subscribers keep upgrading along the same line of versions. It is the lowest-weight topic in the plan, and it builds on the other chapters rather than replacing them.
It assumes you have read the earlier chapters on the Dev Hub, namespaces, sfdx-project.json, package versions, ancestry, and promotion. It also assumes you can run Salesforce CLI (sf) commands and read JSON.
Why migrate at all
A 1GP managed package is built in a packaging org. That org holds the metadata, the namespace, and the version history. A 2GP package is built from source files in version control, using a Dev Hub.
Teams migrate for these reasons:
- Source in Git becomes the source of truth, not a packaging org.
- Version creation can run in CI.
- Dependencies, ancestry, and multiple packages per namespace become available.
- Developers build and test in scratch orgs instead of sharing one packaging org.
Migration does not create a new product. Subscribers who already installed your 1GP package should be able to upgrade to the first 2GP version. The AppExchange listing continues to point at your package.
What you need first
Check these before running anything.
| Requirement | Why it matters |
|---|---|
| The 1GP package is managed and has at least one released version | Conversion starts from a released 1GP version line. |
| The package's namespace is linked to your Dev Hub | 2GP packages use namespaces linked to the Dev Hub. A 1GP namespace lives in a namespace org (the packaging org). |
| You have the 1GP package ID, which starts with `033` | The conversion command takes this ID. |
| Your source is retrievable from the packaging org | You need the metadata as source files for later 2GP builds. |
| You know which metadata types your package uses | Some metadata types are not supported in 2GP. Check the current "Unsupported Metadata Types" list before you start. |
Linking the namespace is the same step you did for a new package. In Dev Hub, open Namespace Registries, choose Link Namespace, and sign in to the org that owns the namespace. You can run the conversion only from a Dev Hub that has the namespace linked.
The migration flow
The conversion has three stages: convert, verify, then promote and publish.
Step 1: Convert the package
sf package convert reads your 1GP package and creates a 2GP package version from it. Run it against the Dev Hub.
sf package convert --package 033000000000001AAA --installation-key-bypass --wait 60 --target-dev-hub devhubReplace the 033... value with your real 1GP package ID. The flags work like they do for sf package version create:
--installation-key-bypassskips the installation key requirement. Leave it out if you want a key.--wait 60waits up to 60 minutes. Conversions of large packages can take a long time.--target-dev-hubnames the Dev Hub that has the namespace linked.
Run sf package convert --help for your CLI version. Flags such as --seed-metadata and --patch-version exist for special cases, and their behavior can change between releases.
The command creates a 2GP package in the Dev Hub and a first beta package version. It also prints a package version ID that starts with 04t. A converted version is built from the contents of the 1GP package. It is not built from your source directory.
Step 2: Get the source into a project
The conversion creates the package record, but it does not give you source files. Retrieve the metadata from the packaging org into a Salesforce DX project. You can do this with sf project retrieve start, using a manifest that lists your package components.
Then add the package to sfdx-project.json. The package alias entry points to the converted package, and the directory entry names it.
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "First 2GP release",
"versionNumber": "1.4.0.NEXT",
"ancestorVersion": "HIGHEST"
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"AcmeApp": "0Ho000000000001AAA"
}
}Two details matter here:
- The version number must be higher than the last released version in the line you are continuing. If your last 1GP release was 1.3, the next 2GP version number can be 1.4.0.NEXT.
ancestorVersionfollows the same rules as a normal 2GP package.HIGHESTpicks the highest released version, which is the converted one once you promote it.
The 0Ho value is a placeholder. The CLI writes the real package ID into packageAliases when the package is registered.
Step 3: Test the upgrade path
Do not promote before you test an upgrade. In a sandbox or scratch org that has your last released 1GP version, install the converted beta version. Check these things:
- The install succeeds as an upgrade, not as a fresh install.
- Existing data and configuration are intact.
- Apex tests pass and code coverage meets the 75% requirement for promotion.
- Components subscribers use, such as custom objects, flows, and permission sets, still behave as before.
A beta version can be installed only in sandboxes and scratch orgs, so this test costs nothing in production.
Step 4: Promote and publish
Promote the version like any other 2GP version.
sf package version promote --package [email protected] --target-dev-hub devhubAfter promotion, subscribers can install it in production. Later versions are built with sf package version create from your source, and they follow the normal ancestry rules from the earlier chapters.
Gotchas and edge cases
- No return trip. After your first released 2GP version, build new versions in 2GP only. Do not keep releasing from the old packaging org. Two parallel version lines confuse subscribers and break upgrade paths.
- Deleted components. In a managed package, you cannot delete certain published components. A 1GP package may hold components that 2GP source no longer represents. When your retrieved source and the converted package disagree, compare them before building the next version.
- Unsupported metadata. A component type that 2GP does not support can block the conversion or your next build. Find these in advance.
- Namespace ownership. If conversion fails with a namespace error, confirm the namespace is linked to the same Dev Hub you are targeting. A package under the same namespace cannot be created from a different Dev Hub.
- Apex test failures. The 1GP line may not have enforced coverage the way 2GP does. Fix failing tests in the packaging org first, or conversion and later builds will fail.
Review
You now have the full path: link the namespace, convert with sf package convert, retrieve source, configure sfdx-project.json, test the upgrade, promote, and continue in 2GP.
Exercise
Your team has a released 1GP managed package with version 2.0, and its namespace is registered in the packaging org. You want to release the first 2GP version as an upgrade for existing subscribers. List the order of steps, and say what must be true about the new version number.
Show answer
First, link the packaging org's namespace to your Dev Hub. Second, run sf package convert with the 033 package ID against that Dev Hub, which creates a beta 2GP version. Third, retrieve the package metadata into a DX project and add the package alias and a package directory entry to sfdx-project.json. Fourth, test installing the beta as an upgrade over version 2.0 in a sandbox or scratch org, and confirm the tests and code coverage pass. Fifth, promote the version with sf package version promote. The new version number must be higher than the last released 1GP version, for example 2.1.0.NEXT, so subscribers see it as an upgrade.
Exercise
After converting, a colleague proposes publishing the next patch from the old 1GP packaging org "just this once". Why is that a problem?
Show answer
It creates two parallel version lines for the same package. The 1GP line and the 2GP line would not share ancestry, so subscribers could be offered versions that do not upgrade cleanly from one another. After the first released 2GP version, all new versions should be built from source with sf package version create.