Sub topic 15
Splitting a Large Project into Modules and Unlocked Packages
This chapter assumes you have followed the trunk-based flow from the earlier chapters. You should be comfortable with sfdx-project.json, feature branches, pull request validation, and tagging a release from main. You also need a Dev Hub org, which is the org that creates and tracks package versions. Here you will see when one force-app directory stops being enough. You will also see how to split it into modules and what that changes in your branching model.
When one directory stops working
A single force-app directory works well for a small team. Problems appear as the project grows:
- Pull request validation runs every local test for every change, even when the change touches one corner of the org.
- Merge conflicts increase because many teams edit the same directory tree.
- Nobody owns the shared objects, fields, and utility classes, so any team can break them.
- Releases are all-or-nothing. A finished feature waits for an unfinished one.
You can address these in two steps. The first step is to split the source into modules. The second step, optional and heavier, is to turn modules into unlocked packages.
Step one: several package directories
The packageDirectories array in sfdx-project.json accepts more than one entry. Each entry is a module. The CLI deploys them in the order listed, so list shared modules first.
{
"packageDirectories": [
{ "path": "force-app/core", "default": true },
{ "path": "force-app/sales" },
{ "path": "force-app/service" }
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Everything is still in one repository and one main branch. Nothing about branching changes yet. You gain the ability to deploy one module at a time:
sf project deploy start --source-dir force-app/sales --target-org case-routingYou also gain a place to attach ownership. A CODEOWNERS file can require the sales team to review force-app/sales/ and the platform team to review force-app/core/.
Many teams stop here, and that is a reasonable place to stop. Modules in one repository cost little. Packages add version management you then have to maintain.
Step two: unlocked packages
An unlocked package is a versioned bundle of metadata that you install into an org. Each package version is an immutable artifact. Once you create it, its contents do not change. Installing version 1.3.0 in UAT and then in production deploys exactly the same bytes. That property is the main reason to adopt packages.
Packages also enforce a dependency graph. A package that uses a custom field from another package must declare that dependency, and the platform checks it.
Dependency direction
Design the graph before you create anything. Dependencies must point one way, toward shared code, and must never form a cycle.
Wait — the diagram must be in the required fence format. Here it is:
Two rules follow from this graph:
corecannot reference anything insalesorservice. If it needs to, move the shared piece intocore.salesandserviceshould not depend on each other. If they must share something, move it down intocore.
Creating the packages
Create each package once, starting with the one that has no dependencies. The CLI writes the package ID into a packageAliases section of sfdx-project.json.
sf package create --name core --package-type Unlocked \
--path force-app/core --no-namespace --target-dev-hub devhub
sf package create --name sales --package-type Unlocked \
--path force-app/sales --no-namespace --target-dev-hub devhubThen declare the dependency and version number on each directory:
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "core",
"versionName": "Core",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/sales",
"package": "sales",
"versionName": "Sales",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "core", "versionNumber": "1.0.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0",
"packageAliases": {
"core": "0Ho000000000001AAA",
"sales": "0Ho000000000002AAA"
}
}The two IDs under packageAliases are placeholders. Your own sf package create run writes the real ones. NEXT means the build number increments on each version creation. LATEST in a dependency means "the newest built version of core with this major, minor, and patch number."
What changes in the branching model
The trunk-based flow stays. main is still the source of truth, and feature branches are still short. Four things change.
Validation builds a package version
A check-only deploy still works for unpackaged source. For a package, the equivalent check is creating a version, which compiles the metadata and runs tests. Run it from the pull request, and restrict it with a path filter so a change to sales does not rebuild core.
name: Validate sales package
on:
pull_request:
branches: [main]
paths:
- 'force-app/sales/**'
- 'sfdx-project.json'
jobs:
build-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the Dev Hub
env:
SFDX_AUTH_URL: ${{ secrets.DEVHUB_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias devhub
rm auth-url.txt
- name: Build a package version with coverage
run: sf package version create --package sales --installation-key-bypass --code-coverage --wait 60 --target-dev-hub devhubThe sfdx-project.json entry in paths matters. A change to a version number or dependency is a real change and needs validation.
Merging builds, promoting releases
A version created on a pull request is a candidate and cannot be installed in production. After the merge, the pipeline builds the version again from main, installs it in UAT, and promotes it when UAT passes:
sf package version create --package sales --installation-key-bypass --code-coverage --wait 60 --target-dev-hub devhub
sf package install --package [email protected] --target-org uat --wait 20
sf package version promote --package [email protected] --target-dev-hub devhub
sf package install --package [email protected] --target-org production --wait 20Promotion marks the version as released. It requires at least 75% Apex code coverage in the package. Dependencies must install first, so the pipeline installs core before sales when both changed.
Tags become per-package
One repository-wide tag no longer says what is in production. Tag each package version separately, using the package name as a prefix:
git tag -a sales-v1.4.0 -m "sales 1.4.0-3 promoted"
git push origin sales-v1.4.0Hotfixes target one package
A hotfix branch still starts from main. You change only the affected module, build a new patch version, and install it. Other packages stay where they are, so a fix to service no longer ships unfinished sales work.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Version creation fails with a missing component error | The package uses metadata that lives in another directory or in unpackaged org metadata | Move the metadata into the package, or into a dependency, and declare the dependency |
| Circular dependency error | `core` references something in `sales` | Move the shared piece down into `core` |
| Promotion is refused | Coverage under 75%, or the version was built with `--skip-validation` | Rebuild with `--code-coverage` and add tests |
| Install in UAT fails on a dependency | Dependency version is not installed there yet | Install `core` first, or pin the version in `dependencies` |
| Builds are slow on every pull request | No path filter, so every package rebuilds | Add `paths` to the workflow |
Moving to packages in stages
Converting a whole org at once rarely works. A safer order is:
- Split the source into modules and add
CODEOWNERS. - Create a package for the most shared module, usually
core, and install it in a scratch org to check that it stands alone. - Package the next module, declare its dependency on
core, and add path-filtered validation. - Move UAT and production deployments to
sf package installone package at a time.
Metadata that does not fit any package, such as org-specific settings, can stay in an unpackaged directory that you deploy with sf project deploy start.
Exercise
Your team has core, sales, and service as unlocked packages. A pull request changes only a class in force-app/service. Which package version does the validation workflow need to build, what keeps the sales workflow from running, and what must already be installed in the target org for the new service version to install?
Show answer
The workflow builds a new service version with sf package version create --package service --code-coverage. A paths filter on force-app/service/** (plus sfdx-project.json) stops the sales workflow from running. The target org must already have the core version that service declares as a dependency, since service depends on core.