environment.md

 1---
 2title: Environment Variables - Zed
 3description: How Zed detects and uses environment variables. Shell integration, dotenv support, and troubleshooting.
 4---
 5
 6# Environment Variables
 7
 8_**Note**: The following only applies to Zed 0.152.0 and later._
 9
10Multiple features in Zed are affected by environment variables:
11
12- [Tasks](./tasks.md)
13- [Built-in terminal](./terminal.md)
14- Look-up of language servers
15- Language servers
16
17To make the best use of these features, it helps to understand where Zed gets environment variables and how it uses them.
18
19## Where does Zed get its environment variables from?
20
21How Zed starts affects which environment variables it can use. That includes launching from the macOS Dock, a Linux window manager, or the `zed` CLI.
22
23### Launched from the CLI
24
25If Zed is opened via the CLI (`zed`), it will inherit the environment variables from the surrounding shell session.
26
27That means if you do
28
29```
30$ export MY_ENV_VAR=hello
31$ zed .
32```
33
34the environment variable `MY_ENV_VAR` is now available inside Zed. For example, in the built-in terminal.
35
36Starting with Zed 0.152.0, the CLI `zed` will _always_ pass along its environment to Zed, regardless of whether a Zed instance was previously running or not. Prior to Zed 0.152.0 this was not the case and only the first Zed instance would inherit the environment variables.
37
38### Launched via window manager, Dock, or launcher
39
40When Zed has been launched via the macOS Dock, or a GNOME or KDE icon on Linux, or an application launcher like Alfred or Raycast, it has no surrounding shell environment from which to inherit its environment variables.
41
42To still have a useful environment, Zed spawns a login shell in the user's home directory and reads its environment. This environment is then set on the Zed _process_, so all Zed windows and projects inherit it.
43
44Since that can lead to problems for users who need different environment variables per project (for example with `direnv`, `asdf`, or `mise`), Zed spawns another login shell when opening a project. This second shell runs in the project's directory. The environment from that shell is _not_ set on the process, because opening a new project would otherwise change the environment for all Zed windows. Instead, that environment is stored and passed along when running tasks, opening terminals, or spawning language servers.
45
46## Where and how are environment variables used?
47
48There are two sets of environment variables:
49
501. Environment variables of the Zed process
512. Environment variables stored per project
52
53The variables from (1) are always used, since they are stored on the process itself and every spawned process (tasks, terminals, language servers, ...) will inherit them by default.
54
55The variables from (2) are used explicitly, depending on the feature.
56
57### Tasks
58
59Tasks are spawned with a combined environment. In order of precedence (low to high, with the last overwriting the first):
60
61- the Zed process environment
62- if the project was opened from the CLI: the CLI environment
63- if the project was not opened from the CLI: the project environment variables obtained by running a login shell in the project's root folder
64- optional, explicitly configured environment in settings
65
66### Built-in terminal
67
68Built-in terminals, like tasks, are spawned with a combined environment. In order of precedence (low to high):
69
70- the Zed process environment
71- if the project was opened from the CLI: the CLI environment
72- if the project was not opened from the CLI: the project environment variables obtained by running a login shell in the project's root folder
73- optional, explicitly configured environment in settings
74
75### Look-up of language servers
76
77For some languages the language server adapters lookup the binary in the user's `$PATH`. Examples:
78
79- Go
80- Zig
81- Rust (if [configured to do so](./languages/rust.md#binary))
82- C
83- TypeScript
84
85For this look-up, Zed uses the following environment:
86
87- if the project was opened from the CLI: the CLI environment
88- if the project was not opened from the CLI: the project environment variables obtained by running a login shell in the project's root folder
89
90### Language servers
91
92After looking up a language server, Zed starts them.
93
94These language server processes always inherit Zed's process environment. But, depending on the language server look-up, additional environment variables might be set or overwrite the process environment.
95
96- If the language server was found in the project environment's `$PATH`, then that project environment is passed along to the language server process. Where the project environment comes from depends on how the project was opened (via CLI or not). See the previous section on language server look-up.
97- If the language server was not found in the project environment, Zed tries to install and start it globally. In that case, the process inherits Zed's process environment and, if the project was opened via CLI, the CLI environment.