Sub topic 3
Describing packages in sfdx-project.json
This chapter assumes you can use the sf CLI in a Salesforce DX project and read JSON. It also assumes you know what a Dev Hub is. The package type chapter covered unlocked and managed packages, and the version number chapter covered MAJOR.MINOR.PATCH.BUILD. Here you will see where that information lives in the project.
What the file does
sfdx-project.json sits in the root of a Salesforce DX project. The CLI reads it for three jobs:
- It maps folders in your repository to packages.
- It records the version you intend to build next for each package.
- It stores the IDs that connect package names and versions to records in your Dev Hub.
The CLI and your own edits both change this file. sf package create and sf package version create write to it, and you edit it by hand to set version numbers, dependencies, and ancestry. Commit it to source control with the rest of the project, because a package version build depends on it.
A complete single-package example
This file describes one unlocked package called Utils, stored in the util folder.
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionDescription": "Shared helper classes",
"versionNumber": "1.1.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA"
}
}The top-level keys apply to the whole project. The entries in packageDirectories describe individual folders.
Top-level keys
| Key | Meaning |
|---|---|
| `packageDirectories` | An array with one entry per source folder. Required. |
| `namespace` | The namespace for packages in this project. Use an empty string for unlocked packages without a namespace. A managed package needs a namespace that is linked to your Dev Hub. |
| `sfdcLoginUrl` | The login URL used when you authorize an org. The default is `https://login.salesforce.com`. |
| `sourceApiVersion` | The API version used when the CLI deploys, retrieves, and builds your source. |
| `packageAliases` | A map from readable names to Salesforce IDs. |
Package directory entries
Each object in packageDirectories can carry these keys.
| Key | Meaning |
|---|---|
| `path` | The folder that holds the source, relative to the project root. Required. |
| `package` | The package name. It must match a key in `packageAliases`. |
| `versionName` | A label for the version, such as "Utils 1.1". |
| `versionDescription` | Free text that describes the version. |
| `versionNumber` | The version you are building, in `MAJOR.MINOR.PATCH.BUILD` form. |
| `default` | Marks the folder the CLI uses when you do not name one. |
| `dependencies` | A list of packages this package needs. The next chapters cover this key. |
| `ancestorId` or `ancestorVersion` | The version this one upgrades from. A later chapter covers this. |
| `definitionFile` | A scratch org definition file used when building the version. |
| `postInstallScript`, `uninstallScript` | Apex classes that run after install or uninstall. Managed packages support these. |
Details that cause trouble
- Exactly one default. Exactly one directory must have
"default": true. If none or several do, the CLI reports an error. - Four-part version numbers. A
versionNumberhas four parts. You can writeNEXTonly in the build position, as in1.1.0.NEXT. At build time the CLI replaces it with the next unused build number for that major, minor, and patch combination. - Matching names. The
packagevalue must match an alias exactly, including case and spaces.Sales AppandSalesAppare different keys. - Unpackaged folders. A directory without a
packagekey is plain source. The CLI deploys it in project pushes, but it never goes into a package version. Teams use these folders for sample data or org-specific setup. - One owner per component. Keep each component in only one package directory. Two packages that both claim the same Apex class cause build and install conflicts.
Two packages in one project
A project can hold several packages. The order of packageDirectories matters, because the CLI processes the entries from top to bottom when it pushes source to a scratch org. List a package before any package that depends on it.
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.1",
"versionNumber": "1.1.0.NEXT",
"default": false
},
{
"path": "app",
"package": "Sales App",
"versionName": "Sales App 2.0",
"versionNumber": "2.0.0.NEXT",
"default": true,
"dependencies": [
{ "package": "Utils", "versionNumber": "1.1.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA",
"Sales App": "0Ho000000000002AAA"
}
}Here app is the default, so a command that does not name a package works on Sales App. Utils comes first because Sales App needs it. Each package has its own versionNumber, and the two numbers do not have to match.
The packageAliases map
packageAliases holds two kinds of entries, and the ID prefix tells them apart.
| Alias form | Example | ID prefix | Points to |
|---|---|---|---|
| Package name | `Utils` | `0Ho` | The package record in the Dev Hub |
| Name, version, and build | `[email protected]` | `04t` | One specific package version |
The 04t entry is the subscriber package version ID. This is the ID you install and the ID a dependency can pin. You rarely type these IDs yourself, because the CLI adds them.
Letting the CLI write the file
You can create a package from the command line and have the CLI update the project file.
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHub
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHubAfter the first command, sfdx-project.json has a Utils alias that maps to a new 0Ho ID. If no entry for the path exists, the CLI also adds a package directory entry with placeholder values. Change the versionName and versionNumber to what you want. After the second command succeeds, the file gains an alias like [email protected] that maps to the new 04t ID.
Choosing and changing the version number
The versionNumber in the file is your plan for the next build. It is not a record of what you have already released. The usual workflow is:
- Leave the number at
1.1.0.NEXTwhile you build betas for that release. - Promote the version you want to ship.
- Edit the file to the next number, such as
1.2.0.NEXT, before the next build.
If you build a version that has the same major, minor, and patch numbers as a promoted version, the CLI refuses. A promoted version cannot be replaced. So after promotion, always raise at least one of the first three numbers.
Validating your edits
Hand edits fail in predictable ways. Check these before you build a version:
- The JSON parses. A trailing comma or a missing quote makes the CLI fail before it reads any package data.
- Every
packageand every dependencypackagehas a matching alias. - Exactly one
"default": trueexists. - Each
pathpoints to a real folder. - The
namespacevalue matches the package type. An unlocked package without a namespace needs an empty string, and a namespaced package needs the real namespace.
Exercise
A project has two directories, core for package Core and ui for package UI, and UI depends on Core. A teammate lists ui first and sets "default": true on both entries. Name the two problems and say how to fix each one.
Show answer
First, two entries have "default": true, but exactly one must. Remove the flag, or set it to false, on one entry. Second, ui is listed before core, but the order should put Core first because UI depends on it. Move the core entry above ui so that a push to a scratch org processes Core before UI.