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