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