1//! Paths to locations used by Zed.
2
3use std::path::{Path, PathBuf};
4use std::sync::OnceLock;
5
6pub use util::paths::home_dir;
7
8/// Returns the relative path to the zed_server directory on the ssh host.
9pub fn remote_server_dir_relative() -> &'static Path {
10 Path::new(".zed_server")
11}
12
13/// Returns the path to the configuration directory used by Zed.
14pub fn config_dir() -> &'static PathBuf {
15 static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
16 CONFIG_DIR.get_or_init(|| {
17 if cfg!(target_os = "windows") {
18 return dirs::config_dir()
19 .expect("failed to determine RoamingAppData directory")
20 .join("Zed");
21 }
22
23 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
24 return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
25 flatpak_xdg_config.into()
26 } else {
27 dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
28 }
29 .join("zed");
30 }
31
32 home_dir().join(".config").join("zed")
33 })
34}
35
36/// Returns the path to the support directory used by Zed.
37pub fn support_dir() -> &'static PathBuf {
38 static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
39 SUPPORT_DIR.get_or_init(|| {
40 if cfg!(target_os = "macos") {
41 return home_dir().join("Library/Application Support/Zed");
42 }
43
44 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
45 return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
46 flatpak_xdg_data.into()
47 } else {
48 dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
49 }
50 .join("zed");
51 }
52
53 if cfg!(target_os = "windows") {
54 return dirs::data_local_dir()
55 .expect("failed to determine LocalAppData directory")
56 .join("Zed");
57 }
58
59 config_dir().clone()
60 })
61}
62
63/// Returns the path to the temp directory used by Zed.
64pub fn temp_dir() -> &'static PathBuf {
65 static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
66 TEMP_DIR.get_or_init(|| {
67 if cfg!(target_os = "macos") {
68 return dirs::cache_dir()
69 .expect("failed to determine cachesDirectory directory")
70 .join("Zed");
71 }
72
73 if cfg!(target_os = "windows") {
74 return dirs::cache_dir()
75 .expect("failed to determine LocalAppData directory")
76 .join("Zed");
77 }
78
79 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
80 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
81 flatpak_xdg_cache.into()
82 } else {
83 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
84 }
85 .join("zed");
86 }
87
88 home_dir().join(".cache").join("zed")
89 })
90}
91
92/// Returns the path to the logs directory.
93pub fn logs_dir() -> &'static PathBuf {
94 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
95 LOGS_DIR.get_or_init(|| {
96 if cfg!(target_os = "macos") {
97 home_dir().join("Library/Logs/Zed")
98 } else {
99 support_dir().join("logs")
100 }
101 })
102}
103
104/// Returns the path to the Zed server directory on this SSH host.
105pub fn remote_server_state_dir() -> &'static PathBuf {
106 static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
107 REMOTE_SERVER_STATE.get_or_init(|| support_dir().join("server_state"))
108}
109
110/// Returns the path to the `Zed.log` file.
111pub fn log_file() -> &'static PathBuf {
112 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
113 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
114}
115
116/// Returns the path to the `Zed.log.old` file.
117pub fn old_log_file() -> &'static PathBuf {
118 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
119 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
120}
121
122/// Returns the path to the database directory.
123pub fn database_dir() -> &'static PathBuf {
124 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
125 DATABASE_DIR.get_or_init(|| support_dir().join("db"))
126}
127
128/// Returns the path to the crashes directory, if it exists for the current platform.
129pub fn crashes_dir() -> &'static Option<PathBuf> {
130 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
131 CRASHES_DIR.get_or_init(|| {
132 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
133 })
134}
135
136/// Returns the path to the retired crashes directory, if it exists for the current platform.
137pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
138 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
139 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
140}
141
142/// Returns the path to the `settings.json` file.
143pub fn settings_file() -> &'static PathBuf {
144 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
145 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
146}
147
148/// Returns the path to the `settings_backup.json` file.
149pub fn settings_backup_file() -> &'static PathBuf {
150 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
151 SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
152}
153
154/// Returns the path to the `keymap.json` file.
155pub fn keymap_file() -> &'static PathBuf {
156 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
157 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
158}
159
160/// Returns the path to the `keymap_backup.json` file.
161pub fn keymap_backup_file() -> &'static PathBuf {
162 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
163 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
164}
165
166/// Returns the path to the `tasks.json` file.
167pub fn tasks_file() -> &'static PathBuf {
168 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
169 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
170}
171
172/// Returns the path to the extensions directory.
173///
174/// This is where installed extensions are stored.
175pub fn extensions_dir() -> &'static PathBuf {
176 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
177 EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
178}
179
180/// Returns the path to the extensions directory.
181///
182/// This is where installed extensions are stored on a remote.
183pub fn remote_extensions_dir() -> &'static PathBuf {
184 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
185 EXTENSIONS_DIR.get_or_init(|| support_dir().join("remote_extensions"))
186}
187
188/// Returns the path to the extensions directory.
189///
190/// This is where installed extensions are stored on a remote.
191pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
192 static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
193 UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
194}
195
196/// Returns the path to the themes directory.
197///
198/// This is where themes that are not provided by extensions are stored.
199pub fn themes_dir() -> &'static PathBuf {
200 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
201 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
202}
203
204/// Returns the path to the snippets directory.
205pub fn snippets_dir() -> &'static PathBuf {
206 static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
207 SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
208}
209
210/// Returns the path to the contexts directory.
211///
212/// This is where the saved contexts from the Assistant are stored.
213pub fn contexts_dir() -> &'static PathBuf {
214 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
215 CONTEXTS_DIR.get_or_init(|| {
216 if cfg!(target_os = "macos") {
217 config_dir().join("conversations")
218 } else {
219 support_dir().join("conversations")
220 }
221 })
222}
223
224/// Returns the path to the contexts directory.
225///
226/// This is where the prompts for use with the Assistant are stored.
227pub fn prompts_dir() -> &'static PathBuf {
228 static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
229 PROMPTS_DIR.get_or_init(|| {
230 if cfg!(target_os = "macos") {
231 config_dir().join("prompts")
232 } else {
233 support_dir().join("prompts")
234 }
235 })
236}
237
238/// Returns the path to the prompt templates directory.
239///
240/// This is where the prompt templates for core features can be overridden with templates.
241///
242/// # Arguments
243///
244/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
245pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
246 if let Some(path) = repo_path {
247 let dev_path = path.join("assets").join("prompts");
248 if dev_path.exists() {
249 return dev_path;
250 }
251 }
252
253 static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
254 PROMPT_TEMPLATES_DIR
255 .get_or_init(|| {
256 if cfg!(target_os = "macos") {
257 config_dir().join("prompt_overrides")
258 } else {
259 support_dir().join("prompt_overrides")
260 }
261 })
262 .clone()
263}
264
265/// Returns the path to the semantic search's embeddings directory.
266///
267/// This is where the embeddings used to power semantic search are stored.
268pub fn embeddings_dir() -> &'static PathBuf {
269 static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
270 EMBEDDINGS_DIR.get_or_init(|| {
271 if cfg!(target_os = "macos") {
272 config_dir().join("embeddings")
273 } else {
274 support_dir().join("embeddings")
275 }
276 })
277}
278
279/// Returns the path to the languages directory.
280///
281/// This is where language servers are downloaded to for languages built-in to Zed.
282pub fn languages_dir() -> &'static PathBuf {
283 static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
284 LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
285}
286
287/// Returns the path to the Copilot directory.
288pub fn copilot_dir() -> &'static PathBuf {
289 static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
290 COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
291}
292
293/// Returns the path to the Supermaven directory.
294pub fn supermaven_dir() -> &'static PathBuf {
295 static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
296 SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
297}
298
299/// Returns the path to the default Prettier directory.
300pub fn default_prettier_dir() -> &'static PathBuf {
301 static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
302 DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
303}
304
305/// Returns the path to the remote server binaries directory.
306pub fn remote_servers_dir() -> &'static PathBuf {
307 static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
308 REMOTE_SERVERS_DIR.get_or_init(|| support_dir().join("remote_servers"))
309}
310
311/// Returns the relative path to a `.zed` folder within a project.
312pub fn local_settings_folder_relative_path() -> &'static Path {
313 Path::new(".zed")
314}
315
316/// Returns the relative path to a `settings.json` file within a project.
317pub fn local_settings_file_relative_path() -> &'static Path {
318 Path::new(".zed/settings.json")
319}
320
321/// Returns the relative path to a `tasks.json` file within a project.
322pub fn local_tasks_file_relative_path() -> &'static Path {
323 Path::new(".zed/tasks.json")
324}
325
326/// Returns the relative path to a `.vscode/tasks.json` file within a project.
327pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
328 Path::new(".vscode/tasks.json")
329}
330
331/// A default editorconfig file name to use when resolving project settings.
332pub const EDITORCONFIG_NAME: &str = ".editorconfig";