1//! Paths to locations used by Zed.
2
3use std::env;
4use std::path::{Path, PathBuf};
5use std::sync::{LazyLock, OnceLock};
6
7pub use util::paths::home_dir;
8use util::rel_path::RelPath;
9
10/// A default editorconfig file name to use when resolving project settings.
11pub const EDITORCONFIG_NAME: &str = ".editorconfig";
12
13/// A custom data directory override, set only by `set_custom_data_dir`.
14/// This is used to override the default data directory location.
15/// The directory will be created if it doesn't exist when set.
16static CUSTOM_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
17
18/// The resolved data directory, combining custom override or platform defaults.
19/// This is set once and cached for subsequent calls.
20/// On macOS, this is `~/Library/Application Support/Zed`.
21/// On Linux/FreeBSD, this is `$XDG_DATA_HOME/zed`.
22/// On Windows, this is `%LOCALAPPDATA%\Zed`.
23static CURRENT_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
24
25/// The resolved config directory, combining custom override or platform defaults.
26/// This is set once and cached for subsequent calls.
27/// On macOS, this is `~/.config/zed`.
28/// On Linux/FreeBSD, this is `$XDG_CONFIG_HOME/zed`.
29/// On Windows, this is `%APPDATA%\Zed`.
30static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
31
32/// Returns the relative path to the zed_server directory on the ssh host.
33pub fn remote_server_dir_relative() -> &'static RelPath {
34 static CACHED: LazyLock<&'static RelPath> =
35 LazyLock::new(|| RelPath::unix(".zed_server").unwrap());
36 *CACHED
37}
38
39// Remove this once 223 goes stable
40/// Returns the relative path to the zed_wsl_server directory on the wsl host.
41pub fn remote_wsl_server_dir_relative() -> &'static RelPath {
42 static CACHED: LazyLock<&'static RelPath> =
43 LazyLock::new(|| RelPath::unix(".zed_wsl_server").unwrap());
44 *CACHED
45}
46
47/// Sets a custom directory for all user data, overriding the default data directory.
48/// This function must be called before any other path operations that depend on the data directory.
49/// The directory's path will be canonicalized to an absolute path by a blocking FS operation.
50/// The directory will be created if it doesn't exist.
51///
52/// # Arguments
53///
54/// * `dir` - The path to use as the custom data directory. This will be used as the base
55/// directory for all user data, including databases, extensions, and logs.
56///
57/// # Returns
58///
59/// A reference to the static `PathBuf` containing the custom data directory path.
60///
61/// # Panics
62///
63/// Panics if:
64/// * Called after the data directory has been initialized (e.g., via `data_dir` or `config_dir`)
65/// * The directory's path cannot be canonicalized to an absolute path
66/// * The directory cannot be created
67pub fn set_custom_data_dir(dir: &str) -> &'static PathBuf {
68 if CURRENT_DATA_DIR.get().is_some() || CONFIG_DIR.get().is_some() {
69 panic!("set_custom_data_dir called after data_dir or config_dir was initialized");
70 }
71 CUSTOM_DATA_DIR.get_or_init(|| {
72 let mut path = PathBuf::from(dir);
73 if path.is_relative() && path.exists() {
74 let abs_path = path
75 .canonicalize()
76 .expect("failed to canonicalize custom data directory's path to an absolute path");
77 path = util::paths::SanitizedPath::new(&abs_path).into()
78 }
79 std::fs::create_dir_all(&path).expect("failed to create custom data directory");
80 path
81 })
82}
83
84/// Returns the path to the configuration directory used by Zed.
85pub fn config_dir() -> &'static PathBuf {
86 CONFIG_DIR.get_or_init(|| {
87 if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
88 custom_dir.join("config")
89 } else if cfg!(target_os = "windows") {
90 dirs::config_dir()
91 .expect("failed to determine RoamingAppData directory")
92 .join("Zed")
93 } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
94 if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
95 flatpak_xdg_config.into()
96 } else {
97 dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
98 }
99 .join("zed")
100 } else {
101 home_dir().join(".config").join("zed")
102 }
103 })
104}
105
106/// Returns the path to the data directory used by Zed.
107pub fn data_dir() -> &'static PathBuf {
108 CURRENT_DATA_DIR.get_or_init(|| {
109 if let Some(custom_dir) = CUSTOM_DATA_DIR.get() {
110 custom_dir.clone()
111 } else if cfg!(target_os = "macos") {
112 home_dir().join("Library/Application Support/Zed")
113 } else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
114 if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
115 flatpak_xdg_data.into()
116 } else {
117 dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
118 }
119 .join("zed")
120 } else if cfg!(target_os = "windows") {
121 dirs::data_local_dir()
122 .expect("failed to determine LocalAppData directory")
123 .join("Zed")
124 } else {
125 config_dir().clone() // Fallback
126 }
127 })
128}
129
130/// Returns the path to the temp directory used by Zed.
131pub fn temp_dir() -> &'static PathBuf {
132 static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
133 TEMP_DIR.get_or_init(|| {
134 if cfg!(target_os = "macos") {
135 return dirs::cache_dir()
136 .expect("failed to determine cachesDirectory directory")
137 .join("Zed");
138 }
139
140 if cfg!(target_os = "windows") {
141 return dirs::cache_dir()
142 .expect("failed to determine LocalAppData directory")
143 .join("Zed");
144 }
145
146 if cfg!(any(target_os = "linux", target_os = "freebsd")) {
147 return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
148 flatpak_xdg_cache.into()
149 } else {
150 dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
151 }
152 .join("zed");
153 }
154
155 home_dir().join(".cache").join("zed")
156 })
157}
158
159/// Returns the path to the hang traces directory.
160pub fn hang_traces_dir() -> &'static PathBuf {
161 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
162 LOGS_DIR.get_or_init(|| data_dir().join("hang_traces"))
163}
164
165/// Returns the path to the logs directory.
166pub fn logs_dir() -> &'static PathBuf {
167 static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
168 LOGS_DIR.get_or_init(|| {
169 if cfg!(target_os = "macos") {
170 home_dir().join("Library/Logs/Zed")
171 } else {
172 data_dir().join("logs")
173 }
174 })
175}
176
177/// Returns the path to the Zed server directory on this SSH host.
178pub fn remote_server_state_dir() -> &'static PathBuf {
179 static REMOTE_SERVER_STATE: OnceLock<PathBuf> = OnceLock::new();
180 REMOTE_SERVER_STATE.get_or_init(|| data_dir().join("server_state"))
181}
182
183/// Returns the path to the `Zed.log` file.
184pub fn log_file() -> &'static PathBuf {
185 static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
186 LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
187}
188
189/// Returns the path to the `Zed.log.old` file.
190pub fn old_log_file() -> &'static PathBuf {
191 static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
192 OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
193}
194
195/// Returns the path to the database directory.
196pub fn database_dir() -> &'static PathBuf {
197 static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
198 DATABASE_DIR.get_or_init(|| data_dir().join("db"))
199}
200
201/// Returns the path to the crashes directory, if it exists for the current platform.
202pub fn crashes_dir() -> &'static Option<PathBuf> {
203 static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
204 CRASHES_DIR.get_or_init(|| {
205 cfg!(target_os = "macos").then_some(home_dir().join("Library/Logs/DiagnosticReports"))
206 })
207}
208
209/// Returns the path to the retired crashes directory, if it exists for the current platform.
210pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
211 static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
212 CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
213}
214
215/// Returns the path to the `settings.json` file.
216pub fn settings_file() -> &'static PathBuf {
217 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
218 SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
219}
220
221/// Returns the path to the global settings file.
222pub fn global_settings_file() -> &'static PathBuf {
223 static GLOBAL_SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
224 GLOBAL_SETTINGS_FILE.get_or_init(|| config_dir().join("global_settings.json"))
225}
226
227/// Returns the path to the `settings_backup.json` file.
228pub fn settings_backup_file() -> &'static PathBuf {
229 static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
230 SETTINGS_FILE.get_or_init(|| config_dir().join("settings_backup.json"))
231}
232
233/// Returns the path to the `keymap.json` file.
234pub fn keymap_file() -> &'static PathBuf {
235 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
236 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
237}
238
239/// Returns the path to the `keymap_backup.json` file.
240pub fn keymap_backup_file() -> &'static PathBuf {
241 static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
242 KEYMAP_FILE.get_or_init(|| config_dir().join("keymap_backup.json"))
243}
244
245/// Returns the path to the `tasks.json` file.
246pub fn tasks_file() -> &'static PathBuf {
247 static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
248 TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
249}
250
251/// Returns the path to the `debug.json` file.
252pub fn debug_scenarios_file() -> &'static PathBuf {
253 static DEBUG_SCENARIOS_FILE: OnceLock<PathBuf> = OnceLock::new();
254 DEBUG_SCENARIOS_FILE.get_or_init(|| config_dir().join("debug.json"))
255}
256
257/// Returns the path to the extensions directory.
258///
259/// This is where installed extensions are stored.
260pub fn extensions_dir() -> &'static PathBuf {
261 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
262 EXTENSIONS_DIR.get_or_init(|| data_dir().join("extensions"))
263}
264
265/// Returns the path to the extensions directory.
266///
267/// This is where installed extensions are stored on a remote.
268pub fn remote_extensions_dir() -> &'static PathBuf {
269 static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
270 EXTENSIONS_DIR.get_or_init(|| data_dir().join("remote_extensions"))
271}
272
273/// Returns the path to the extensions directory.
274///
275/// This is where installed extensions are stored on a remote.
276pub fn remote_extensions_uploads_dir() -> &'static PathBuf {
277 static UPLOAD_DIR: OnceLock<PathBuf> = OnceLock::new();
278 UPLOAD_DIR.get_or_init(|| remote_extensions_dir().join("uploads"))
279}
280
281/// Returns the path to the themes directory.
282///
283/// This is where themes that are not provided by extensions are stored.
284pub fn themes_dir() -> &'static PathBuf {
285 static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
286 THEMES_DIR.get_or_init(|| config_dir().join("themes"))
287}
288
289/// Returns the path to the snippets directory.
290pub fn snippets_dir() -> &'static PathBuf {
291 static SNIPPETS_DIR: OnceLock<PathBuf> = OnceLock::new();
292 SNIPPETS_DIR.get_or_init(|| config_dir().join("snippets"))
293}
294
295/// Returns the path to the contexts directory.
296///
297/// This is where the saved contexts from the Assistant are stored.
298pub fn text_threads_dir() -> &'static PathBuf {
299 static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
300 CONTEXTS_DIR.get_or_init(|| {
301 if cfg!(target_os = "macos") {
302 config_dir().join("conversations")
303 } else {
304 data_dir().join("conversations")
305 }
306 })
307}
308
309/// Returns the path to the contexts directory.
310///
311/// This is where the prompts for use with the Assistant are stored.
312pub fn prompts_dir() -> &'static PathBuf {
313 static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
314 PROMPTS_DIR.get_or_init(|| {
315 if cfg!(target_os = "macos") {
316 config_dir().join("prompts")
317 } else {
318 data_dir().join("prompts")
319 }
320 })
321}
322
323/// Returns the path to the prompt templates directory.
324///
325/// This is where the prompt templates for core features can be overridden with templates.
326///
327/// # Arguments
328///
329/// * `dev_mode` - If true, assumes the current working directory is the Zed repository.
330pub fn prompt_overrides_dir(repo_path: Option<&Path>) -> PathBuf {
331 if let Some(path) = repo_path {
332 let dev_path = path.join("assets").join("prompts");
333 if dev_path.exists() {
334 return dev_path;
335 }
336 }
337
338 static PROMPT_TEMPLATES_DIR: OnceLock<PathBuf> = OnceLock::new();
339 PROMPT_TEMPLATES_DIR
340 .get_or_init(|| {
341 if cfg!(target_os = "macos") {
342 config_dir().join("prompt_overrides")
343 } else {
344 data_dir().join("prompt_overrides")
345 }
346 })
347 .clone()
348}
349
350/// Returns the path to the semantic search's embeddings directory.
351///
352/// This is where the embeddings used to power semantic search are stored.
353pub fn embeddings_dir() -> &'static PathBuf {
354 static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
355 EMBEDDINGS_DIR.get_or_init(|| {
356 if cfg!(target_os = "macos") {
357 config_dir().join("embeddings")
358 } else {
359 data_dir().join("embeddings")
360 }
361 })
362}
363
364/// Returns the path to the languages directory.
365///
366/// This is where language servers are downloaded to for languages built-in to Zed.
367pub fn languages_dir() -> &'static PathBuf {
368 static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
369 LANGUAGES_DIR.get_or_init(|| data_dir().join("languages"))
370}
371
372/// Returns the path to the debug adapters directory
373///
374/// This is where debug adapters are downloaded to for DAPs that are built-in to Zed.
375pub fn debug_adapters_dir() -> &'static PathBuf {
376 static DEBUG_ADAPTERS_DIR: OnceLock<PathBuf> = OnceLock::new();
377 DEBUG_ADAPTERS_DIR.get_or_init(|| data_dir().join("debug_adapters"))
378}
379
380/// Returns the path to the external agents directory
381///
382/// This is where agent servers are downloaded to
383pub fn external_agents_dir() -> &'static PathBuf {
384 static EXTERNAL_AGENTS_DIR: OnceLock<PathBuf> = OnceLock::new();
385 EXTERNAL_AGENTS_DIR.get_or_init(|| data_dir().join("external_agents"))
386}
387
388/// Returns the path to the Copilot directory.
389pub fn copilot_dir() -> &'static PathBuf {
390 static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
391 COPILOT_DIR.get_or_init(|| data_dir().join("copilot"))
392}
393
394/// Returns the path to the Supermaven directory.
395pub fn supermaven_dir() -> &'static PathBuf {
396 static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
397 SUPERMAVEN_DIR.get_or_init(|| data_dir().join("supermaven"))
398}
399
400/// Returns the path to the default Prettier directory.
401pub fn default_prettier_dir() -> &'static PathBuf {
402 static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
403 DEFAULT_PRETTIER_DIR.get_or_init(|| data_dir().join("prettier"))
404}
405
406/// Returns the path to the remote server binaries directory.
407pub fn remote_servers_dir() -> &'static PathBuf {
408 static REMOTE_SERVERS_DIR: OnceLock<PathBuf> = OnceLock::new();
409 REMOTE_SERVERS_DIR.get_or_init(|| data_dir().join("remote_servers"))
410}
411
412/// Returns the path to the directory where the devcontainer CLI is installed.
413pub fn devcontainer_dir() -> &'static PathBuf {
414 static DEVCONTAINER_DIR: OnceLock<PathBuf> = OnceLock::new();
415 DEVCONTAINER_DIR.get_or_init(|| data_dir().join("devcontainer"))
416}
417
418/// Returns the relative path to a `.zed` folder within a project.
419pub fn local_settings_folder_name() -> &'static str {
420 ".zed"
421}
422
423/// Returns the relative path to a `.vscode` folder within a project.
424pub fn local_vscode_folder_name() -> &'static str {
425 ".vscode"
426}
427
428/// Returns the relative path to a `settings.json` file within a project.
429pub fn local_settings_file_relative_path() -> &'static RelPath {
430 static CACHED: LazyLock<&'static RelPath> =
431 LazyLock::new(|| RelPath::unix(".zed/settings.json").unwrap());
432 *CACHED
433}
434
435/// Returns the relative path to a `tasks.json` file within a project.
436pub fn local_tasks_file_relative_path() -> &'static RelPath {
437 static CACHED: LazyLock<&'static RelPath> =
438 LazyLock::new(|| RelPath::unix(".zed/tasks.json").unwrap());
439 *CACHED
440}
441
442/// Returns the relative path to a `.vscode/tasks.json` file within a project.
443pub fn local_vscode_tasks_file_relative_path() -> &'static RelPath {
444 static CACHED: LazyLock<&'static RelPath> =
445 LazyLock::new(|| RelPath::unix(".vscode/tasks.json").unwrap());
446 *CACHED
447}
448
449pub fn debug_task_file_name() -> &'static str {
450 "debug.json"
451}
452
453pub fn task_file_name() -> &'static str {
454 "tasks.json"
455}
456
457/// Returns the relative path to a `debug.json` file within a project.
458/// .zed/debug.json
459pub fn local_debug_file_relative_path() -> &'static RelPath {
460 static CACHED: LazyLock<&'static RelPath> =
461 LazyLock::new(|| RelPath::unix(".zed/debug.json").unwrap());
462 *CACHED
463}
464
465/// Returns the relative path to a `.vscode/launch.json` file within a project.
466pub fn local_vscode_launch_file_relative_path() -> &'static RelPath {
467 static CACHED: LazyLock<&'static RelPath> =
468 LazyLock::new(|| RelPath::unix(".vscode/launch.json").unwrap());
469 *CACHED
470}
471
472pub fn user_ssh_config_file() -> PathBuf {
473 home_dir().join(".ssh/config")
474}
475
476pub fn global_ssh_config_file() -> Option<&'static Path> {
477 if cfg!(windows) {
478 None
479 } else {
480 Some(Path::new("/etc/ssh/ssh_config"))
481 }
482}
483
484/// Returns candidate paths for the vscode user settings file
485pub fn vscode_settings_file_paths() -> Vec<PathBuf> {
486 let mut paths = vscode_user_data_paths();
487 for path in paths.iter_mut() {
488 path.push("User/settings.json");
489 }
490 paths
491}
492
493/// Returns candidate paths for the cursor user settings file
494pub fn cursor_settings_file_paths() -> Vec<PathBuf> {
495 let mut paths = cursor_user_data_paths();
496 for path in paths.iter_mut() {
497 path.push("User/settings.json");
498 }
499 paths
500}
501
502fn vscode_user_data_paths() -> Vec<PathBuf> {
503 // https://github.com/microsoft/vscode/blob/23e7148cdb6d8a27f0109ff77e5b1e019f8da051/src/vs/platform/environment/node/userDataPath.ts#L45
504 const VSCODE_PRODUCT_NAMES: &[&str] = &[
505 "Code",
506 "Code - Insiders",
507 "Code - OSS",
508 "VSCodium",
509 "VSCodium - Insiders",
510 "Code Dev",
511 "Code - OSS Dev",
512 "code-oss-dev",
513 ];
514 let mut paths = Vec::new();
515 if let Ok(portable_path) = env::var("VSCODE_PORTABLE") {
516 paths.push(Path::new(&portable_path).join("user-data"));
517 }
518 if let Ok(vscode_appdata) = env::var("VSCODE_APPDATA") {
519 for product_name in VSCODE_PRODUCT_NAMES {
520 paths.push(Path::new(&vscode_appdata).join(product_name));
521 }
522 }
523 for product_name in VSCODE_PRODUCT_NAMES {
524 add_vscode_user_data_paths(&mut paths, product_name);
525 }
526 paths
527}
528
529fn cursor_user_data_paths() -> Vec<PathBuf> {
530 let mut paths = Vec::new();
531 add_vscode_user_data_paths(&mut paths, "Cursor");
532 paths
533}
534
535fn add_vscode_user_data_paths(paths: &mut Vec<PathBuf>, product_name: &str) {
536 if cfg!(target_os = "macos") {
537 paths.push(
538 home_dir()
539 .join("Library/Application Support")
540 .join(product_name),
541 );
542 } else if cfg!(target_os = "windows") {
543 if let Some(data_local_dir) = dirs::data_local_dir() {
544 paths.push(data_local_dir.join(product_name));
545 }
546 if let Some(data_dir) = dirs::data_dir() {
547 paths.push(data_dir.join(product_name));
548 }
549 } else {
550 paths.push(
551 dirs::config_dir()
552 .unwrap_or(home_dir().join(".config"))
553 .join(product_name),
554 );
555 }
556}
557
558#[cfg(any(test, feature = "test-support"))]
559pub fn global_gitignore_path() -> Option<PathBuf> {
560 Some(home_dir().join(".config").join("git").join("ignore"))
561}
562
563#[cfg(not(any(test, feature = "test-support")))]
564pub fn global_gitignore_path() -> Option<PathBuf> {
565 static GLOBAL_GITIGNORE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
566 GLOBAL_GITIGNORE_PATH
567 .get_or_init(::ignore::gitignore::gitconfig_excludes_path)
568 .clone()
569}