Sub topic 13
Common setup problems and useful settings
This chapter assumes you finished the earlier setup steps. VS Code, the Salesforce CLI, a Java JDK, and the Salesforce Extension Pack are installed. You have a Salesforce DX project and have authorized an org at least once. You should be comfortable opening a terminal and editing settings.json.
Most setup failures come from a small set of causes. The extensions cannot find a tool, the project is missing or misread, or the org login has gone stale. The goal here is to learn which layer is failing and what to check first.
A layered way to diagnose problems
The tooling has four layers, and a failure in a lower layer shows up as a confusing error in a higher one:
- VS Code runs the extensions.
- The Salesforce CLI (
sf) does the org work: authorization, deploy, retrieve, and test runs. - Java runs the Apex language server, which provides completion, navigation, and diagnostics.
- The org is the remote target for all of the above.
Start at the bottom of the failing feature's dependency chain and work up. If code completion is dead, check Java before you reinstall the extensions. If deploy fails, check the org and CLI before you suspect VS Code.
First checks that solve most problems
Run these in the terminal built into VS Code. This matters because the integrated terminal shows you the environment the extensions inherit.
code --version
sf --version
java -version
sf config get target-org
sf org listRead the results as follows:
- If
sforjavais "command not found", the tool is not installed or not on yourPATH. - If
sf config get target-orgprints nothing, no default org is set for this project. - If
sf org listshows your org marked as expired, or shows no orgs, you need to authorize again.
A tool can work in a terminal you opened earlier and still fail in VS Code. VS Code reads environment variables when it starts. After you install Java or change PATH or JAVA_HOME, quit VS Code completely and reopen it. A reload is not always enough, and on Windows you may need to sign out and back in.
Problem: the Apex language server will not start
Symptoms include no code completion, no outline for Apex files, no "Go to Definition", or a notification that Java cannot be found or is the wrong version.
The Apex language server is a Java program, so the extension needs a JDK. It looks for one in a fixed order. It uses the salesforcedx-vscode-apex.java.home setting if present, and otherwise falls back to the JDK_HOME and JAVA_HOME environment variables and then the PATH. A JRE alone is not enough, and a JDK version that is too old or too new for your extension release will fail to launch the server. The supported versions change between releases, so check the extension's current documentation for the range it accepts.
Work through these steps:
- Run
java -versionin the terminal and confirm it prints a supported version. - If several JDKs are installed, the one first on your
PATHwins, which may not be the one you want. Point the extension at a specific JDK instead. - Add the setting below, using the real path to the JDK's home directory. This is the folder that contains
bin, not thebinfolder itself.
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}- Run Developer: Reload Window from the Command Palette.
- Open View > Output and pick the Apex language server channel from the dropdown. Startup errors are printed there, and they usually name the exact problem.
Large orgs and large projects can make the language server slow on first launch. It indexes your Apex classes and builds a cache, and the first run after opening a project takes longest. Give it a minute before concluding that it is hung.
Problem: SFDX commands are missing
If typing SFDX: in the Command Palette shows only a few commands, or none, the extensions did not detect a Salesforce DX project. The extensions activate when the folder you opened has a sfdx-project.json file at its root.
The usual mistake is opening the wrong folder. If you open a parent directory, such as the folder that contains my-apex-project, the file is one level too deep and the extensions stay mostly inactive. Use File > Open Folder and select the folder that directly contains sfdx-project.json.
Also confirm that:
- The Salesforce Extension Pack is enabled for this workspace. Open the Extensions view and look for a "Disabled" label.
- The extensions are not stuck in an error state. Developer: Show Running Extensions lists what activated and flags failures.
sfdx-project.jsonis valid JSON. A stray comma stops the tooling from reading the project, and VS Code underlines the error in the file.
Problem: the org is unauthorized or expired
Deploys and test runs fail with messages about an invalid session, an expired access token, or "No default org found". Check each of these:
| Symptom | Likely cause | Fix |
|---|---|---|
| "No default org" or "No target-org found" | No default org is set for this project | `sf config set target-org my-org`, or run **SFDX: Set a Default Org** |
| "Expired access/refresh token" | The org session was revoked, or a scratch org or sandbox expired | Log in again with `sf org login web --alias my-org --set-default` |
| Login succeeds but deploys go to the wrong place | A different default is set at the project level than at the global level | Run `sf config get target-org` inside the project and compare it with the global value |
| Browser login hangs | A browser extension or a firewall is blocking the local callback | Retry in a clean browser profile, or check your network policy |
| Wrong login URL | The org is a sandbox or a My Domain org, but the project uses the production login URL | Set `sfdcLoginUrl` in `sfdx-project.json`, or pass `--instance-url` when you log in |
For a sandbox, the login command looks like this:
sf org login web --alias my-sandbox --instance-url https://test.salesforce.com --set-default
sf org display --target-org my-sandboxsf org display prints the username, org ID, and instance URL that the CLI has stored. If the values look wrong, you have found the problem. The status bar at the bottom of VS Code also shows the default org alias, so glance there before you deploy.
Default orgs can be set at two levels. A project-level value is stored inside the project and applies only there. A global value applies everywhere. The project-level value wins. If you work in many projects, set the default per project so that a deploy does not land in the org you used yesterday.
Problem: the CLI and the extensions disagree
Older material refers to sfdx, the earlier command-line tool, and its colon-style commands such as sfdx force:source:deploy. The current tool is sf. If a tutorial or a script uses the older syntax, translate it rather than mixing the two. Use sf --version to confirm which version you have. If the extensions report that the CLI is missing even though you installed it, the usual cause is a stale PATH, as described earlier.
Useful settings
Settings can live in your user settings (applied everywhere) or in .vscode/settings.json inside the project (applied only there). Project-level settings are better for anything that depends on the project, such as a specific JDK. Setting names change between extension releases, so open the Settings editor and search for salesforcedx to see what your version supports.
These are worth knowing:
| Setting | What it does | When to change it |
|---|---|---|
| `salesforcedx-vscode-apex.java.home` | Selects the JDK for the language server | Java is not found, or the wrong JDK is picked |
| `salesforcedx-vscode-core.push-or-deploy-on-save.enabled` | Deploys a file to the org each time you save it | You want fast feedback in a scratch org. Avoid it for shared sandboxes |
| `salesforcedx-vscode-apex.enable-semantic-errors` | Turns on extra compile-style checks in the editor | You want earlier error detection, accepting that this may be less stable |
A project-level .vscode/settings.json that fixes the JDK and enables deploy on save for a scratch-org project could look like this:
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk",
"salesforcedx-vscode-core.push-or-deploy-on-save.enabled": true
}If you commit .vscode/settings.json, remember that a JDK path is specific to one machine. Teammates on other systems will get a broken path. Either keep that setting in your own user settings or leave it out of the repository.
Collecting evidence before asking for help
When a problem survives the checks above, gather this information. It makes any bug report or support question answerable:
- The output of
code --version,sf --version, andjava -version. - The text of the Output panel for the Apex language server channel and for the Salesforce CLI channel.
- Whether the problem happens in a fresh project made with
sf project generate. If a fresh project works, the cause is in your project's files or settings. If it fails too, the cause is in your installation. - Whether it persists with other extensions disabled. Conflicts with other language extensions are rare but possible.
As a last resort, uninstall and reinstall the Extension Pack, then fully quit and reopen VS Code. Do this only after the cheaper checks, because it rarely fixes a Java or org problem.
Exercise
After installing a new JDK, you open VS Code and still get no Apex code completion, and java -version in a terminal outside VS Code prints the new version. List the steps you would take, in order, and say where you would read the actual error.
Show answer
First, fully quit and reopen VS Code, because it reads environment variables only at startup. If completion is still missing, set salesforcedx-vscode-apex.java.home to the JDK root folder (the one containing bin, not bin itself), then run Developer: Reload Window. Confirm that the opened folder contains sfdx-project.json at its root. The actual error appears in View > Output, in the Apex language server channel.
Exercise
A deploy fails with an expired token message, and sf config get target-org shows the alias my-org. What command re-authorizes it so that it remains the default, and why might a project-level default differ from a global one?
Show answer
Run sf org login web --alias my-org --set-default. A project-level default is stored inside the project and takes precedence over the global default, so the two can point to different orgs. Run sf config get target-org from inside the project to see the value that applies there.