Sub topic 14
Troubleshooting common project configuration errors
This chapter assumes you can read JSON, run sf commands, and already know the roles of sfdx-project.json, .forceignore, and the scratch org definition file from earlier chapters. It teaches a repeatable way to diagnose configuration problems, then walks through the errors you will meet most often.
A diagnostic routine
Most project errors come from one of four places: the JSON syntax, the packageDirectories entries, the files on disk, or the org you are talking to. Work through them in that order. Each step is cheap and rules out a whole class of causes.
- Confirm you are in the project. The CLI looks for
sfdx-project.jsonin the current directory, then in each parent directory. If it finds none, project commands fail. - Validate the JSON syntax.
- Check that every
pathexists and that exactly one directory is the default. - Check
.forceignorefor patterns that hide the files you expect. - Check the target org, its API version, and any package dependencies.
The project file is not found
If you run sf project deploy start from a folder outside the project, the CLI cannot find sfdx-project.json and reports that you are not inside a Salesforce DX project. The fix is to change into the project folder or any subfolder of it.
pwd
ls sfdx-project.json
cd ~/projects/my-project
sf project deploy startA common variant is a project nested one level too deep. This happens when you clone a repository whose root holds a project folder, so sfdx-project.json sits in a subfolder. Change into that subfolder before running commands.
Invalid JSON
The CLI parses sfdx-project.json as strict JSON. It does not accept comments or trailing commas, and keys and strings need double quotes. A single typo makes every project command fail with a parse error.
This file has two defects: a trailing comma after the last property and a single-quoted string.
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": 'my-project',
"sourceApiVersion": "61.0",
}The corrected version:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"name": "my-project",
"sourceApiVersion": "61.0"
}You can check syntax from the terminal without installing anything extra if Node.js is present, because the Salesforce CLI requires it for some installs, or use any JSON validator in your editor.
node -e "JSON.parse(require('fs').readFileSync('sfdx-project.json','utf8')); console.log('valid JSON')"Package directory problems
The packageDirectories array is the part of the file that most often goes wrong. The common failures are listed below.
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a package directory path does not exist | `path` has a typo, a wrong case, or points to a folder you never created | Fix the path or create the folder |
| Error about no default package directory | No entry has `"default": true` in a multi-directory project | Mark exactly one entry as default |
| Error about more than one default | Two entries have `"default": true` | Keep one |
| Deploy succeeds but nothing is deployed | The files live outside every listed `path` | Move them under a listed path or add a directory |
| Wrong folder receives new components | The default flag is on a different directory than you intended | Move the `default` flag |
Paths are relative to the project root and should use forward slashes, which work on every operating system. Do not use an absolute path.
A project with a single package directory does not strictly need the default flag, because the CLI treats the only entry as the default. Once you add a second directory, set the flag explicitly. The default directory is where commands such as sf apex generate class place new files when you do not pass --output-dir.
{
"packageDirectories": [
{ "path": "force-app", "default": true },
{ "path": "unpackaged", "default": false }
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Files that never deploy or retrieve
If a file exists on disk but a deploy ignores it, suspect .forceignore before anything else. The file uses gitignore-style patterns, and a broad pattern can match more than you meant.
# Intended: skip test fixtures
**/__tests__/**
# Mistake: this hides every profile, including the ones you want to deploy
**/profiles/**Test your assumptions by listing what the CLI plans to do. A preview deploy shows which files are included without changing the org.
sf project deploy previewIf a component is missing from the preview but present in the folder, check the patterns. If the component appears in the preview but is absent in the org, move on to org-side causes.
Two gotchas are worth remembering:
- A pattern in
.forceignoreapplies to deploys, retrieves, and source tracking together. Ignoring a file to avoid a deploy error also stops you from retrieving it later. - The file is read relative to the project root. A
.forceignorecopied into a subfolder has no effect.
Source API version mismatches
sourceApiVersion sets the API version used for source operations. Two problems follow from it.
First, the value may be higher than the org supports. A scratch org or sandbox on an older release rejects metadata types or fields that belong to a newer API. The error usually names the unsupported element or version. Lower sourceApiVersion or use an org on a release that supports it.
Second, the value may be so old that newer metadata is silently dropped on retrieve, because the retrieve uses that API version to decide which fields exist. If a field you added in the org does not appear in your retrieved files, compare sourceApiVersion with the version in which the field was introduced.
You can list the API versions the org supports and the version the CLI would use:
sf org display --target-org my-scratch
sf config get org-api-versionLogin URL and org authorization errors
sfdcLoginUrl is the default login URL used when you authorize an org with sf org login web. If you authorize a sandbox while the URL still points at https://login.salesforce.com, the login fails or lands on the wrong org. For a sandbox, pass the URL on the command, or set sfdcLoginUrl to https://test.salesforce.com in the project.
sf org login web --instance-url https://test.salesforce.com --alias my-sandboxFor a My Domain org, use the full domain URL. An alias that points to an expired authorization produces a different error, which tells you the token is no longer valid. Authorize again to refresh it.
Namespace conflicts
The namespace property should be empty unless you are developing a managed package or a namespaced unlocked package, and you own that namespace. Setting a namespace on a project that deploys to an org without a matching namespace causes failures when the CLI tries to apply the prefix. If you copied a project from a namespaced package and do not need the prefix, set "namespace": "".
Scratch org creation failures
Scratch org creation reads config/project-scratch-def.json. Failures there are separate from sfdx-project.json errors, but they look similar from the terminal.
- Invalid JSON in the definition file. The same strict rules apply.
- Unknown feature or setting. A misspelled key under
featuresorsettingsis rejected. Settings names are case sensitive and follow the Metadata API settings types. - Wrong edition. The
editionmust be one the Dev Hub allows, such asDeveloperorEnterprise. - No Dev Hub. If no Dev Hub is authorized,
sf org create scratchfails before it reads your definition. Runsf org listand confirm a Dev Hub appears.
sf org list
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-defaultPackage and dependency errors
Multi-package projects add three more places to check.
- Alias not found. A
dependenciesentry or apackagevalue must match a key inpackageAliases. If you rename a package in one place, rename it in the other. - Placeholder IDs. The sample IDs in tutorials are placeholders. Create the package with
sf package create, which writes the real0HoID intopackageAliases. Do not hand-edit the ID unless you copy it from the org. - Wrong order. List the depended-on directory before the directory that depends on it. In the earlier example,
coreprecedesfeature, becausefeature-pkgdepends oncore-pkg. A reversed order produces errors about missing components when source is deployed.
{
"path": "feature",
"package": "feature-pkg",
"versionNumber": "0.1.0.NEXT",
"default": false,
"dependencies": [
{ "package": "core-pkg", "versionNumber": "1.0.0.LATEST" }
]
}A dependency on a version that does not exist yet also fails. LATEST resolves to the newest created version of that package, so create at least one version of core-pkg first.
Deploy errors that look like configuration errors
Some deploy failures come from the metadata, not from the project settings. Read the error text before you change sfdx-project.json.
- A message naming a component and a line number points to the metadata file, such as an Apex compile error.
- A message about a missing dependency, such as a field referenced by a layout, means the referenced component is not in the deploy and not in the org. Include it or deploy it first.
- A message about an unsupported type or field points to an API version problem, covered above.
If the failure is unclear, narrow the scope. Deploy one directory or one component to isolate the cause.
sf project deploy start --source-dir force-app/main/default/classes
sf project deploy start --metadata ApexClass:MyClassSummary checklist
- Run commands from inside the project.
- Keep
sfdx-project.jsonvalid, strict JSON. - Make sure each
pathexists and one directory is the default. - Review
.forceignorewhen files go missing. - Match
sourceApiVersionto the org. - Keep
packageAliases,package, anddependenciesnames consistent, and list dependencies first.
Exercise
A teammate's sf project deploy start fails with a parse error. The file contains "sourceApiVersion": "61.0", as the last line before the closing brace. What is wrong and how do you confirm the fix?
Show answer
The trailing comma after the last property is invalid JSON. Remove the comma, then check the file with a JSON validator or node -e "JSON.parse(require('fs').readFileSync('sfdx-project.json','utf8'))", and rerun the deploy.
Exercise
You added force-app/main/default/classes/Helper.cls, but sf project deploy preview does not list it. Name two causes and how you would check each.
Show answer
First, a .forceignore pattern may match the file, so read the patterns and test by narrowing them. Second, the file may sit outside every path in packageDirectories, so compare the file location with the listed paths. Verify that force-app is listed and that the folder is spelled correctly.
Exercise
In a two-package project, deploying feature fails with missing component errors for objects defined in core. What do you check in sfdx-project.json?
Show answer
Check that the core directory is listed before feature in packageDirectories. Also check that feature-pkg declares core-pkg under dependencies, and that both names exist in packageAliases.