Sub topic 5
packageDirectories and the default directory
This chapter assumes you can generate a DX project, read JSON, and run sf project deploy start. You should also know that source format places metadata under force-app/main/default.
What packageDirectories does
packageDirectories is an array in sfdx-project.json. Each entry names a folder, relative to the project root, that holds deployable source. The CLI reads this list to decide which folders to scan when you deploy, retrieve, or run source tracking. Source outside these folders is invisible to those commands.
A generated project has one entry:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The path value is a folder path from the project root. Use forward slashes, even on Windows.
The default directory
Exactly one entry should set "default": true. The CLI uses that folder as the landing place for new source. For example, when you run sf apex generate class, the file goes into the default directory unless you pass --output-dir. Retrieves of metadata that is not yet in the project also land there.
The default flag does not limit deploys. A deploy with no flags covers every directory in the array.
Using more than one directory
You can list several folders. A common reason is to keep unrelated code apart, such as shared utilities and one feature.
{
"packageDirectories": [
{ "path": "force-app", "default": true },
{ "path": "unpackaged" }
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}To act on one directory only, pass its path:
sf project deploy start --source-dir unpackagedDirectory order matters when entries depend on each other. A later chapter covers dependencies. For now, remember that the CLI processes the array from top to bottom.
Rules to follow
- Every
pathmust exist on disk. A missing folder causes an error when the CLI reads the project. - Do not nest one package directory inside another. Overlapping paths make it unclear which entry owns a file.
- Do not list the same component twice across directories. The CLI cannot tell which copy to deploy.
- Keep exactly one
default: true. With a single entry, the CLI treats it as the default even if you leave the flag out. With several entries and no flag, commands that need a default fail.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a path does not exist | Typo in `path`, or the folder was never created | Correct the path or create the folder |
| Error about no default package directory | Several entries, none marked `default: true` | Set `default` on one entry |
| Error about multiple defaults | More than one entry has `default: true` | Leave it on one entry only |
| New class lands in the wrong folder | The wrong entry is the default | Move `default: true` to the intended entry |
| Components are not deployed | They sit in a folder missing from the array | Add the folder to `packageDirectories` |
Exercise
Your project lists two directories, force-app (default) and unpackaged. You run sf project deploy start with no flags. Which directories are deployed, and where does a newly generated Apex class go?
Show answer
Both force-app and unpackaged are deployed, because a deploy without flags covers every listed directory. The new Apex class goes into force-app, because it is the entry with "default": true.
Exercise
A teammate adds { "path": "feature-app" } to the array but forgets to create the folder. What happens, and how do you fix it?
Show answer
The CLI reports that the path does not exist when it reads the project. Create the feature-app folder, or remove or correct the entry.