Sub topic 5
packageDirectories and the default directory
This chapter assumes you have generated a Salesforce DX project, you can read JSON, and you know that the sf CLI reads sfdx-project.json from the project root. It covers the packageDirectories array: what each entry means, how the default flag works, and the errors that come from getting it wrong.
What packageDirectories is
packageDirectories is the only required key in sfdx-project.json. It is an array of objects, and each object points to a folder in your project that holds metadata source. The CLI uses this list to decide which folders to scan when you deploy, retrieve, or track changes.
A minimal project has one entry:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The name "package directory" is historical. A package directory does not have to be an installable package. In a plain project it is just a source folder. It becomes a package only when you add package fields, which a later chapter covers.
The path field
path is required. It is relative to the project root, which is the folder that contains sfdx-project.json.
- Use forward slashes, even on Windows, for example
force-apporsrc/core. - Do not use an absolute path or a path that climbs out of the project with
... - The folder must exist. A path that points nowhere makes commands fail.
- The path is case-sensitive on some file systems, so match the folder name exactly.
Inside the folder, the CLI finds metadata by walking the directory tree. The generated project places metadata under force-app/main/default, but the CLI does not require main/default. It recognizes metadata by folder names such as classes, lwc, and objects, and by file suffixes such as .cls-meta.xml. You can nest folders any way you like, and the CLI still finds the components.
The default flag
Exactly one package directory should have "default": true. The default directory is where the CLI puts new source when it has to choose a location and you did not say where.
Two common cases:
- Retrieving a component that does not yet exist in your project. The CLI writes it into the default directory.
- Creating new components with commands such as
sf apex generate class, when you do not pass--output-dir.
Retrieving a component that already exists in the project is different. The CLI writes it back to the folder where it already lives, whichever directory that is.
With a single entry, default is optional in practice, because there is only one place to write. Set it anyway. It keeps the file correct if you add a second directory later.
Rules for default
- With one directory, the CLI treats it as the default even if
defaultis missing. - With two or more directories, exactly one needs
"default": true. - Two directories marked
trueis an error. - No directory marked
truein a multi-directory project is also an error.
Multiple package directories
You can list several directories. The order of the array matters, and so does which entry is the default.
{
"packageDirectories": [
{
"path": "core",
"default": true
},
{
"path": "feature",
"default": false
},
{
"path": "unpackaged"
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}When you run sf project deploy start with no other flags, the CLI deploys every directory in the list. If you want only one, name it:
sf project deploy start --source-dir feature--source-dir accepts any path under a listed package directory. It does not accept a folder outside all of them.
Why split source into several directories
- Separate concerns. Keep shared objects in
coreand a feature's Apex and components infeature. - Prepare for packaging. Each directory can later become its own unlocked package with its own version.
- Keep unpackaged items apart. Put sample data, scripts, or org-specific metadata in a directory you never package.
The array order also becomes the deploy order when directories depend on each other. A directory that others rely on should come first. A later chapter covers dependencies in detail.
Nested and overlapping paths
Do not let one package directory contain another. If force-app is listed and you also list force-app/extra, the same files belong to two directories. The CLI reports an error about overlapping or nested package directories, or it behaves unpredictably. Give each directory its own separate folder.
The same component must also not exist in two directories. If core/objects/Account and feature/objects/Account both define the same custom field, a deploy sees a duplicate and fails or overwrites one with the other.
Combining with .forceignore
.forceignore filters files after the CLI reads packageDirectories. A directory can be listed and still contribute nothing if the ignore patterns match all its contents. If a directory seems to be skipped, check the ignore file before you blame the JSON.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Invalid sfdx-project.json` or a JSON parse message | Trailing comma, missing quote, or comment in the file | Remove the comma or comment. JSON allows neither. |
| Error that no default package directory exists | Several directories and none has `"default": true` | Mark one directory as the default. |
| Error about multiple defaults | Two entries have `"default": true` | Leave `true` on exactly one. |
| Path not found or "does not exist" | `path` has a typo, wrong case, or the folder was never created | Fix the spelling or create the folder. |
| New component lands in an unexpected folder | The default flag is on a different directory than you thought | Move the `default` flag, or retrieve with `--output-dir`. |
| Deploy ignores a folder | Patterns in `.forceignore` match everything inside it | Narrow the patterns. |
| Duplicate component error | The same metadata exists in two directories | Keep the component in one directory only. |
Checking your configuration
You do not need an org to validate the file's structure. Run a command that reads the project and watch for errors.
sf project deploy start --dry-run --target-org my-scratch--dry-run validates the deploy without saving changes, but it still needs an org. For a check that needs no org, list the directories yourself:
cat sfdx-project.json
ls force-appIf the file parses and every path exists, any remaining failure is more likely in the metadata than in the project file.
Summary of decisions
- Start with one directory named
force-appand"default": true. - Add directories only when you have a reason: packaging, separation, or unpackaged content.
- Keep exactly one default, and keep every path relative, existing, and non-overlapping.
- Put a directory first in the array if other directories depend on it.
Exercise
A project lists two package directories, core and feature, and neither has "default": true. What goes wrong, and how do you fix it?
Show answer
With more than one directory, the CLI needs to know where to write new source, so it reports an error that no default package directory is set. Fix it by adding "default": true to exactly one entry, for example core, and leaving the other without it or set to false.
Exercise
You list force-app and force-app/extra as two package directories. Why is this a problem, and what should you do instead?
Show answer
The second path sits inside the first, so the same files belong to two directories. This causes overlap errors or duplicate components. Use separate sibling folders, such as force-app and extra-app, or keep everything in one directory.
Exercise
You run sf project retrieve start for a Apex class that already exists in feature/classes. The default directory is core. Where is the retrieved file written?
Show answer
It is written back to feature/classes, where the component already lives. The default directory only receives components that do not yet exist in the project.