Sub topic 10
Organizing source across multiple package directories
This chapter assumes you can generate a DX project, read sfdx-project.json, and run sf project deploy start. It builds on the packageDirectories array and the default directory.
Why use more than one directory
A single force-app folder works for small projects. As a project grows, you may want to split source by purpose. Common splits are shared objects and utilities in one folder, and a feature in another. Each entry in packageDirectories points the CLI at one of these folders. The CLI treats every listed folder as part of the project.
Declaring several directories
Each entry needs a path. Paths are relative to the folder that holds sfdx-project.json.
{
"packageDirectories": [
{
"path": "core",
"default": true
},
{
"path": "feature",
"default": false
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Each folder uses the same source format as force-app. A typical layout looks like this:
my-project/
├── core/
│ └── main/default/
│ ├── classes/
│ └── objects/
├── feature/
│ └── main/default/
│ ├── classes/
│ └── lwc/
└── sfdx-project.jsonWhat the default directory does
Exactly one entry must have "default": true. When you create metadata with a command such as sf apex generate class, the CLI writes it to the default directory unless you pass --output-dir. The flag does not change what gets deployed. It only changes where new files land.
When you have more than one entry, mark the default explicitly. Without it, the CLI reports an error about a missing default package directory.
Deploying and retrieving across directories
By default, sf project deploy start deploys every directory in the array. To work with only part of the project, use --source-dir.
# Deploy everything listed in packageDirectories
sf project deploy start
# Deploy only the feature folder
sf project deploy start --source-dir feature
# Retrieve only the core folder
sf project retrieve start --source-dir coreThe path you pass must sit inside one of the listed package directories. A path outside them is not part of the project, so the CLI cannot resolve it.
Order matters
The CLI processes directories in the order they appear in the array. Put code that other code relies on first. If feature references an Apex class or custom object in core, list core before feature. A wrong order shows up as deploy errors about missing components, even though the component exists in your project.
flowchart LRThe diagram below shows the dependency direction.
Same component in two directories
A component must live in only one package directory. If two folders contain a file for the same metadata, such as the same Apex class name, the CLI cannot tell which one wins. Deploys and source tracking can fail or behave unpredictably. Keep each component in a single folder and move shared items into the folder that others depend on.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Error about no default package directory | No entry has `"default": true` |
| Folder missing from deploys | The folder is not listed in `packageDirectories` |
| Path not found | `path` is misspelled or not relative to the project root |
| Missing component errors on deploy | A dependent directory is listed before the one it relies on |
Exercise
Your project lists feature first and core second. A class in feature calls a class in core, and deploy fails with a missing component error. What do you change, and why?
Show answer
Move the core entry above the feature entry in packageDirectories. The CLI processes directories in array order, so the class in core must be listed before the code that uses it.