paths.rs

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