Sub topic 10
Organizing Source Across Multiple Package Directories
This chapter assumes you can read sfdx-project.json and know that packageDirectories is its main array. It also assumes you know the standard force-app/main/default layout and have deployed source with the sf CLI. Here you will split one project into several source folders and learn how the CLI treats them.
Why use more than one package directory
A new project has one entry in packageDirectories, usually force-app. That works for a small team. Larger projects often outgrow a single folder, for these reasons:
- Shared foundations, such as utility Apex and base objects, change slowly. Feature code changes often. Separate folders keep the two apart in code review and in Git history.
- Some metadata should not ship with everything else, such as sample data setup or a test harness.
- Teams that plan to package later can start by drawing the boundaries as folders. Each folder can later become a package.
A package directory is a folder of source that the CLI treats as one unit. It does not have to be a package. Declaring it as a package is a separate step, covered in the next chapter.
Declaring several directories
Each object in the packageDirectories array names one folder with path. Paths are relative to the folder that contains sfdx-project.json.
{
"packageDirectories": [
{ "path": "core", "default": true },
{ "path": "feature", "default": false },
{ "path": "unpackaged", "default": false }
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Each folder holds its own source-format tree. The main/default structure repeats inside every directory:
my-project/
├── core/
│ └── main/default/
│ ├── classes/
│ └── objects/
├── feature/
│ └── main/default/
│ ├── classes/
│ └── lwc/
├── unpackaged/
│ └── main/default/
│ └── permissionsets/
└── sfdx-project.jsonThe main/default part is a convention. The CLI scans everything under a package directory, so a folder like core/classes also works. Keeping main/default makes it easy to move a folder between projects or to add other sibling folders, such as core/test.
Correction: the diagram below is the one to use.
The default directory
Exactly one entry should have "default": true. The CLI uses it as the destination when it needs to put source somewhere and has no better hint. In practice:
- New components that source tracking pulls from an org land in the default directory.
- Tools such as the Salesforce extensions for VS Code use it as the starting location for new files.
Other directories only receive files that you place there or that already exist there. If a component already exists in a non-default directory, a retrieve updates it in place. It does not move it.
You can override the default per command. For example, to create a class in the feature directory:
sf apex generate class --name AccountService --output-dir feature/main/default/classesOrder matters
The CLI processes package directories in the order they are listed. Two things depend on this:
- When a directory depends on another, the dependency must come first. For example, if
featurereferences custom objects incore, listcoreabovefeature. - When a command with no path flags runs, it works through every directory in this order.
For unpackaged directories, order is less critical. It is still a good habit to list shared code first, because it keeps the file readable and matches how the project is later packaged.
What a command touches
The same project can be deployed in several ways. The flags decide how much of it is sent.
| Command | What is deployed |
|---|---|
| `sf project deploy start` | All package directories, in order |
| `sf project deploy start --source-dir core` | Only the `core` directory |
| `sf project deploy start --source-dir core --source-dir feature` | Both named directories |
| `sf project deploy start --metadata ApexClass:AccountService` | One component, found in any directory |
Retrieving works the same way. A retrieve with no flags covers the project's directories. If you retrieve a specific component, the CLI writes it where it already lives. Only a new component goes to the default directory.
# Deploy only the shared foundation
sf project deploy start --source-dir core
# Then deploy the feature code that depends on it
sf project deploy start --source-dir featureDeploying in two steps like this is useful when you want to confirm that the foundation works before you add code on top of it.
One file for the whole project
Several project files apply to all directories at once:
.forceignoresits at the project root and applies across every package directory. A pattern like**/profiles/**matchescore/main/default/profilesandfeature/main/default/profilesalike.sourceApiVersion,sfdcLoginUrl, andnamespaceare project-level settings. You cannot set them per directory.- The scratch org definition in
config/is shared. Every directory is deployed into the same scratch org.
Because the settings are shared, a team that needs different API versions for different areas has to either choose one version or split the work into separate projects.
Avoiding duplicates
Do not put the same component in two directories. If core and feature both contain AccountService.cls, the CLI cannot know which copy you mean. Depending on the command, you may see a deploy error, or you may deploy one copy and silently ignore the other. Neither outcome is what you want.
To find accidental duplicates, search for the same file name under more than one directory:
find core feature unpackaged -name "AccountService.cls-meta.xml"If the command prints more than one path, remove or merge one.
A related case is metadata that several directories extend, such as a custom object that gets fields from both core and feature. This is allowed because each file is different. The object definition file lives in one place, and the field files live under that object's fields folder in whichever directory owns them:
core/main/default/objects/Invoice__c/Invoice__c.object-meta.xml
feature/main/default/objects/Invoice__c/fields/Discount__c.field-meta.xmlBoth paths are for the same object. The object must exist in the org when the field deploys, so core must come first in the array.
Common mistakes
- A path that does not exist. If
pathpoints to a folder that is not there, commands fail. Check spelling and case, because file systems on Linux are case-sensitive. - No default directory, or two of them. Set
"default": trueon one entry only. - An absolute path. Use relative paths so that teammates and CI can use the same file.
- A trailing slash or a leading
./. The CLI usually tolerates these, but keep the style consistent so that tools reading the file see the same value. - Forgetting that the order is the deploy order. Putting
featureabovecorecan produce "entity not found" errors on a clean org, even though it works in an org that already has the core metadata.
A short checklist
- Create one folder per logical area, each with a
main/defaulttree. - Add an entry for each folder in
packageDirectories, listing dependencies first. - Mark one entry as default, usually the folder most new metadata will go into.
- Deploy to a fresh scratch org with
sf project deploy start. - Search for duplicate files across directories.
Exercise
Your project lists feature before core in packageDirectories. A feature Apex class uses a custom object defined in core. A deploy to a brand new scratch org fails, but the same deploy to your long-lived sandbox works. Why, and what do you change?
Show answer
The CLI deploys directories in the listed order. In the new scratch org, the custom object does not exist yet when feature is processed, so the class fails. The sandbox already has the object from earlier deploys, which hides the problem. Move the core entry above feature in packageDirectories, then redeploy to a fresh scratch org to confirm.
Exercise
You run sf project retrieve start and the org contains a brand new Apex class that is not in your project. With three package directories and "default": true on core, which directory receives the new class? What happens to a class that already exists in feature and was changed in the org?
Show answer
The new class is written to the default directory, core. The changed class in feature is updated in place, in feature, because the CLI writes an existing component where it already lives.